Validation control on serverside using dropdown and textbox .asp c# code | YourSite

Validation control on serverside using dropdown and textbox .asp c# code

MS Dot NET • 1044 views

Server side validation in a dropdown list

file name: TravelDetailsInsert.aspx

In this section we are going to compare the two location should not be same. If the location will be same then it will five an error that Both locations are same"

In the below section AutoPostBack="true" is very much important

Also OnSelectedIndexChanged="ToLocation_SelectedIndexChanged1" this code is important to create the function inside the .aspx.cs file


<table>
<tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="From Location"></asp:Label> </td>
            <td>
                <asp:DropDownList ID="FromLocation" runat="server" AutoPostBack="true">
                    <asp:ListItem>Please Select</asp:ListItem>
                    <asp:ListItem>Thiruvananthapuram</asp:ListItem>
                    <asp:ListItem>Kochi</asp:ListItem>
                    <asp:ListItem>Kozhikode</asp:ListItem>
                    <asp:ListItem>Kollam</asp:ListItem>
                    <asp:ListItem>Thrissur</asp:ListItem>
                    <asp:ListItem>Kannur</asp:ListItem>
                    <asp:ListItem>Alappuzha</asp:ListItem>
                </asp:DropDownList> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Select the Base location" ControlToValidate="FromLocation"></asp:RequiredFieldValidator></td>
            
        </tr>
         <tr>
            <td> <asp:Label ID="Label3" runat="server" Text="To Location"></asp:Label> </td>
            <td> <asp:DropDownList ID="ToLocation" runat="server"  AutoPostBack="true"  OnSelectedIndexChanged="ToLocation_SelectedIndexChanged1">
                <asp:ListItem>Thiruvananthapuram</asp:ListItem>
                    <asp:ListItem>Kochi</asp:ListItem>
                    <asp:ListItem>Kozhikode</asp:ListItem>
                    <asp:ListItem>Kollam</asp:ListItem>
                    <asp:ListItem>Thrissur</asp:ListItem>
                    <asp:ListItem>Kannur</asp:ListItem>
                    <asp:ListItem>Alappuzha</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Select Your Destination Location" ControlToValidate="ToLocation"></asp:RequiredFieldValidator>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="TO and FROM is same" ControlToValidate="ToLocation"></asp:CustomValidator>
                <asp:Label ID="Label6" runat="server" Text="Label" ForeColor="#FF3399"></asp:Label>
            </td>
        </tr>
</table>

file name: TravelDetailsInsert.aspx.cs

This event will occur when anyone select the second dropdown list. this event is called SelectedIndexChanged


 protected void ToLocation_SelectedIndexChanged1(object sender, EventArgs e)
        {
            if (ToLocation.Text == FromLocation.Text)
            {
                Label6.Text = "Location Should not same";

            }
            else
            {
                Label6.Text = "";
            }
        }


Server side validation in a Text box

In this part we are going to learn a serverside validation using a awesome feature of the .net which is called as TextChanged

In this part we will take a value from the one textbox which is DistanceinKm and we will calculate the EstimatedAmount and we will put that value indide another text box

Note: the important part of the below code is onTextChanged="DistanceinKm_TextChanged" AutoPostBack="true"


<table>
 <tr>
            <td> <asp:Label ID="Label5" runat="server" Text="Distance in Km "></asp:Label>  </td>
            <td> <asp:TextBox ID="DistanceinKm" runat="server" onTextChanged="DistanceinKm_TextChanged" AutoPostBack="true"  ></asp:TextBox>
                <%-- <asp:Button ID="Button2" runat="server" Text="See Car Rent" OnClick="TextBox2_TextChanged"  />--%>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Select the distance" ControlToValidate="DistanceinKm"></asp:RequiredFieldValidator>
            </td>
        </tr>
         <tr>
            <td> <asp:Label ID="EstimatedAmountText" runat="server" Text="EstimatedAmount"></asp:Label> </td>
            <td>  <asp:TextBox ID="EstimatedAmount" runat="server"></asp:TextBox> </td>
        </tr>
</table>
<h3>

file name: TravelDetailsInsert.aspx.cs


 protected void DistanceinKm_TextChanged(object sender, EventArgs e)
        {
            int distance = Convert.ToInt32(DistanceinKm.Text);

            int rate = 10 * distance;
            EstimatedAmount.Text = Convert.ToString(rate);
        }


🚀 More Blogs You Might Like

Explore more articles and keep learning

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do
health
Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do...

👁 8 2026-04-26
Read More →
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It
health
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It

Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It...

👁 9 2026-04-26
Read More →
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It
health
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It

Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It...

👁 10 2026-04-26
Read More →
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science
class-1-12-resources
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science

The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science...

👁 47 2026-04-11
Read More →