<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.IsPostBack)
{
this.Validate();
foreach (object v in this.Validators)
{
if (v is RequiredFieldValidator)
{
RequiredFieldValidator r = v as RequiredFieldValidator;
TextBox t = this.FindField(this.Page.Form, r.ControlToValidate) as TextBox;
if (t != null)
{
if (r.IsValid)
t.BackColor = System.Drawing.Color.Green;
else
t.BackColor = System.Drawing.Color.Red;
}
}
}
}
}
private Control FindField(Control source, string id)
{
Control target = source.FindControl(id);
if (target != null) return target;
foreach (Control child in source.Controls)
{
target = this.FindField(child, id);
if (target != null) return target;
}
return null;
}
protected void Save(object sender, EventArgs e)
{
}
</script>
</head>
<body>
<form id="form1" runat="server">
Name: <asp:TextBox runat="server" ID="txtName" />
<asp:RequiredFieldValidator runat="server" ID="rvalName" ControlToValidate="txtName"
ErrorMessage="Name is required" EnableClientScript="false" /><br />
Address: <asp:TextBox runat="server" ID="txtAddress" />
<asp:RequiredFieldValidator runat="server" ID="rvalAddress" ControlToValidate="txtAddress"
ErrorMessage="Address is required" EnableClientScript="false" /><br />
Phone: <asp:TextBox runat="server" ID="txtPhone" />
<asp:RequiredFieldValidator runat="server" ID="rvalPhone" ControlToValidate="txtPhone"
ErrorMessage="Phone is required" EnableClientScript="false" /><br />
<hr />
<asp:Button runat="server" ID="btnSave" OnClick="Save" Text="Save" />
</form>
</body>
</html
No comments:
Post a Comment