How to check expression on button click in asp.net c #

I am creating a website and I have created form fields such as the email field and validation expressions associated with it. Validation begins with a change in text. But I want it to be executed when the submit button is clicked. I searched, but could not find a solution to my problem. Please tell me why this is happening and how I can fix it. I am new to this area of ​​web development, so I need help from you guys.

Thanks in advance!

Hamad

+3
source share
6 answers

You can disable the display of errors in the validator itself and instead make a summary of the check, which will be shown only after clicking the submit button.

Like this:

<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ValidationGroup="vRegister" Display="None" ErrorMessage="Email field cannot be empty"></asp:RequiredFieldValidator>

and then announce the check summary:

<asp:ValidationSummary runat="server" ID="vSummary" ValidationGroup="vRegister" DisplayMode="BulletList" />
+6

, EnableClientScript false. ( , ). 1 )

EnableClientScript: , , .

 <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
 <asp:RequiredFieldValidator ID="rfvName" runat="server" EnableClientScript="false"
 ErrorMessage="*" ControlToValidate="txtName" ></asp:RequiredFieldValidator>

:

   protected void btnSave_Click(object sender, EventArgs e)
   {
          if (Page.IsValid)
             {
               //Do stuff
             }
             //No need for else, the validations should display accordingly
   }

: http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx

+2

? , EnableValidation true. , button_Click .

+1

:

 <asp:TextBox ID="txtName" runat="server" Height="23px" 
 Width="252px"></asp:TextBox>
 <asp:RequiredFieldValidator ID="rfvName" runat="server" 
 ErrorMessage="*" ControlToValidate="txtName" 
 ValidationGroup="vadd"></asp:RequiredFieldValidator>
+1

The "Causes Validation" property on the button itself will automatically make your page meet your verification requirements before running the rest of the code associated with the button click.

0
source

If I understand your question, you need to create the same validation group for each validation control on the aspx page. You must also have a validation summary with the same validation group. And by clicking the submit button on the aspx page, you must specify the same validation group ...

0
source

All Articles