在Sharepoint主页上查找控件
时间:2020-03-06 14:55:55 来源:igfitidea点击:
我试图遍历一个共享点页面上的所有控件,出于测试的目的,我只想输出控件ID
这是我正在使用的代码
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()
但是它不会超过" MasterPage"输出。
我希望看到我在内容占位符中拥有的所有控件的名称,但我发现它们有点令人困惑。
解决方案
MasterPage不是当前页面的控件,而是它的属性,在Page.MasterPage中
从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
我发现这段代码似乎列出了我需要的控件,不过我认为这更像是黑客。
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
我们可以通过递归简单地执行此操作,而不是有效的操作,但这很简单...尝试以下方法:
public void getControls(控制输入)
{ foreach (Control c in input.Controls) { Response.Write(c.GetType().ToString() + " - " + c.ID + "<br />"); getControls(c); } }
并这样称呼它:
getControls(Page);
这将循环浏览页面上的所有控件并输出它们的类型ID,并将其打印到页面顶部...我们还可以使用代码构造列表或者执行任何操作。