Providing a Unique ID Attribute for Elements in a ScriptControl

I am creating a control based on ScriptControl and I am overriding the method Render

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 a div attribute to the ID attribute and have it unique on the page, even if there are multi-leaf instances of my control?

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

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

      

This will prevent name collisions between instances of my control, but what if I've already created a div somewhere else on the page that coincidentally has the same ID?

I have also heard about the implementation of INamingContainer. Will this apply here? How can I use it?

UPDATE

I worked around this by overriding CreateChildControls

and adding actual controls as opposed to rendering HTML directly. In this case, the INamingContainer does its job. However, I'm still wondering if there is a way to solve my original problem (unique IDs for directly rendered items).

+2


a source to share


3 answers


INamingController is a marker interface. The implementation will guarantee you unique identifiers for each instance of your control.

http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx



  public class MyScriptControl: ScriptControl, INamingContainer {

  }

      

0


a source


You don't want to instantiate controls just to get unique IDs, there are no unnecessary overhead / complex tasks in this approach. The Control.ID property may not be unique, but the Control.ClientID property will be unique. therefore

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

      



this attribute must be unique if you use the "_divTest" suffix twice in your custom control.

0


a source


I am trying to understand your problem:

public class SC : ScriptControl
{
    protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
    {
        return null;
    }
        protected override IEnumerable<ScriptReference> GetScriptReferences()
        {
            return null;
        }
    }
    //... Page code
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var sc = new SC();
            var sc1 = new SC();
            Page.Form.Controls.Add(sc);
            Page.Form.Controls.Add(sc1);
        }

      

But sc and sc1 have different ClientIDs. So this is not an ASP.NET problem. Look at your awareness. Maybe you are creating ids for divs before adding ScriptControls to the page, or maybe you are trying to create 2 divs within one ScriptControl or maybe you are dynamically creating ScriptConrol2 in async-posback and have the same ID as and ScriptControl1, which is not dynamically added on postback.

0


a source







All Articles