Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


How to get unique ClientId in Custom .NET Server Control
If you use RenderContents to render child controls in your Render method of a custom web control, the ID will not be set to unique like it is when you add the controls as children of your web control.
This is necessary if you are planning to use the control in javascript and need a unique ID (such as having multiples of your control on a page)

To fix, update the ID programmatically prior to rendering the child control.

public class CustomBox : TextBox
{
Label lblResult = new Label();
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer); // render me
writer.WriteBreak(); // render some HTML
lblResult.ID = Parent.ClientID + "_lblResult"; // generate valid unique id
lblResult.RenderControl(writer); // render a child
}
}

Created By: amos 4/16/2015 2:42:40 PM
Updated: 4/16/2015 2:43:50 PM