Validation Group in Asp.net

Bangalore: If you want to validate first text box in your from by button 1 and second text box by button 2 the answer is Validation group, so you can categorize the form by validation group who is going to validate which fields as per your requirements.


Validation Post Click here


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GroupValidation.aspx.cs" Inherits="Validation.GroupValidation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <table>
    <tr>
    <td>First Button</td>
    <td>
    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>*
    </td>
    <td>
    <asp:RequiredFieldValidator id="fname" ValidationGroup="btn1" runat="server" ErrorMessage="Please Fill First Name" ControlToValidate="txtFirstName"></asp:RequiredFieldValidator>
    </td>
    </tr>

    <tr>
    <td>Second Button</td>
    <td>
    <asp:TextBox id="txtLastName" runat="server" ></asp:TextBox>*
    </td>
     <td>
    <asp:RequiredFieldValidator ValidationGroup="btn2" id="lname" runat="server" ErrorMessage="Please Fill Last Name" ControlToValidate="txtLastName"></asp:RequiredFieldValidator>
    </td>
    </tr>

    <tr>
    <td>
    <asp:Button runat="server" ID="btnlogin" ValidationGroup="btn1" Text="Register" />
    </td>

    <td>
    <asp:Button runat="server" ID="btnCancel" Text="Register 2" ValidationGroup="btn2" />
    </td>
    </tr>

    </table>
    </div>
    </form>
</body>
</html>


If you don't want to validate by second button then you can set the cause validation is false
<asp:Button runat="server" ID="btnCancel" Text="Register 2" CausesValidation="false" />

/* androidsharp */

0 comments: