Specify control within DataList Items

Posted on 2007-10-10
Medium Priority
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
  • 2
LVL 37

Expert Comment

ID: 20048819
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

Author Comment

ID: 20049310
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

samtran0331 earned 1000 total points
ID: 20049435
instead of:
Dim EndDate As Label = CType(DataList1.FindControl("EndDateLabel"), Label)

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 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