如何以简单的方式获取 ContentPlaceHolder 的客户端 id?

发布于 2024-07-16 14:58:47 字数 419 浏览 4 评论 0原文

我需要在某些控件属性中添加 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 技术交流群。

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

发布评论

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

评论(2

土豪 2024-07-23 14:58:47

如果只有很少的这样的控件,您可以设置一个全局变量:

var contentPlaceholderId = '<%= this.ContentPlaceholder1.ClientId %>'

If there's only few controls like this you can set up a global variable:

var contentPlaceholderId = '<%= this.ContentPlaceholder1.ClientId %>'
梦屿孤独相伴 2024-07-23 14:58:47

我在 jQuery 中执行以下操作:

$("*[id$='DivFullWarning']").each(function() {
  // do whatever in here
});

这会搜索 ID 中包含 DivFullWarning 的任何控件,因此您可以将 ContentPlaceHolder 命名为“contentPlaceHolder”,然后无论您的 id 中有多少 asp.net 垃圾,您仍然可以获取它是客户端。

您可以只在某处公开内容 placeHolder,这样您就不必循环遍历所有控件。

I do the following in jQuery:

$("*[id$='DivFullWarning']").each(function() {
  // do whatever in here
});

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.

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