简体   繁体   English

下拉列表SelectedItems回发不存在

[英]Dropdown List SelectedItems Postback Not there

I'm not sure what to do here, I might have to use viewstates, but I need help. 我不确定在这里做什么,我可能必须使用viewstates,但是我需要帮助。

I have a dropdown list, I am not databinding. 我有一个下拉列表,我没有数据绑定。 I would know if I was I should do a Page.IsPostBack and not databind. 我想知道我是否应该执行Page.IsPostBack而不是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. 在按钮上单击总是“ 1”,而不是所选项目。

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. 请使用if(!IsPostBack)或使用更新面板来防止回发。

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 Aspx方面:将autopostback = true添加到下拉属性,并使用listitem,如下所示:ListItem Text =“ 4” Value =“ 4”>等

Code Behind: string wwin = ""; 后面的代码: string wwin =“”; wwin = ddlWeeklyWeightIn.SelectedValue; wwin = ddlWeeklyWeightIn.SelectedValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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