I made a login control which have a ValidationGroup. I used this control twice in Master page. However, the validation will check both controls. What I need is it only checks the fields within its containing control...
the html elements in the usercontrol will also show same Ids, which made getElementById not work in javascript
I m wondering how to avoid such conflicts? Thanks.
To avoid problems with IDs you can make your control implement the INamingContainer interface. It's a marker interface that simply tells ASP.NET that the controls within should have a parent ID prefix in their client IDs. More info here.
As to ValidationGroup, I see two options. First - define a public property for your control. Set it in the markup (make sure it is different for each of your controls on the Master) and in control's PreRender handler set it programmaticly for each validator etc.
public partial class MyLoginControl : Control
{
...
public string ValidationGroup
{
get; set;
}
...
protected void Page_PreRender(object sender, EventArgs e)
{
RequredValidator1.ValidationGroup = this.ValidationGroup;
}
...
}
Second approach is basically the same, but instead of public property you can use control's ID:
public partial class MyLoginControl : Control
{
...
protected void Page_PreRender(object sender, EventArgs e)
{
RequredValidator1.ValidationGroup = this.ID + "ValidationGroup";
}
...
}