Page.Controls(0).FindControl("tbxCompetitor01Product")

G

Guest

Hello,

I am looping through each control using the following to find a particular
control (strFindCtl = "tbxCompetitor" & strForLoop1 & "Product").

For ctrRow = 1 To 3
strForLoop1 = "0" & ctrRow
strFindCtl = "tbxCompetitor" & strForLoop1 & "Product"
For Each ctlMaster As Control In Page.Controls ' Page
If TypeOf ctlMaster Is MasterPage Then '
MasterPage
For Each ctlForm As Control In ctlMaster.Controls
If TypeOf ctlForm Is HtmlForm Then '
HtmlForm
For Each ctlContent As Control In ctlForm.Controls
If TypeOf ctlContent Is ContentPlaceHolder Then
'ContentPlaceHolder
For Each ctlChild As Control In ctlContent.Controls

If TypeOf ctlChild Is TextBox Then
If ctlChild.ID = strFindCtl Then strSubmitter &=
CType(ctlChild, TextBox).Text & ", "
End If

Next
End If
Next
End If
Next
End If
Next
Next




Based on the article
(http://dotnetjunkies.com/WebLog/jpalermo/archive/2004/09/01/23919.aspx), I
am trying to follow the format for the following in order to reduce my code:
(MyUserControl)Page.Controls[0].FindControl(â€ucMyUserControlâ€);

However, when I use the following:

Dim ctlSteven As Control =
Page.Controls(0).FindControl("tbxCompetitor01Product")
Response.Write(ctlSteven.ID)

I get the error:
I get the error: System.NullReferenceException: Object reference not set to
an instance of an object.

Any help with this would be appreciated.
 
G

Guest

Hi Scott,

For my loop, I referenced the contentplaceholder on the master page as you
suggested (and it works!). One thing I ran into is that I was unable to
figure out how to reference the the "Content" control (id="Content1") on the
content page or if I even need too. Thanks again for your help...

This is on the master page:
<asp:ContentPlaceHolder ID="cphMainContent"
Runat="server"></asp:ContentPlaceHolder>

Is on the content page:
<asp:Content ID="Content1" ContentPlaceHolderID="cphMainContent"
Runat="server">

This works...
For ctrRow = 1 To 3
strForLoop1 = "0" & ctrRow
strFindCtl = "tbxCompetitor" & strForLoop1 & "Product"
ctlFind = Master.FindControl("cphMainContent").FindControl(strFindCtl)
strFrmObj = CType(ctlFind, TextBox).Text & "<br>"
Response.Write(strFindCtl & " = " & strFrmObj)
Next

Scott Allen said:
Hi sck10:

You have to be aware of 'naming containers' when using FindControl.
You'll need to invoke FindControl on the ContentPlaceHolder instance.

For an example of what I mean, see my article:
In Search Of ASP.Net Controls
http://odetocode.com/Articles/116.aspx

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

Hello,

I am looping through each control using the following to find a particular
control (strFindCtl = "tbxCompetitor" & strForLoop1 & "Product").

For ctrRow = 1 To 3
strForLoop1 = "0" & ctrRow
strFindCtl = "tbxCompetitor" & strForLoop1 & "Product"
For Each ctlMaster As Control In Page.Controls ' Page
If TypeOf ctlMaster Is MasterPage Then '
MasterPage
For Each ctlForm As Control In ctlMaster.Controls
If TypeOf ctlForm Is HtmlForm Then '
HtmlForm
For Each ctlContent As Control In ctlForm.Controls
If TypeOf ctlContent Is ContentPlaceHolder Then
'ContentPlaceHolder
For Each ctlChild As Control In ctlContent.Controls

If TypeOf ctlChild Is TextBox Then
If ctlChild.ID = strFindCtl Then strSubmitter &=
CType(ctlChild, TextBox).Text & ", "
End If

Next
End If
Next
End If
Next
End If
Next
Next




Based on the article
(http://dotnetjunkies.com/WebLog/jpalermo/archive/2004/09/01/23919.aspx), I
am trying to follow the format for the following in order to reduce my code:
(MyUserControl)Page.Controls[0].FindControl(â€ucMyUserControlâ€);

However, when I use the following:

Dim ctlSteven As Control =
Page.Controls(0).FindControl("tbxCompetitor01Product")
Response.Write(ctlSteven.ID)

I get the error:
I get the error: System.NullReferenceException: Object reference not set to
an instance of an object.

Any help with this would be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top