Tuesday, September 20, 2011

use Validator and ValidationSummary in ASP.Net Validation

Today I implemented a form for user submit since i need to ensure all the required fields are filled by the user

I use RequiredFieldValidator to control the Mandatory fields validation.


the code works perfectly fine, all the erorr message displayed after user click 
submit button.
 
after i added another panel with another submission sesseion. 
no matter which button is trigger
all the error messages are shown. there is a ValidationGroup Property
in control which is availble for .net 3.5 and later. when the button is clicked
by the user, only the specific Validation Group that specified in the Button
validationGroup Property will be triggered and validated. All the Error Message 
will only shown  

here is the sample code:

    <asp:ValidationSummary ID="ValidationSummaryControl" runat="server"   
HeaderText="Validation Errors:" ValidationGroup="RateSubmit" /> 
 
<asp:ValidationSummary ID="ValidationSummary1" runat="server"   
HeaderText="Validation Errors:" ValidationGroup="ExpenseSubmit" /> 
 
      
<td class="style2">
<asp:Label runat="server" ID="lblDateFrom" Text="Date From: " Font-Bold="true">
</asp:Label></td>
<td class="style3">
<asp:TextBox runat="server" ID="txtDateFrom"></asp:TextBox>
 <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator3" Display="None" 
ErrorMessage="Please fill Expense Date From" ControlToValidate="txtDateFrom" 
ValidationGroup="ExpenseSubmit"  ></asp:RequiredFieldValidator>
 
<asp:Button runat="server" ID="btnExpenseSubmit"  Text="Submit" causesvalidation="true"  
onclick="btnExpenseSubmit_Click" ValidationGroup="ExpenseSubmit" /></td>
 
    <td class="style2">
<asp:Label runat="server" ID="Label3" Text="Subaru Milage Rate: " Font-Bold="true"></asp:Label></td>
<td class="style3">
 <asp:TextBox runat="server" ID="txtMilagRate" ValidationGroup="RateSubmit"></asp:TextBox> 
 <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator10" Display="None"  
ErrorMessage="Please fill Milage Rate Field" ControlToValidate="txtMilagRate" 
ValidationGroup="RateSubmit"></asp:RequiredFieldValidator>
                    </td>
           </tr>
           
                <td class="style5" align="right">
                    <asp:Button runat="server" ID="btnRateSubmit"  
Text="Submit" onclick="btnRateSubmit_Click" 
ValidationGroup="RateSubmit" CausesValidation="true" /></td>





No comments:

Post a Comment