This Simple asp.NET program will demonstrate you to use the RequiredFieldValidator. The use of require field validation is very simple and it to validate values it does not require a server trip. So all validation is perform on Client side.
Just specify the ControlToValidate property and give the id of control which need to validated. We have given example with radio button and text box , you can use it for other controls also.
<html>
<head>
<script language="C#" runat=server>
void abc(Object S, EventArgs e)
{
if (Page.IsValid == true)
{
aa.Text = "Page is Valid!";
}
else
{
aa.Text = "Some of the required fields are empty";
}
}
</script>
</head>
<body>
<form id="Form1" runat="server">
<asp:Label ID="aa" text="fill it up" runat=server />
<ASP:RadioButtonList id=bb runat=server>
<asp:ListItem>a1</asp:ListItem>
<asp:ListItem>a2</asp:ListItem>
</ASP:RadioButtonList>
<asp:RequiredFieldValidator id="r1" ControlToValidate="bb" InitialValue="" runat=server>
Radio Button Selection is missing
</asp:RequiredFieldValidator>
<br />
<br />
<br />
<ASP:TextBox id=cc runat=server />
<asp:RequiredFieldValidator id="r2" ControlToValidate="cc" Width="100%" runat=server>
Text Field Values are Missing
</asp:RequiredFieldValidator>
<ASP:Button id=B1 text="Validate" OnClick="abc" runat=server />
</form>
</body>
</html>
0 comments:
Post a Comment