Label OnDataBinding

R

rn5a

Consider the following code:

<script runat="server">
Sub ShowData(obj As Object, ea As EventArgs)
lblDate.Text = DateTime.Now.ToString("d")
lblDate.DataBind()
End Sub
</script>
<form runat="server">
<asp:Label ID="lblDate" OnDataBinding="ShowData" runat="server"/>
</form>

But the Label doesn't display the current date. Why?

If I replace the OnDataBinding event with OnInit or OnLoad or
OnPreRender in the Label control, then the Label displays the current
date.

Actually the Form has a DataList as well after the Label control.
Records from a SQL Server 2005 DB table are displayed in the DataList
using SqlDataReader. One of the columns in the DB table is named
'OrderDate' which gets populated with the date (along with the time) on
which a user has placed an order.

I want to display the date on which the order was placed by a
particular customer in the Label control while the DataList should
display the rest of the records existing in the other DB table columns.
To render the order date in the Label, I can use

While (sqlReader.Read)
lblDate.Text = "Your order was placed on " &
sqlReader.GetDateTime(5).ToString("d")
End While

But since SqlDataReader is read-only, even if I do something like this
after the While (SqlReader.Read) code snippet

DataList1.DataSource = sqlReader
DataList1.DataBind()

the DataList won't display the rest of the records. I need to first
display the order date in the Label followed by the rest of the records
in the DataList. How do I accomplish this?

Please note that I want to do this using SqlDataReader only (not
SqlDataAdapter or any other object).
 
B

bruce barker \(sqlwork.com\)

for a label control to fire on databind, a databind has to happen. you have
no code for this. the label needs to be a child of a control that will
databind its children, datalist, repeater, ...

you have two options.

1) use a datatable rather than a sqlreader. this is a hte best option as its
a bad practice to use datareaders for bindings.

2) catch the databind item event of the list , and if first row, update
caption.

-- bruce (sqlwork.com)

want to attach o the daalists
 
R

rn5a

Sorry to say, Bruce, but I couldn't exactly follow what you are trying
to say. You are saying that for a Label control to fire on DataBind, a
DataBind has to happen.....OK.....that's fine. Then you say that I
don't have any code for this. Then what is the code snippet I have
shown in post #1 for? I have the Label control with the OnDataBinding
event i.e.

<asp:Label ID="lblDate" OnDataBinding="ShowData" runat="server"/>

& then I call the sub ShowData. Doesn't the <asp:Label....> fire the
DataBind event of the Label control?

You also say that the Label needs to be a child of a control that will
DataBind it's children. Why the Label needs to be a child of a control?
Do other web server controls like TextBox, DropDownList, RadioButtons
etc. need to be children of a control as well? Moreover, what do you
mean by "control" here? Do you mean the 3 DataBinding controls -
Repeater, DataList & DataGrid?

Are you trying to say that the Label's OnDataBinding event will fire
only when the Label is contained within a Repeater, DataList & DataGrid
controls? If yes, does the same hold true for other web server controls
like TextBox, CheckBox etc....?

Please clarify my doubts...........please.........
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top