Re: Label OnDataBinding

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.........


bruce barker (sqlwork.com) wrote:
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
<rn5a@xxxxxxxxxxxxxx> wrote in message
news:1160776153.119516.319180@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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).


.



Relevant Pages

  • RE: Userform help text
    ... You can then make the button display a MsgBox ... control to the appropriate context ID in the help project. ... TextBox on a single UserForm. ... I'd go with the Label. ...
    (microsoft.public.word.vba.general)
  • Re: Problem with free form text
    ... The examples I've found all show using a Label in a Panel In an ItemTemplate ... to display the field and a Multiline Textbox in the EditItemTemplate. ... with the proper new line sequence for the respective control. ... how do I refernce the Label and Textbox fields burried in the ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Reapeter Control and Array List
    ... >I have an array list that I want to display to the browser. ... >precise control of the repeater, so I have function for the OnItemDataBound ... >with client side script. ... so instead I use a label and in the text of the label I set it to ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: dynamically display label and checkbox
    ... don't know why I got off on the wrong path of trying to display the lbls ... control has an expression as the ControlSource, if it does then you can't ... as for the Visible property of a label, setting it will only have an effect ... filter while in Form2, i'd suggest that instead of filtering the recordset ...
    (microsoft.public.access.modulesdaovba)
  • Re: How to modify label.text in a dynamically generated label in VB.net
    ... hybrid control that contains a checkbox and a label. ... That frees up the tag ... CheckBox control and the value was the Label control. ...
    (microsoft.public.dotnet.languages.vb)