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 1053 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

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →