This article will show you how you can validate the dropdown
list selected value and if user select the proper value then redirect and get the
selected value at server end using c#.net, jQuery in asp.net.
Some of my previous articles are as follows: Single
RadioButton Selection in GridView In Asp.Net Using C#.Net, Bind
and Validate GridView TextBox Value by jQuery In Asp.Net Using C#, Bind
and Validate GridView TextBox Value by RequiredFieldValidator In Asp.Net Using
C#, Asp.Net
Login Form With User Role Selection By Dropdownlist In Asp.net Using C#.Net,
Populate
Data in GridView on DropdownList Selected Role in Asp.net Using C#.net, Strong
Random String Password Generation Using C#.Net.
So for this article first we will create a new asp.net
application and add the below code into the .asp page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.WebForm4"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Validate
DropDownlist Selected Value Using jQuery In Asp.net ,C#.Net</title>
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
function
Validateddl() {
var
selectedValue = $("#<%=DropDownList1.ClientID %>").val();
if
(selectedValue == "0") {
alert("Please select some item.");
return
false;
}
return
true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Select : <asp:DropDownList ID="DropDownList1" runat="server" Width="158px">
<asp:ListItem Selected="True" Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">Item1</asp:ListItem>
<asp:ListItem Value="2">Item2</asp:ListItem>
<asp:ListItem Value="3">Item3</asp:ListItem>
<asp:ListItem Value="4">Item4</asp:ListItem>
</asp:DropDownList>
<br /> <br />
<asp:Button ID="Button1" runat="server" Text="Submit"
OnClientClick="javascript:return Validateddl();" onclick="Button1_Click"/><br />
<asp:Label ID="Label1" runat="server" style="color: #FF3300" Text=""></asp:Label>
</div>
</form>
</body>
</html>
|
In above code please check the jquery code
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
function
Validateddl() {
var
selectedValue = $("#<%=DropDownList1.ClientID %>").val();
if
(selectedValue == "0") {
alert("Please select some item.");
return
false;
}
return
true;
}
</script>
|
In above code first I have retrieve
the selected value and after that I have check the selected value is allowed to
access or not for postback. If it is allowed on that case we will we will return
true and postback will take place.
0 comments:
Please let me know your view