Ensuring unique ID attribute for elements within ScriptControl

Posted by Andy West on Stack Overflow See other posts from Stack Overflow or by Andy West
Published on 2010-04-27T19:41:25Z Indexed on 2010/04/27 19:43 UTC
Read the original article Hit count: 429

I'm creating a control based on ScriptControl, and I'm overriding the Render method like this:

protected override void Render(HtmlTextWriter writer)
{
    RenderBeginTag(writer);

    writer.RenderBeginTag(HtmlTextWriterTag.Div);
    writer.Write("This is a test.");
    writer.RenderEndTag();

    RenderEndTag(writer);
}

My question is, what if I want to assign the div an ID attribute and have it be unique on the page, even if there are mulitple instances of my control?

I've seen other people's code that does this:

writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID + "_divTest");

That will prevent naming conflicts between instances of my control, but what if I've already created a div elsewhere on the page that coincidentally has the same ID?

I've also heard about implementing INamingContainer. Would that apply here? How could I use it?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#