重写渲染方法时的客户端 ID
我正在使用自定义 Web 控件,其中 Render 方法被重写。
我的问题是渲染的控件的ID不包括该控件所在的母版页和用户控件的ID。
这意味着如果我的渲染方法包含以下内容:
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.RenderEndTag();
并且按如下方式使用:
<somePrefix:MyWebControl ID="WebControl1" runat="server" />
那么控件将呈现为:
<div id="WebControl1"></div>
虽然我想要类似的东西:(
<div id="ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolderMain_WebControl1"></div>
如果我放置一个 ID 为 WebControl1 的 div 并运行,则会生成以下内容) =“服务器”)
我可以做什么来实现这一目标?
编辑
我对这种情况发生的时间是错误的。当我将控件直接放置在页面中时,id 就可以了(asp.net 生成的 id)。
但是,当从另一个包含我的控件集合的 Web 控件呈现 MyControl 时,就会出现问题。
在容器控件中,RenderControl 方法被重写,如下所示:
foreach (Control control in MyControls)
{
divControls.Controls.Add(control);
}
divControls.RenderControl(writer)
I'm using a custom web control where the Render method was overriden.
My problem is that the ID of the rendered control does not include IDs the master pages and user controls where the control is located.
Meaning that if my render method includes the following:
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.RenderEndTag();
And it is used as follows:
<somePrefix:MyWebControl ID="WebControl1" runat="server" />
Then the control will be rendered as:
<div id="WebControl1"></div>
While I want something like:
<div id="ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolderMain_WebControl1"></div>
(which is what would be generated if I put up a div with the ID WebControl1 and runat="server")
What can I do to achieve that?
Edit
I was wrong regarding when this is happening. When I place the control directly in the page, the id is fine (asp.net generated id).
However, the problem occurs when MyControl is rendered from another web controls, which holds a collection of my controls.
In the container control the RenderControl method is overriden as follows:
foreach (Control control in MyControls)
{
divControls.Controls.Add(control);
}
divControls.RenderControl(writer)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对 HtmlTextWriterAttribute.Id 使用 UniqueID 而不是 ClientID。
Use UniqueID for the HtmlTextWriterAttribute.Id instead of ClientID.