简体   繁体   English

在Sharepoint主页上查找控件

[英]Find controls on sharepoint master page

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 我试图遍历一个共享点页面上的所有控件,出于测试目的,我只想输出控件ID

this is the code i'm using 这是我正在使用的代码

Public Shared Sub SubstituteValues3(ByVal CurrentPage As Page, ByRef s As StringBuilder) Public Shared Sub SubstituteValues3(ByVal CurrentPage作为页面,ByRef作为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. 但是它不会超过“ MasterPage”输出。

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. 我希望看到我在内容占位符中拥有的所有控件的名称,但是我发现它们有点令人困惑。

Start with Page.Master.Controls 从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

MasterPage不是当前页面的控件,而是它的属性,在Page.MasterPage中

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

you can do this simply with recursion, not efficent, but it is simple... try this method: public void getControls(Control input) 您可以简单地通过递归来执行此操作,而不是有效的,但这很简单...试试此方法: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. 这将循环遍历页面上的所有控件并输出类型-它们的ID并将其打印到页面顶部...您还可以使用该代码构造列表或执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM