这里很容易。
我在主页内的控件内有一个控件。页面名称在HTML
中看起来像这样ctl00$MasterPageBody$MainControl$ChildControl
知道如何从代码中获取上述内容吗?
谢谢!
答案 0 :(得分:1)
它是控件的clientID属性。
答案 1 :(得分:1)
如果我们在讨论页面上的顶级控件,或者至少有一个不是任何重复类型的控件,则可以使用ClientID
属性。
<asp:Label runat="server" ID="testLabel" />
<script>
$('#<%= testLabel.ClientID %>').click(function() { ... });
</script>
如果我们正在谈论不能直接访问的内容,那么你必须做同样的事情,但它必须包含在{{1}中}。这样的例子不是那么干净,所以我相信你可以从我的话语中理解。但基本上,如果您有一个标签在FindControl
的每一行显示代码隐藏的给定文本,则您必须调用GridView
。仍然很直接,但比第一种情况复杂一点。
答案 2 :(得分:0)
您应该使用与此类似的功能:
public static Control FindControlRecursive(Control ctl, string id) {
if (!ctl.HasControls())
return null;
Control res = null;
foreach(Control c in ctl.Controls) {
if (c.ID == id) {
res = c;
break;
} else {
res = FindControlRecursive(c, id);
if (res != null)
break;
}
}
return res;
}
以这种方式:
Control ChildControl = FindControlRecursive(this.Page, "ChildControl");
string ID = ChildControl.ClientID;