I have a dropdown list that takes it's values (country name) from a database. But what I would like to be able to do is to set the first item in the dropdown list manully.
· Add DropDownList in your .aspx page
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem value="Select Country">---Select Country---</asp:ListItem>
</asp:DropDownList>
· Now set the AppendDataBoundItems Property of dropdownlist to true
<asp:dropdownlist id="DropDownList1" runat="server" appenddatabounditems="True">
<asp:listitem value="Select Country">---Select Country--</asp:ListItem>
</asp:DropDownList>
· Bind the dropdownlist with datasource (datareader,itemlist,arrayliat etc.) as follow
. use namespace
System.Data.SqlClient;
· Create a method and call it in pageload
protected void countrybind()
{
SqlConnection con = new SqlConnection("server=.;database=dbcont;uid=sa;");
con.Open();
System.Data.SqlClient.SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select cntcod,cntnam from tbcnt";
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "cntnam";
DropDownList1.DataValueField = "cntcod";
DropDownList1.DataBind();
}
Now write following code in drop down list’s selected index event
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0)
{
//your code here
}
}
Vist here for how to bind years to dropdownlist
Hot to Show Years in dropDownList
Try it!
Add Default Item in DropDownList
Category:
DropDownList
Subscribe to:
Post Comments (Atom)
Comments :
Post a Comment