RequiredFieldValidator is actually simple example, You can use it to make sure that the user has entered
something in a TextBox control. Let's give it a try, and add a
RequiredFieldValidator in asp.net to our page. We will also add a TextBox to
validate, as well as a button to submit the form with.
Right now, the button does nothing, besides posting back if the page is valid. We will change this by adding an onclick event to it:
<form id="form1" runat="server">
Your name:<br />
<asp:TextBox runat="server" id="txtName" />
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="txtName" errormessage="Please enter your name!" />
<br />
<asp:Button runat="server" id="btnSubmitForm" text="Ok" />
</form>
Actually, that's all we need to test the most basic part of the RequiredFieldValidator.
I'm sure that all the attributes of the controls makes sense by now, so I won't go into details about them.
Try running the website and click the button. You should see something like this:
Right now, the button does nothing, besides posting back if the page is valid. We will change this by adding an onclick event to it:
<asp:Button runat="server" id="btnSubmitForm" text="Ok" onclick="btnSubmitForm_Click" />
In the CodeBehind file, we add the following event handler:
protected void btnSubmitForm_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
btnSubmitForm.Text = "Form is valid!";
}
}
No comments:
Post a Comment