Design the Form as Follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function(){
var countryId = $('#DropDownList1').find('option:selected').attr("Country");
alert(countryId);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
.cs Page:
private DataRow[] _dataRow;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("................");
SqlDataAdapter da = new SqlDataAdapter("select * from Employee2", con);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = "FirstName";
DropDownList1.DataValueField = "EmpID";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
foreach (ListItem _listItem in DropDownList1.Items)
{
_dataRow = ds.Tables[0].Select("EmpID=" + _listItem.Value);
if (_dataRow.Count() > 0)
{
_listItem.Attributes["Country"] = _dataRow[0]["Country"].ToString();
}
}
DropDownList1.Items.Insert(0, new ListItem("select", "0"));
}
}
output:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function(){
var countryId = $('#DropDownList1').find('option:selected').attr("Country");
alert(countryId);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
.cs Page:
private DataRow[] _dataRow;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("................");
SqlDataAdapter da = new SqlDataAdapter("select * from Employee2", con);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = "FirstName";
DropDownList1.DataValueField = "EmpID";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
foreach (ListItem _listItem in DropDownList1.Items)
{
_dataRow = ds.Tables[0].Select("EmpID=" + _listItem.Value);
if (_dataRow.Count() > 0)
{
_listItem.Attributes["Country"] = _dataRow[0]["Country"].ToString();
}
}
DropDownList1.Items.Insert(0, new ListItem("select", "0"));
}
}
output:
No comments:
Post a Comment
Thank you for visiting my blog