Client ID When Overriding Render Method
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 colle开发者_如何学Pythonction 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)
Use UniqueID for the HtmlTextWriterAttribute.Id instead of ClientID.
精彩评论