- RequiredFieldValidator is one of top Asp.net control and it is very simple and very use full.
- we can use as the user has entered something in the TextBox or not.
| <form id="form1" runat="server"> <div> <br /> Username: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"> </asp:RequiredFieldValidator> <br /> <br /> </div> <div style="margin-left: 80px"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="CHECK REQUIRE FIELD VALIDATION" /> <br /> </div> </form> |
- Now Run the above program you see the form like as below image.
- if your browser supports the JavaScript,which most modern browsers do,So you can notice that the webpage isn't being posted back to server but the validation happens in client side!!!.
- This is the one of the good thing about validators of asp.net validation is performed in client side.
- If we use the attribute enableclientscript="false" to RequiredFieldValidator then the validator performed at server side
- If necessary you want to perform the validation in server side then use the attribute enableclientscript enable as false then you can see the browser posting back to server, but the result will be the same(still the validator works very well)
if the page is valid then we will change this by adding an onclick event to the button control
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="CHECK REQUIRE FIELD VALIDATION" />
- In code behind file, we add the below code and run the page
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { if (Page.IsValid) { Button1.Text = "form submitted succefully"; } } } |
No comments:
Post a Comment