简体   繁体   中英

Dropdown List SelectedItems Postback Not there

I'm not sure what to do here, I might have to use viewstates, but I need help.

I have a dropdown list, I am not databinding. I would know if I was I should do a Page.IsPostBack and not databind.

<asp:DropDownList ID="ddlWeeklyWeightIn" runat="server">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    <asp:ListItem>4</asp:ListItem>
</asp:DropDownList>

Now in my code behind I have this:

 protected void Button1_Click(object sender, EventArgs e)
    {
        string wwin = "";
        wwin = ddlWeeklyWeightIn.SelectedItem.Text;
    }

On the button click is always "1", never the selected item.

Thank you

尝试将EnableViewState =“ True”添加到下拉控件中。

A post back is causing to reset your selected value. either use if(!IsPostBack) or use update panel to prevent post back.

This is just to make sure but could you try using:

<asp:DropDownList ID="ddlWeeklyWeightIn" runat="server">
    <asp:ListItem Text="1"></asp:ListItem>
    <asp:ListItem Text="2"></asp:ListItem>
    <asp:ListItem Text="3"></asp:ListItem>
    <asp:ListItem Text="4"></asp:ListItem>
</asp:DropDownList>

Could you check you don't just SET the selected item during page's initialization?

Aspx side: add autopostback=true to dropdown property and and use listitem as following: ListItem Text="4" Value="4"> etc

Code Behind: string wwin = ""; wwin = ddlWeeklyWeightIn.SelectedValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM