在 sharepoint 母版页上查找控件

发布于 2024-07-06 14:16:21 字数 1454 浏览 8 评论 0原文

我试图循环访问共享点页面上的所有控件,出于测试目的,我只想输出控件 ID,

这是我正在使用的代码

Public Shared Sub SubstituteValues3(ByVal CurrentPage As Page, ByRef s As StringBuilder )

    'Page()
    '- MasterPage
    '- HtmlForm
    '- ContentPlaceHolder
    '- The TextBoxes, etc.

    For Each ctlMaster As Control In CurrentPage.Controls


        If TypeOf ctlMaster Is MasterPage Then
            HttpContext.Current.Response.Output.Write("Master Page <br/>")

            For Each ctlForm As Control In ctlMaster.Controls

                If TypeOf ctlForm Is HtmlForm Then
                    HttpContext.Current.Response.Output.Write("HTML Form <br/>")

                    For Each ctlContent As Control In ctlForm.Controls
                        If TypeOf ctlContent Is ContentPlaceHolder Then
                            HttpContext.Current.Response.Output.Write("Content Placeholder <br/>")

                            For Each ctlChild As Control In ctlContent.Controls
                                HttpContext.Current.Response.Output.Write(ctlChild.ID.ToString & "<br />")
                            Next
                        End If
                    Next
                End If
            Next
        End If
    Next

    HttpContext.Current.Response.Output.Write("--------------")
    HttpContext.Current.Response.End()

但是它没有通过“MasterPage”输出。

我希望看到内容占位符中所有控件的名称,但我发现这有点令人困惑。

I'm trying to loop through all the controls on a sharepoint page, for the purposes of testing i just want to output the control ID

this is the code i'm using

Public Shared Sub SubstituteValues3(ByVal CurrentPage As Page, ByRef s As StringBuilder)

    'Page()
    '- MasterPage
    '- HtmlForm
    '- ContentPlaceHolder
    '- The TextBoxes, etc.

    For Each ctlMaster As Control In CurrentPage.Controls


        If TypeOf ctlMaster Is MasterPage Then
            HttpContext.Current.Response.Output.Write("Master Page <br/>")

            For Each ctlForm As Control In ctlMaster.Controls

                If TypeOf ctlForm Is HtmlForm Then
                    HttpContext.Current.Response.Output.Write("HTML Form <br/>")

                    For Each ctlContent As Control In ctlForm.Controls
                        If TypeOf ctlContent Is ContentPlaceHolder Then
                            HttpContext.Current.Response.Output.Write("Content Placeholder <br/>")

                            For Each ctlChild As Control In ctlContent.Controls
                                HttpContext.Current.Response.Output.Write(ctlChild.ID.ToString & "<br />")
                            Next
                        End If
                    Next
                End If
            Next
        End If
    Next

    HttpContext.Current.Response.Output.Write("--------------")
    HttpContext.Current.Response.End()

however it's not getting past the 'MasterPage' output.

I would expect to see the names of all the controls i have inside my content placeholder but i find it all a bit confusing.

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

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

发布评论

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

评论(4

花辞树 2024-07-13 14:16:21

从 Page.Master.Controls 开始

从那里你所拥有的基本上应该可以工作

        For Each ctlForm As Control In Page.Master.Controls

            If TypeOf ctlForm Is HtmlForm Then
                HttpContext.Current.Response.Output.Write("HTML Form <br/>")

                For Each ctlContent As Control In ctlForm.Controls
                    If TypeOf ctlContent Is ContentPlaceHolder Then
                        HttpContext.Current.Response.Output.Write("Content Placeholder <br/>")

                        For Each ctlChild As Control In ctlContent.Controls
                            HttpContext.Current.Response.Output.Write(ctlChild.ID.ToString & "<br />")
                        Next
                    End If
                Next
            End If
        Next

Start with Page.Master.Controls

From there what you have should basically work

        For Each ctlForm As Control In Page.Master.Controls

            If TypeOf ctlForm Is HtmlForm Then
                HttpContext.Current.Response.Output.Write("HTML Form <br/>")

                For Each ctlContent As Control In ctlForm.Controls
                    If TypeOf ctlContent Is ContentPlaceHolder Then
                        HttpContext.Current.Response.Output.Write("Content Placeholder <br/>")

                        For Each ctlChild As Control In ctlContent.Controls
                            HttpContext.Current.Response.Output.Write(ctlChild.ID.ToString & "<br />")
                        Next
                    End If
                Next
            End If
        Next
骷髅 2024-07-13 14:16:21

MasterPage 不是当前页面的控件,而是当前页面的一个属性,位于 Page.MasterPage

A MasterPage isn't a control of the current page, it's a property of it, in Page.MasterPage

颜漓半夏 2024-07-13 14:16:21

我发现这段代码似乎列出了我需要的控件,但我认为这更像是一种黑客攻击。

For i = 0 To CurrentPage.Request.Form.AllKeys.Length - 1
        If CurrentPage.Request.Form.GetKey(i).Contains("ctl00$PlaceHolderMain$") Then


            Dim key As String = CurrentPage.Request.Form.GetKey(i).Substring(22)
            Dim keyText As String = String.Format("[{0}]", key)

            HttpContext.Current.Response.Output.Write(keyText & "<br/>")

            'Text.Replace(keyText, CurrentPage.Request.Form("ctl00$PlaceHolderMain$" & key))
        End If
    Next

i found this piece of code which seems list the controls I need, i think it's more of a hack though.

For i = 0 To CurrentPage.Request.Form.AllKeys.Length - 1
        If CurrentPage.Request.Form.GetKey(i).Contains("ctl00$PlaceHolderMain$") Then


            Dim key As String = CurrentPage.Request.Form.GetKey(i).Substring(22)
            Dim keyText As String = String.Format("[{0}]", key)

            HttpContext.Current.Response.Output.Write(keyText & "<br/>")

            'Text.Replace(keyText, CurrentPage.Request.Form("ctl00$PlaceHolderMain$" & key))
        End If
    Next
π浅易 2024-07-13 14:16:21

你可以简单地用递归来做到这一点,效率不高,但很简单......尝试这个方法:
public void getControls(Control input)

{
    foreach (Control c in input.Controls)
    {
        Response.Write(c.GetType().ToString() + " - " + c.ID + "<br />");
        getControls(c);
    }
}

并像这样调用它:

getControls(Page);

这将循环遍历页面上的所有控件并输出它们的类型 - ID 并将其打印到页面顶部...您还可以使用代码来构建列表或任何您想做的事情。

you can do this simply with recursion, not efficent, but it is simple... try this method:
public void getControls(Control input)

{
    foreach (Control c in input.Controls)
    {
        Response.Write(c.GetType().ToString() + " - " + c.ID + "<br />");
        getControls(c);
    }
}

And call it like this:

getControls(Page);

That will cycle trough all controls on your page and output the type - ID of them and print it out to the top of the page... you could also use the code to construct a list or whatever you want to do.

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