Monday 6 February 2012

What is the ValidationSummary in Asp.net?

What is the ValidationSummary?The validation summary is an ASP.NET control that allows you to easily display validation error messages. It works by taking all of the validation error messages from an invalid page and displays them in a single location. The validation summary is used on pages that contain validators to simplify and organize error messages for the user. 
Using the ValidationSummaryTo demonstrate the use of a validation summary, we will need to create a simple web page with a few validators on it. To do this, create a new ASP.NET Empty Web Site and:
  1. Right click the project in your solution explorer.
  2. Select add new item...
  3. Select a web form.
  4. Name it 'Default.aspx'.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a textbox onto the web form.
  8. Drag and drop a requiredfieldvalidator onto the web form.
  9. Change the ErrorMessage property to 'TextBox1 is required.'
  10. Set the ControlToValidate property to 'TextBox1'.
  11. Set the Text property to '*'.
  12. Add a break line.
  13. Drag and drop a textbox onto the web form.
  14. Drag and drop a requiredfieldvalidator onto the web form.
  15. Change the ErrorMessage property to 'TextBox2 is required.'
  16. Set the ControlToValidate property to 'TextBox2'.
  17. Set the Text property to '*'.
  18. Add a break line.
  19. Drag and drop a button onto the web form.
  20. Add a break line.
  21. Drag and drop a validationsummary onto the web form.
This has created a simple form with two text boxes that are validated as required fields and a button to cause validation. By default, the validation summary will pick up on the error messages that need to be displayed on the page, without us having to specify which validator's error messages to display. However, you can pick and choose what validator's you want to be applied to the validation summary by making them all a part of a validation group. To do this, you would need to change theValidationGroup property on each of the controls you wanted to associate with each other. Furthermore, you can also choose a display mode to format the display of the validation summary. The validation summary offers three different display modes, BulletListList, andSingleParagraph. Also, the validation summary allows you to choose whether you want to display the errors on the web page or a message box via the ShowSummary and ShowMessageBoxproperties. 


Code---


<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        Label1.Text = "Your Country: " +
            TextBox1.Text.ToString() +
            "<br />Region: " +
            TextBox2.Text.ToString() +
            "<br />City: " +
            TextBox3.Text.ToString();
            
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ValidationSummary example: how to use ValidationSummary control in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="BlueViolet"></asp:Label>
        <br /><br />
        <asp:Label ID="Label2" runat="server" Text="Country Name" AssociatedControlID="TextBox1"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator1"
             runat="server"
             ControlToValidate="TextBox1"
             ErrorMessage='Input Country!'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br />

        <asp:Label ID="Label3" runat="server" Text="Region Name" AssociatedControlID="TextBox2"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator2"
             runat="server"
             ControlToValidate="TextBox2"
             ErrorMessage='Input Region!'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br />
        
        <asp:Label ID="Label4" runat="server" Text="City Name" AssociatedControlID="TextBox3"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator3"
             runat="server"
             ControlToValidate="TextBox3"
             ErrorMessage='Input Region!'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
        <br /><br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Following error occurs:" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" />
    </div>
    </form>
</body>
</html>



 


No comments:

Post a Comment