Link to home
Start Free TrialLog in
Avatar of hacresIT
hacresITFlag for United States of America

asked on

Specify control within DataList Items

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;

If
    StartDateLabel.Text = EndDateLabel.Text
Then
    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.

Thanks
Avatar of samtran0331
samtran0331
Flag of United States of America image

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
Avatar of hacresIT

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial