[RESOLVED] FindControl method doesn't work with masterpage

Hi all, i have a masterpage and other pages. i want to use findcontrol method to find a textbox (not on the master page) to check whether it is empty or not.

my code is as folows;

Dim myContentPlaceHolder As ContentPlaceHolder = CType(Master.FindControl("MainContent"), ContentPlaceHolder)

Dim UpdatePanel1 As UpdatePanel = CType(myContentPlaceHolder.FindControl("UP1"), UpdatePanel)

Any help please. 


Dim tmptxt As TextBox = CType(Master.FindControl("txtbox1"), TextBox)

If Not tmptxt Is Nothing Then
 'get tmptxt.Text value
End If

my textbox in not on the masterpage, it is on the default.aspx page. i tried

Dim myTextBox As TextBox = CType(Master.FindControl("TextBox1"), TextBox)  , but doesn't worked. Any more help please

i should also mention that the TextBox1 is inside listview, the listview is inside an updatepanel (updatepanel is inside a panel -ajax modalpopupextender).


First you need to find the textbox in listview. Since I'm not sure which textbox you find in listview. Here's a example:

<asp:ListView ID="ListView1" runat="server">
                <tr id="Tr2" runat="server">
                    <td align="center">
                        <asp:Label ID="lbP1" runat="Server" Text='<%#Eval("P1") %>' />
                    <td align="center">
                        <asp:Label ID="lbP2" runat="Server" Text='<%#Eval("P2") %>' />

Protected Sub FindItems()
	Dim ctl As Control = Me.ListView1.Items(0).FindControl("lbP1")  //find textbox in the first item of ListView
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs)
End Sub

Find the child page's ListView in masterpage:

Dim panel As UpdatePanel = TryCast(Me.ContentPlaceHolder1.FindControl("UpdatePanelID"), UpdatePanel)
Dim listview As ListView = TryCast(panel.FindControl("ListViewID"), ListView)

Please notice that if the control is wrapped in a parent control, you need to first find the control from this parent control(container).

Here are some good article:



Hope this can help you.

thoughts on "[RESOLVED] FindControl method doesn't work with masterpage"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>