The trick is to make the Validation Group name in each user control unique. Since its a single user control used in both places, you have to make it unique programmatically. Do this in Page_Load of the user control:
Assign the value of the UniqueID property of the UserControl to each ValidationGroup property on the UserControl. Something like this:
public string ValidationGroup { get { if (ViewState["VALGROUP"] == null) return this.ClientID; else return (string)ViewState["VALGROUP"]; } set { ViewState["VALGROUP"] = this.ClientID; } }Create an UserControl Test.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Test.ascx.cs" Inherits="Test" %> <asp:TextBox ID="txt1" runat="Server"></asp:TextBox> <asp:RequiredFieldValidator ID="req1" runat="Server" ErrorMessage="(Require)" ControlToValidate="txt1"></asp:RequiredFieldValidator> <asp:Button ID="btnAdd" runat="server" Text="Add" />Add Multiple Instance of UserControl on test page.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <%@ Register Src="Test.ascx" TagName="Test" TagPrefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <table style="width: 100%"> <tr> <td style="width: 100px"> <uc1:Test ID="Test1" runat="server" /> </td> <td style="width: 100px"> <uc1:Test ID="Test3" runat="server"></uc1:Test> </td> </tr> <tr> <td style="width: 100px"> <uc1:Test ID="Test2" runat="server" /> </td> <td style="width: 100px"> <uc1:Test ID="Test4" runat="server" /> </td> </tr> </table> </div> </form> </body> </html>


No comments:
Post a Comment