I'm trying to simply get the value of a FormView control IDLabel1 that is placed within a div

IDLabel1 is a bound control getting data from a database via an sqldatasource control
I've tried the following:



Dim lbl As Label = DirectCast(FindControl("IDLabel1"), Label)

Dim lbl As Label = DirectCast(FindControl("FormView1&IDLabel1"), Label) 

Dim lbl As Label = DirectCast(Page.FindControl("FormView1").FindControl(IDLabel1), Label) 

All the above returns error control has null value. I've also tried the following Function:

Public Shared Function FindCont(controlId As String, controls As ControlCollection) As Control
        For Each control As Control In controls
            If control.ID = controlId Then
                Return control
            End If

            If control.HasControls() Then
                Dim nestedControl As Control = FindCont(controlId, control.Controls)

                If nestedControl IsNot Nothing Then
                    Return nestedControl
                End If
            End If
        Return Nothing
    End Function

I've also tried data binding before calling FindControl

Again using this function I only get a null return.

The only thing I can think of now is that the problem might have something to do with the div??

Any help will be appreciated

Recommended Answers

Are you using master pages ? Is this control with in a content placeholder?

Jump to Post

All 4 Replies

Are you using master pages ? Is this control with in a content placeholder?

Yes I'm using a Master Page and yes the control is within a placeholder.

So you have to incorporate the content place holder in your search. For example say your label is wihin a contentplaceholder....

ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
Label lbl = (Label)cph.FindControl("IDLabel1");
lbl.Visible = true;  // or approprite code for this control.

Thanks for that. I'm just about to board a flight. I'll try it tonight, it seems very logical your suggestion I had not taken the Master page into consideration.
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.