Validating textboxes Dynamically

Prabhukiran345
Posted by Prabhukiran345 under C# category on | Points: 40 | Views : 1882
In .cs Page:
Note: (Here i am creating my textboxes too dynamically)
protected void Page_Load(object sender, EventArgs e)
{
TextBox tb1 = new TextBox() { ID="txt1" };
RequiredFieldValidator reqTxt1 = new RequiredFieldValidator();
reqTxt1.ControlToValidate = "txt1";
reqTxt1.ID = "req1";
reqTxt1.ForeColor = Color.Red;
reqTxt1.ValidationGroup = "gp1";
reqTxt1.Display = ValidatorDisplay.None;
reqTxt1.ErrorMessage = "text Cant be empty";
TextBox tb2 = new TextBox() { ID = "txt2" };
RequiredFieldValidator reqTxt2 = new RequiredFieldValidator();
reqTxt2.ControlToValidate = "txt2";
reqTxt2.ID = "req2";
reqTxt2.Display = ValidatorDisplay.None;
reqTxt2.ForeColor = Color.Red;
reqTxt2.ValidationGroup = "gp1";
reqTxt2.ErrorMessage = "text Cant be empty";
pnl.Controls.Add(tb1);
pnl.Controls.Add(reqTxt1);
pnl.Controls.Add(new LiteralControl("<br/>"));
pnl.Controls.Add(tb2);
pnl.Controls.Add(reqTxt2);
ValidationSummary vs = new ValidationSummary();
vs.ValidationGroup = "gp1";
pnl.Controls.Add(vs);
}

and in my aspx page:

<div>
<asp:Panel ID="pnl" runat="server">
</asp:Panel>
<asp:Button ID="btnShow" runat="server" Text="Show" ValidationGroup="gp1" onclick="btnShow_Click" />
</div>

Comments or Responses

Login to post response