Is there a way to control how .Net sets the Name
and ID
property of my controls? I have some radio buttons for which I need the name to be the same.
Yes.
Declare a descendant class from, say, a textbox, which overrides the UniqueID and ClientID properties. Then, use (in this example) MyControls.Textbox instead of the built-in textbox.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyControls
{
public class Textbox : System.Web.UI.WebControls.TextBox
{
public override string ClientID
{
get
{
return ID;
}
}
public override string UniqueID
{
get
{
return ID;
}
}
}
public class Button : System.Web.UI.WebControls.Button
{
public override string ClientID
{
get
{
return ID;
}
}
public override string UniqueID
{
get
{
return ID;
}
}
}
}
Note that you'll need to register your controls in web.config:
Then you can reference them in your markup: