Specify control within DataList Items

Posted on 2007-10-10
Last Modified: 2013-12-26
I am trying to specify a specific item/control inside the DataList control in my VB.NET codebehind.

I have a StartDateLabel and EndDateLabel. Both are databound to a SQL database. What I need to do is this;

    StartDateLabel.Text = EndDateLabel.Text
    EndDateLabel.Visible = False
End If

Again, the problem is I don't know how to plug it in. What I need to know is how to specify the control within the Datalist Items. I have not worked with the Datalist control much (until recently) and could really use some help in this regard.

Question by:hacresIT
    LVL 37

    Expert Comment

    you probably need to use "findcontrol" to...find the control...within the datalist....
    within the itemdatabound event of the datalist, try something like:

    Dim lbstart as label
    dim lbend as label
    lbstart = ctype(datalistname.findcontrol("startdatelabel"),label)
    lbend= ctype(datalistname.findcontrol("enddatelabel"),label)

    if lbstart.text = lbend.text then
    lbend.visible = false
    end if
    LVL 1

    Author Comment

    Ok, this is what I have so far:
        Protected Sub DataList1_ItemDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
            Dim StartDate As Label = CType(DataList1.FindControl("StartDateLabel"), Label)
            Dim EndDate As Label = CType(DataList1.FindControl("EndDateLabel"), Label)

            If StartDate.Text = EndDate.Text Then
                EndDate.Visible = False
            End If
        End Sub
    I'm getting the following error:

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 7:          Dim EndDate As Label = CType(DataList1.FindControl("EndDateLabel"), Label)
    Line 8:  
    Line 9:          If StartDate.Text = EndDate.Text Then
    Line 10:             EndDate.Visible = False
    Line 11:         End If
    LVL 37

    Accepted Solution

    instead of:
    Dim EndDate As Label = CType(DataList1.FindControl("EndDateLabel"), Label)

    Dim EndDate As Label = CType(e.Item.FindControl("EndDateLabel"), Label)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now