Reputation: 751
I have a dropdownlist control of asp.net.I want to track the item selected in the dropdownlist. Let me show what i have done.
This is DropdownList control:
<asp:DropDownList ID="Dd_Cat" runat="server"
DataValueField="CatId" DataTextField="CatName"
OnSelectedIndexChanged="wtf" >
</asp:DropDownList>
This is the code behind used to populate the dropdownlist
from database.
protected void bindDropdown()
{
string mystr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
SqlConnection con = new SqlConnection(mystr);
con.Open();
SqlCommand cmd = new SqlCommand("AddCat", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@action", "select");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Dd_Cat.DataSource = ds;
Dd_Cat.DataBind();
con.Close();
con.Dispose();
}
The onselected
property of dropdownlist
control:
protected void wtf(object sender, EventArgs e)
{
string st = Dd_Cat.SelectedValue;
}
This always show the value "1" nomatter what is selected.Please help.
Upvotes: 0
Views: 1261
Reputation: 460058
I assume you call bindDropdown
in Page_Load
without using !IsPostBack
-check. Then you load always the default values and the SelectedIndexChanged
event isn't triggered.
So use this instead if EnableViewState
is set to true
(default):
protected void Page_Load(Object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// page is loaded the first time, load from database
bindDropdown();
}
}
Read: Page.IsPostBack
Upvotes: 2