简体   繁体   中英

server side validation in asp.net?

hi all as i know that asp.net provide server side validation control which has there own built-in logic, so i want to ask here can we extend built-in logic, i means suppose we use compare validator for comparing two file at this point it will validate according to there built-in logic, but i want to add some code for compare validator, is this possible.

According to my knowledge in asp.net every control treated as class which has data and code so according to inheritance or extend can we add some code in validation control ?

It looks like you need to use CustomValidator

You can use a custom function to define when your control passes your validation. In this case could be something like this.

void ServerValidation (object source, ServerValidateEventArgs args)
{
    args.IsValid = //Define your validation here
}

CustomValidator is one option. If you rather going to implement validator similar to existing one, you can simply derive from it and override all necessary methods. But the most important method you should look for is the EvaluateIsValid .

A CustomValidator is better in situations when your logic is more likely unique. In case you want to use the logic in multiple places, I would recommend to use inheritance. It allows you to encapsulate the logic in class library if you want, CustomValidator doesn't.

In your markup:

<asp:TextBox id="Text1" runat="server" />

<asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Names="verdana" 
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

<asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

In the server side code:

void ValidateBtn_OnClick(object sender, EventArgs e) 
      { 
         // Display whether the page passed validation.
         if (Page.IsValid) 
         {
            Message.Text = "Page is valid.";
         }

         else 
         {
            Message.Text = "Page is not valid!";
         }
      }

      void ServerValidation(object source, ServerValidateEventArgs args)
      {
         try 
         {
            // Test whether the value entered into the text box is even.
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);
         }

         catch(Exception ex)
         {
            args.IsValid = false;
         }
      }

This example is a shortened version of the one found at the documentation page for CustomValidator:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx

Yes you can. Like Carlos's answer link will tell you. You can put the code in the code behind.(.cs file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM