如何以简单的方式获取 ContentPlaceHolder 的客户端 id?
我需要在某些控件属性中添加 JavaScript 调用,我正在使用母版页,但为了获取 contentplaceholder 客户端 ID,我正在迭代表单控件。 还有其他方法可以在内容页的服务器端代码中获取吗?
foreach (Control control in this.Form.Controls)
{
if (control is ContentPlaceHolder)
{
contentPlaceHolderID = control.ClientID;
break;
}
}
i need to add JavaScript calls in some controls attributes, i am using master pages but in order to obtain the contentplaceholder client id i am iterating over the forms controls.
is there another way to obtain in the server side code of the content page?
foreach (Control control in this.Form.Controls)
{
if (control is ContentPlaceHolder)
{
contentPlaceHolderID = control.ClientID;
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果只有很少的这样的控件,您可以设置一个全局变量:
If there's only few controls like this you can set up a global variable:
我在 jQuery 中执行以下操作:
这会搜索 ID 中包含 DivFullWarning 的任何控件,因此您可以将 ContentPlaceHolder 命名为“contentPlaceHolder”,然后无论您的 id 中有多少 asp.net 垃圾,您仍然可以获取它是客户端。
您可以只在某处公开内容 placeHolder,这样您就不必循环遍历所有控件。
I do the following in jQuery:
this searches for any control that contains DivFullWarning in their ID, so you could name your ContentPlaceHolder "contentPlaceHolder" and then no matter how much asp.net muck's with your id, you'll still be able to grab it client side.
You could just expose the content placeHolder somewhere so you don't have to loop through all the controls.