ctl00_CPHContent in .net
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 con开发者_开发问答trol but it is not finding the controls.
any ideas or suggestions..... I can't use javascript to find a solution
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.
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.
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")
精彩评论