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
3 Years
Discussion Span
Last Post by Antpit

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.

Edited by JorgeM


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.