0

I am using vb.net master pages and .net is putting ctl00$CPHContent$ and ctl00_CPHContent_ before the control ID and control name.

I am trying to use the findControl to look for my control but it is not finding the controls.

any ideas or suggestions..... I can't use javascript to find a solution

WingMan20-10
  • 3,594
  • 9
  • 31
  • 42

3 Answers3

6

The ID generation you are seeing is because your controls are inside another control that implements INamingContainer. The whole purpose of this is to allow templated controls - for example, every row of a datagrid might have a TextBox with the ID "TextBox1". Clearly all the textboxes can't have the same ID - so the DataGrid qualifies those controls with a prefix on their ID's.

Most Asp.Net controls that have a Controls collections will implement INamingContainer (such as Panel).

INamingContainer doesn't hamper the functionality of FindControl though. For example, you can still search for "TextBox1" inside each DataGrid row from the above example.

The problem is likely that you're likely not calling FindControl() on the right container (Page.FindControl isn't recursive - it only searches for controls immediately inside it's own Controls collection.)

If you need a recursive version of FindControl(), I put code for it in this old answer.

Community
  • 1
  • 1
womp
  • 115,835
  • 26
  • 236
  • 269
  • Good point about the non-recursive nature of FindControl(). Forgot about that one. – Carl Feb 17 '10 at 20:24
  • I dynamiclly created all my controls and placed them in a panel, this i am using panel1.findcontorl. I tested the same code on a page that is not using master pages and it works fine – WingMan20-10 Feb 17 '10 at 20:28
  • Where are you calling FindControl()? Are you sure your controls are on the page by the time you call FindControl()? – Carl Feb 17 '10 at 20:32
  • Yes I have a search button at the end of the form, i can calling the findcontrol in the click event of that button. – WingMan20-10 Feb 17 '10 at 20:36
  • 1
    A MasterPage is an INamingContainer, so if your FindControl code is directed at the Page object in the working code, the controls aren't in the same place when you put it inside the master page. You're going to need to look for it inside the ContentPlaceHolders. See here for an example: http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx – womp Feb 17 '10 at 20:48
1

On server-side, in your VB.NET code, you should be able to find your control by the ID you gave it. So, if you had something like

You can access it on your server-side by calling FindControl("myPlaceHolder"). What you're seeing, "ctl00_CPHPContent_" is .NET's way of making sure each element on the front end (read: HTML) all have unique IDs across the board, so it gives it names denoting its location on the page, etc.

So, if I understand you correctly, you're using FindControl("ctl100_CPHPContent_myPlaceHolder") and it's coming up null? Or are you using FindControl("myPlaceHolder") and it's not finding the control?

Another thing to keep in mind is how you're getting the control on the page? Are you writing it into the aspx or ascx file, or are you created it dynamically in VB.NET? If it's the later, be sure you're creating dynamic controls during the correct stage of the page lifecycle, i.e. in Page_Init.

Hope that helps a little.

Carl
  • 1,706
  • 1
  • 17
  • 25
  • I've tried both methods FindControl("ctl100_CPHPContent_myPlaceHolder") and FindControl("myPlaceHolder") I still get null #2 i am declaring all the controls dynamically in the page int. in my aspx file – WingMan20-10 Feb 17 '10 at 20:35
1

This might be a little brute force, but you can customize to your needs.

I put this function in my base page class, which allows me to find a control anywhere in the contentBody ContentPlaceHolder:

''' <summary>Finds a control on a page, even if the page is from a master page</summary>
''' <param name="id">Id of control to find on page</param>
''' <returns>The control object, if found</returns>
Public Function FindAControl(ByVal id As String) As Control
    Dim result As Control = Nothing
    Dim contentBody As Control = Me.Controls(0).FindControl("contentBody")
    If contentBody IsNot Nothing Then
        result = contentBody.FindControl(id)
    Else
        result = Me.FindControl(id)
    End If
    Return result
End Function

In your MasterPage, you have something like this:

<asp:ContentPlaceHolder ID="contentBody" runat="server"/>

The ID you see there, is what you put in:

Dim contentBody As Control = Me.Controls(0).FindControl("contentBody")
slolife
  • 19,520
  • 20
  • 78
  • 121
  • Note, you'll have to change the contentBody string in Me.Controls(0).FindControl("contentBody") to your own ID. The function can be made a lot more generic and functional, but I just copy/pasted from my code. – slolife Feb 17 '10 at 21:40
  • would i put the ID of the content place holder on my master page or do i put the ID of the content on my child page?? I think this might work – WingMan20-10 Feb 18 '10 at 18:25
  • Added more description to the answer. It should be the ID of the – slolife Feb 18 '10 at 23:04