Skip to main content

How to use ValidationSummary control in asp.net

ValidationSummary web server control
ValidationSummary control allow us to display summary of all validation errors. We can display validation errors summary inline of a web page or a message box or both by using ShowMessageBox and ShowSummary property value true or false. We can display validation messages as bulleted list, single paragraph or only list based on DisplayMode.

We can set a header text for validation summary. ASP.NET ValidationSummary control have many properties to design the error messages text as like fore color, back color, border color, border style, border width, theme, skin and after all CSS class.

ValidationSummary allow to summarize of all validation error messages from all validators in a single location. This example show you how can we display all validation error messages as summary using ValidationSummary control.

Here we uses three text box and make them required field using requiredfieldvalidator control. When someone submit form without entering TextBoxes value, he got a validation error messages summary in one location as bulleted list.
ValidationSummaryExample.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button_Submit_Click(object sender, System.EventArgs e)
    {
        Label_Message.Text = "Your submitted info....<br />" +
            "Name : " +
            TextBox_FirstName.Text.ToString() + " " +
            TextBox_LastName.Text.ToString() +
            "<br />City: " +
            TextBox_City.Text.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use ValidationSummary control in asp.net</title>
</head>
<body style="padding:25px">
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">      
            How to use ValidationSummary control
        </h2>      
        <hr width="450" align="left" color="Gainsboro" />
        <asp:Label
            ID="Label_Message"
            runat="server"
            Font-Bold="true"
            Font-Names="Comic Sans MS"
            ForeColor="OliveDrab"
            Font-Italic="true"
            Font-Size="X-Large"
            />
        <asp:ValidationSummary 
            ID="ValidationSummary1" 
            runat="server" 
            HeaderText="Following error occurs....." 
            ShowMessageBox="false" 
            DisplayMode="BulletList" 
            ShowSummary="true"
            BackColor="Snow"
            Width="450"
            ForeColor="Red"
            Font-Size="X-Large"
            Font-Italic="true"
            />
        <br /><br />
        <asp:Label 
            ID="Label_FirstName" 
            runat="server" 
            Text="First name" 
            AssociatedControlID="TextBox_FirstName"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="Navy"
            />
        <asp:TextBox 
            ID="TextBox_FirstName" 
            runat="server"
            Font-Bold="true"
            Font-Size="Large"
            Height="30"
            BackColor="Gold"
            Font-Names="Courier New"
            />
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator1"
             runat="server"
             ControlToValidate="TextBox_FirstName"
             ErrorMessage='Input your first name.'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br /><br />

        <asp:Label 
            ID="Label_LastName" 
            runat="server" 
            Text="Last name" 
            AssociatedControlID="TextBox_LastName"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="Navy"
            />
        <asp:TextBox 
            ID="TextBox_LastName" 
            runat="server"
            Font-Bold="true"
            Font-Size="Large"
            Height="30"
            BackColor="Gold"
            Font-Names="Courier New"
            />
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator2"
             runat="server"
             ControlToValidate="TextBox_LastName"
             ErrorMessage='Input your last name.'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br /><br />
        
        <asp:Label 
            ID="Label_City" 
            runat="server" 
            Text="City" 
            AssociatedControlID="TextBox_City"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="Navy"
            />
        <asp:TextBox 
            ID="TextBox_City" 
            runat="server"
            Font-Bold="true"
            Font-Size="Large"
            Height="30"
            BackColor="Gold"
            Font-Names="Courier New"
            />
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator3"
             runat="server"
             ControlToValidate="TextBox_City"
             ErrorMessage='Input your city.'
             EnableClientScript="true"
             SetFocusOnError="true"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br /><br />
        <asp:Button 
            ID="Button_Submit" 
            runat="server" 
            Text="Submit" 
            OnClick="Button_Submit_Click"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="DodgerBlue"
            Font-Names="Monaco"
            Height="45"
            Width="350"
            />
        <br /><br />
    </div>
    </form>
</body>
</html>
More asp.net examples

Popular posts from this blog

How to use Table in asp.net

Table Web Server Control ASP.NET Table web server control display a table on a web page. It is a .NET web server control so we can program it in server code such as add, remove table row, cell programmatically. TableRow and TableCell web server controls allow us to display content for the Table. Table server control display tabular data and we can format Table server control using it's built in methods and properties. The big difference between HTML table and asp.net Table server control is that we can manipulate Table server control using an object model. Generally we should use Table server control when we intend to add remove rows and cells (columns) programmatically at run time.

Table server control acts as a parent (container) for TableRows controls. Table control's Rows property is a collection of TableRow objects. each TableRow control has a collection named Cells. Cells collection contains TableCell objects.

Actually table server control display the TableCell con…

How to use RadioButton control in asp.net c#

RadioButton web server control RadioButton control lets you make a group of radio buttons with other RadioButton. If you set it's GroupName property same for multiple radio buttons then all radio buttons with same name act as a single group. Within a group you can only select one RadioButton at a time. RadioButton group work as like RadioButtonList. With RadioButton control you can more customize it than RadioButtonList items.

Back color, border color, border style, font names, font bold, font italic, fore color (text color) and many more property help you to design RadioButton. It also support CSS class, theme and skin. Even validation are works with RadioButton control. RadioButton have AutoPostBack property and CheckChanged event. By using those feature you can determine immediately which RadioButton is selected from a group.

This example uses a RadioButton group with two RadioButton. Here we assign AutoPostBack property value to true and setup a CheckChanged event. When …

How to use RadioButtonList control in asp.net c#

RadioButtonList web server control ASP.NET RadioButtonList control enable user to select an item from list. RadioButtonList support data bind programmatically from database. We can also populate it manually by input list item inside RadioButtonList tag. RadioButtonList is a single selection radio button group. RadioButtonList have an items collection. We can determine which item is selected by test it's SelectedItem property.

We can change RadioButtonList design by setting up it's various property as like BackColor, BorderColor, BorderStyle, BorderWidth, CellPadding, CellSpacing, CssClass, Font-Bold, Font-Italic, Font-Names, Font-Overline, Font-Size etc. there are more three properties RepeatColumns, RepeatDirection and RepeatLayout which help you to place list item vertically or horizontally.

RadioButtonList have standard AutoPostBack property. As like other list control RadioButtonList have an excellent event SelectedIndexChanged. So that when someone select an item it…