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

Rumman Ansari   2019-02-28   Student   MS dot NET > server-side-validation   799 Share

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);
        }