ctl00_CPH.net 中的内容

发布于 2024-08-21 22:19:21 字数 175 浏览 5 评论 0原文

我正在使用 vb.net 母版页,.net 将 ctl00$CPHContent$ 和 ctl00_CPHContent_ 放在控件 ID 和控件名称之前。

我试图使用 findControl 来查找我的控件,但它没有找到控件。

任何想法或建议......我无法使用 javascript 来找到解决方案

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶舞 2024-08-28 22:19:21

您看到的 ID 生成是因为您的控件位于另一个实现 INamingContainer 的控件内。这样做的全部目的是允许模板化控件 - 例如,数据网格的每一行都可能有一个 ID 为“TextBox1”的 TextBox。显然,所有文本框不能具有相同的 ID - 因此 DataGrid 使用 ID 上的前缀来限定这些控件。

大多数具有 Controls 集合的 Asp.Net 控件将实现 INamingContainer(例如 Panel)。

不过,INamingContainer 不会妨碍 FindControl 的功能。例如,您仍然可以在上面示例中的每个 DataGrid 行中搜索“TextBox1”。

问题很可能是您可能没有在正确的容器上调用 FindControl()Page.FindControl 不是递归的 - 它仅立即在其自己的内部搜索控件控件集合。)

如果您需要 FindControl() 的递归版本,我为其添加了代码

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.

鱼窥荷 2024-08-28 22:19:21

在服务器端,在 VB.NET 代码中,您应该能够通过您提供的 ID 找到您的控件。因此,如果您有类似的内容,

您可以通过调用 FindControl("myPlaceHolder") 在服务器端访问它。您所看到的,“ctl00_CPHPContent_”是 .NET 确保前端(即 HTML)上的每个元素都具有唯一 ID 的方法,因此它给出了表示其在页面上的位置的名称,等等

。 ,如果我理解正确的话,您正在使用 FindControl("ctl100_CPHPContent_myPlaceHolder") 并且它会出现空值?或者您使用 FindControl("myPlaceHolder") 但它没有找到控件?

另一件要记住的事情是如何获得页面上的控件?您是将其写入 aspx 或 ascx 文件中,还是在 VB.NET 中动态创建它?如果是后者,请确保在页面生命周期的正确阶段(即在 Page_Init 中)创建动态控件。

希望能有所帮助。

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.

浴红衣 2024-08-28 22:19:21

这可能有点暴力,但您可以根据您的需求进行定制。

我将此函数放在我的基页类中,这使我可以在 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

在您的 MasterPage 中,您有类似这样的内容:

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

您在那里看到的 ID 就是您输入的内容:

Dim contentBody As Control = Me.Controls(0).FindControl("contentBody")

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文