views:

43

answers:

3

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

+4  A: 

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.

womp
Good point about the non-recursive nature of FindControl(). Forgot about that one.
Carl
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
Where are you calling FindControl()? Are you sure your controls are on the page by the time you call FindControl()?
Carl
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
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
+1  A: 

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
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
+1  A: 

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
I will give this a shot, let you know how this works for me.
WingMan20-10
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
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
Added more description to the answer. It should be the ID of the <asp:content control in the master page
slolife