Why can't get the correct dropdownlist value in asp.net
I have the following codes in asp.net:
<asp:dropdownlist id="ddlApp" runat="server" /> <asp:button id="btnSmt" runat="server" Text="Submit" /> and the code behind:
private void btnSmt_Click(object sender, System.EventArgs e) { lbl.Text = ddlApp.SelectedItem.Value; } The logic is very simple. Get the selected value of the dropdown list and pass it to lbl.text.
But the problem no matter how I try, the text shows the first value of the list in the drop-down list, not the selected value.
And I notice that every time I click the page refresh button.
Please, help.
By the way, I have the following event binding:
private void InitializeComponent() { this.btnSmt.Click += new System.EventHandler(this.btnSmt_Click); this.Load += new System.EventHandler(this.Page_Load); this.ddlApp.SelectedIndexChanged +=new System.EventHandler(this.ddlApp_Change); } You must bind to the dropdown in
if (!Page.IsPostBack) Otherwise, it will rebuild the items for the drop-down list at each postback and therefore only return the selected item in the new collection - this is the first.
It also looks like you are missing btnSmt_Click on a button, but you probably installed it somewhere else ...
At first you debugged this ??? The reason is that C # code seems to be root.
Try changing this:
<asp:button id="btnSmt" runat="server" Text="Submit" /> For
<asp:button id="btnSmt" runat="server" Text="Submit" OnClick="btnSmt_Click" /> If this is really your code, your click event would never be caught, so if you put a breakpoint in your C # code, you would see that the action does not fire.
Anyway, hope this helps