Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To generate unique ID for controls inside a User Control

I have a User Control in which I have one label.

<asp:Label ID="lblProductionName" 
           runat="server" 
           Text="Production Name will come here">
</asp:Label>

I render the given UC from code behind using this function:

private string RenderControl(Control control)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter writer = new HtmlTextWriter(sw);

    control.RenderControl(writer);
    return sb.ToString();
}

After rendering, I get this as the output string:

<span id="lblProductionName">Production Name will come here</span>

Now, when I put two instances of the same User Control, I get the same span ID in the output string.

I want to generate two different IDs for two instances of User Control. How can I generate it?

like image 271
Sid M Avatar asked Oct 07 '13 12:10

Sid M


People also ask

How are unique IDs generated?

Sometimes called time-based UUIDs, these IDs are generated using a combination of datetime values (reflecting the time the UUID is being generated), a random value, and a part of the MAC address of the device generating the UUID.

What is unique ID asp net?

The UniqueID property is the page-wide unique identifier of an ASP.NET server control. Its uniqueness is guaranteed by prefixing the ID of a server control with the ID of its NamingContainer. If the NamingContainer is the Page the UniqueID will remain the same as the ID.


1 Answers

The option which always works would be: Guid.NewGuid :)

like image 189
Rinat Galyautdinov Avatar answered Oct 05 '22 06:10

Rinat Galyautdinov