ASP.NET Code


Understanding ASP.NET Validation Controls
Validation Controls:
A Validation server control is used to validate the data of an input control; So that check withers input is correct or proper, if input is not correct or proper than prompts message display to client.  
The Six types of ASP.NET Validation Controls in ASP.NET Framework include:



1) Required Field Validator
2) RangeValidator
3) Regular Expression Validator
4) Compare Validator
5) Custom Validator
6)ValidationSummary
General properties:
  • ControlToValidate - This value is which control the validator is applied to(id of Control).
  • ErrorMessage  this property is used to display the Error Messsage.




General properties:
       ·         ControlToValidate="txtfname"
       ·         ErrorMessage="RequiredFieldValidator"     
For Example:-

Source Code like this

First Name:<asp:TextBox ID="txtfname" runat="server" ValidationGroup="name_group">
           </asp:TextBox>
&nbsp<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtfname" ErrorMessage="You must enter your First Name">*</asp:RequiredFieldValidator>



2)   : if the data entered in the input field is not within the range of the values specified by the Maximum and Minimum properties of the validation control.
General properties:
      ·         ControlToValidate="txtage"
      ·         ErrorMessage="You must be between 25 and 35"
      ·         MaximumValue="35"
      ·         MinimumValue="25"
      ·         Type=”Integer/Double/Date/Currency ">


Source Code like this

Age:<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
&nbsp;
<asp:RangeValidator ID="RangeValidator1" runat="server"
      ControlToValidate="txtage" ErrorMessage="You must be between 25 and 35"
      MaximumValue="35" MinimumValue="25" Type="Integer">
</asp:RangeValidator>

                        OR

Last Name:<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
&nbsp;
<asp:RangeValidator ID="RangeValidator3" runat="server"
   ControlToValidate="txtage" ErrorMessage="You must be between A and D"
 MaximumValue="D" MinimumValue="A" Type="String">
</asp:RangeValidator>

Similar for Double,Date,Currency

Note:-Type:- there are 5 type as following (Interview Question)




3)    

: You can check the validation of the commonly performed formats such as e-mail addresses, telephone numbers, social security numbers, and postal code for different countries.
General properties:
      ·         ControlToValidate=" txtemail 
      ·         ErrorMessage=" You must enter an email address (abc@google.com)" or    ErrorMessage='<img src="Image/error.gif" />'
      ·         ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">


Source Code like this

Email: <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
&nbsp;
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtemail"
 ErrorMessage=" You must enter an email address (abc@google.com)"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
                      Or
Also display error.gif image instade of message(You must enter an email address (abc@google.com))
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtalteremail"
                        ErrorMessage='<img src="Image/error.gif" />'
                        ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>



4)      

: Compares the value of one input control to the value of another input control or to a fixed value, such as equality, less-than, greater-than, NotEqual ,GreaterThan , GreaterThanEqual , LessThan ,LessThanEqual ,DataTypeCheck(these are operation properties )
General properties:
      ·         ControlToValidate="txtconfirmpass(TextBox2)"
      ·         ControlToCompare="txtpass(TextBox1)"
      ·         ErrorMessage="Passwords do not match!!!"

(Interview Question) what are the operator in comparison validation









Source Code like this

Password:<asp:TextBox ID="txtpass" runat="server" TextMode="Password"></asp:TextBox>

<asp:CompareValidator ID="CompareValidator1" runat="server"  ErrorMessage="Passwords do not match!!!" ControlToCompare="txtpass" ControlToValidate="txtconfirmpass">
</asp:CompareValidator>

<asp:RequiredFieldValidator
ID="RequiredFieldValidator7" runat="server"
  ControlToValidate="txtpass"
ErrorMessage="RequiredFieldValidator_Password">
</asp:RequiredFieldValidator>








Allows you to define your own condition for validating the data in the input fields.
Two validation functions can be performed using Custom Validation control:-
1)    The server-side and
2)    The client-side. Custom control.
These functions contain logic defined by you to validate the input fields.
These functions returns the True value and False value, if the condition you specified is correct or not respectively.
For Example:-
1)       At times, you may want to compare the user's input to a value in a database.
2)       To determine whether his input conforms to some arithmetic validation that you are looking for (if the number is even or odd). You can do all this and more by using this type of validation control.
Creating your own client-side validation functions
General properties:
      ·         The important part of the Custom Validator server control here is the ClientValidationFunction property= (write javascript function name). 
      ·         ErrorMessage="Number must be divisible by 9"
Step-I
Create CustomValidationControl.aspx
 




Set Properties as following


Step-II

Source Code like this

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Custom Validation Control</title>

 <script language="JavaScript"   >
        function validateNumber(oSrc, args)
        {
           args.IsValid = (args.Value % 9 == 0);
        }
    </script>
</head><body>

    <form id="form1"   runat="server">
    <p>
            Number:
            <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
             &nbsp;
            <asp:CustomValidator id="CustomValidator2" runat="server"
             ControlToValidate="TextBox2"
             ErrorMessage="Number must be divisible by 9"
             ClientValidationFunction="validateNumber">
            </asp:CustomValidator>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click"
             runat="server" Text="Button"></asp:Button>
        </p><p>
            <asp:Label id="Label11" runat="server"></asp:Label>
        </p></form><br />
</body></html>
Step-III

 --àRun(F5) OR --à

Creating your own Server-side validation functions
General properties:
      ·         ControlToValidate="txtage"
     ·         ErrorMessage="You must be between 25 and 35"
Performing custom server-side validation with the CustomValidator server controls requires that you use theOnServerValidate property instead of the ClientValidationFunction property. The ClientValidationFunction is used with the CustomValidator server control when working with client-side validation.






     

 error messages can be displayed in a list, bulleted list, or paragraph.
General properties:
·         HeaderText="You received the following errors:"
DisplayMode property :-  you can change the value of the DisplayMode property of the Validation Summary control. The possible values of this control can be set to the following:
  • BulletList(By Default)(Interview Question)
  • List
  • SingleParagraph

A dialog box showing the page's validation errors
<asp:ValidationSummary ID="ValidationSummary1"
runat="server"  HeaderText="You received the following errors:"
ShowMessageBox=true(You must type in source code than & than display dialog box) />

Using different text for the validation error messages


<asp:ValidationSummary ID="ValidationSummary1"
runat="server"  HeaderText="You received the following errors:" />

Source Code like this

<asp:ValidationSummary ID="ValidationSummary1"
runat="server"  HeaderText="You received the following errors:" />



Step-I
Create ValidationControlExamples.aspx as following

Step-II

Source Code like this

Take table 20*3

Then take all control

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 63%;
            background-color: #C0C0C0;
            height: 619px;
        }
        .style2
        {
            width: 121px;
        }
        .style3
        {
            width: 80px;
        }
        .style4
        {
            width: 324px;
        }
        .style5
        {
            width: 121px;
            height: 21px;
        }
        .style6
        {
            width: 80px;
            height: 21px;
        }
        .style7
        {
            width: 324px;
            height: 21px;
        }
        </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    FirstName</td>
                <td class="style3">
                    <asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
                </td>
                <td class="style4">
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
          ControlToValidate="txtfname" ErrorMessage="You must enter your First Name">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    </td>
                <td class="style3">
                    </td>
                <td class="style4">
                    </td>
            </tr>
            <tr>
                <td class="style5">
                    Last name:
  &nbsp;
&nbsp;</td>
                <td class="style6">
<asp:TextBox id="txtlname" runat="server"></asp:TextBox>
                </td>
                <td class="style7">
<asp:RangeValidator id="RangeValidator3" runat="server"
  ControlToValidate="txtlname"
  ErrorMessage="Your last name needs to be between A and D"
  MaximumValue="D" MinimumValue="A"></asp:RangeValidator>
                </td> </tr> <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
 <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
      ControlToValidate="txtlname" ErrorMessage="RequiredFieldValidator_Lname">
</asp:RequiredFieldValidator>
                </td></tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    &nbsp;</td>
            </tr> <tr>
                <td class="style2">
                    Age:
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtage" runat="server"></asp:TextBox>
                </td>
                <td class="style4">
 <asp:RangeValidator ID="RangeValidator1" runat="server"
  ControlToValidate="txtage" ErrorMessage="You must be between 25 and 35"
   MaximumValue="35" MinimumValue="25" Type="Integer">
</asp:RangeValidator>
                </td></tr> <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
      ControlToValidate="txtage" ErrorMessage="RequiredFieldValidator_Age">
</asp:RequiredFieldValidator>
                </td></tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    Email:</td>
                <td class="style3">
                    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                </td>
                <td class="style4">
 <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtemail"                       ErrorMessage="You must enter an email address (abc@google.com)"
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
                </td> </tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
     ControlToValidate="txtemail" rrorMessage="RequiredFieldValidator_Email">
</asp:RequiredFieldValidator>
                </td> </tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    Alternative
                    Email:</td>
                <td class="style3">
     <asp:TextBox ID="txtalteremail" runat="server"></asp:TextBox>
                </td>
                <td class="style4">
 <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtalteremail"
ErrorMessage='<img src="Image/error.gif" />'
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
                    <br />
  <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
    ControlToValidate="txtalteremail"                      ErrorMessage="RequiredFieldValidator_AlternativeEmail">
</asp:RequiredFieldValidator>
                </td></tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td class="style3">
  <asp:TextBox ID="txtpass" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <td class="style4">
  <asp:CompareValidator ID="CompareValidator1" runat="server"
       ErrorMessage="Passwords do not match!!!" ControlToCompare="txtpass"
                        ControlToValidate="txtconfirmpass"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
                        ControlToValidate="txtpass" ErrorMessage="RequiredFieldValidator_Password"></asp:RequiredFieldValidator>
                </td></tr><tr>
                <td class="style2">
                     Confirm Password :   <td class="style3">
                    <asp:TextBox ID="txtconfirmpass" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <td class="style4">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    <asp:Button ID="Button1" runat="server" Text="Button" Width="121px" />
                </td>
                <td class="style4">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server"
                        HeaderText="You received the following errors:" ShowMessageBox=true />
                    </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td class="style4">
                    </td></tr>
</table></div></form></body></html>
Step-III
--àRun(F5) OR --à





How to clear all controls values in web page?

Using Clear Method to clear all control value, let’s start

For an example we are create Employee Registration page in our project in that time we placed lots of controls like textbox, radio button ,drop down list &  check box etc. In this Example I took two Concept(way),
First Way: - If user click that clear button we need to clear all controls values, with using C Sharp.
Second Way:-If user click that clear button we need to clear all controls values, with using Java script.

Client side code
I have places some controls in client side of the web page (is called ClearAllMethod.aspx) and shown in below figure.

Way-I(USING CSHARP)

Using Clear Method to clear all control value, with using C Sharp.
Server side
using System;
using System.Data;

public partial class ExampleOfInsertDisplay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnresetc_Click(object sender, EventArgs e)
    {
        //clear method i call here
        ClearAll();
    
    }
    //this is a clear method to clear all control
public void ClearAll()
    {
        //First WAY -I

        //TextBox1.Text = "";
        //RadioButton1.Checked = false;
        //RadioButton2.Checked = false;
        //TextBox2.Text = "";
        //TextBox3.Text = "";
        //DropDownList1.SelectedIndex = 0;
        //CheckBox1.Checked = false;
        //CheckBox2.Checked = false;
        //CheckBox3.Checked = false;
        //CheckBox4.Checked = false;


        //Second WAY-II

        foreach (Control item in form1.Controls)
        {
            //Clear all textbox values
            if (item is TextBox)
                ((TextBox)item).Text = "";

            //Clear all radio buttons
            if (item is RadioButton)
                ((RadioButton)item).Checked = false;         

            //clear all check boxes
            if (item is CheckBox)
                ((CheckBox)item).Checked = false;

            //Clear all radio buttons
            if (item is DropDownList)
                ((DropDownList)item).SelectedIndex = 0;
      }
   }
}
OUTPUT:
Buil---àRun(F5)---àCHECK OUTPUT
When you click Reset Using C# btn all control value is clear.

Way-II(USING JavaScript)

Using Clear Method to clear all control value, with using JavaScript.
I have write one JavaScript function to get form elements and check that element type and assign values based on that type.

Client side
<head runat="server">
    <title>Untitled Page</title>   
    <script type="text/javascript" language="javascript" >

    function clearAllControlValue()
 {       
        var myfrm = document.forms[0];
        for (i = 0; i < myfrm.elements.length; i++)
        {               
            if (myfrm.elements[i].type == "checkbox")
            {
                myfrm.elements[i].checked = false;
            }
            if (myfrm.elements[i].type == "radio")
            {
                myfrm.elements[i].checked = false;
            }
            if (myfrm.elements[i].type == "text")
            {
                myfrm.elements[i].value="";
            }
            if (myfrm.elements[i].type == "select")
            {              
                myfrm.elements[i].value=0;
            }
        }
    }
</script>

</head>

And then call in the button click event(mean OnClientClick) like this
<asp:Button ID="btnresetjava" runat="server"
     OnClientClick="clearAllControlValue()"
     Text="Reset Using Javascript"/>
OUTPUT:
Buil---àRun(F5)---àCHECK OUTPUT
When you click Reset Using JavaScript btn all control value is clear.





How to use Label? Properties For Label?
What is mean by AssociatedControlID?
AssociatedControlID, it tells label where focus should go once the label is clicked.
 See as following example


Select Lable(lblname)--àProperties--à1)Text=Enter First Name 2)AssociatedControlID=txtfname (NAME OF TextBox)

Similar for Lable(lbllastname)--àProperties--à1)Text=Enter Last Name 2)AssociatedControlID=txtlname (NAME OF TextBox)
Then
Build--àRun(F5) 







How to use Panel Control?

Step-I

Create a new aspx page(ExampleOf2Panel.aspx).
Then  add two Panel  & just take one button in Panel1 as shown in below figure



Step-II

If I click submit button ,then I display Panel2 , so that I want to set Panel2 --àProperties --àVisible="False" .
                           OR
At the time of page Load you have to set Panel2.Visible=False see below cs code
protected void Page_Load(object sender, EventArgs e)
    {
        Panel2.Visible = false;
    }
WHEN I click submit button ,then Panel2 will be display .

Then write code on Submit Button
protected void Button1_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
     
    }

OutPut:-
Build----àRun(F5)



Tabs


Tab 1 content goes here

Tab 2 content goes here

Tab 3 content goes here
Multi-Tabbed Widget | DotNetIs