【问题标题】:Dropdown list selecteditem or selectedindex not working as expected下拉列表 selecteditem 或 selectedindex 未按预期工作
【发布时间】:2011-10-14 20:09:20
【问题描述】:

我有一个 DropDownList 和一个函数,它获取选择的值,但 SelectedIndex 和 SelectedValue 始终返回第一项。

下拉代码是

<asp:DropDownList ID="lstApps" runat="server" DataSourceID="sqlDataSource" 
                        DataTextField="some_val" DataValueField="some_id"
                        TabIndex="5" >
</asp:DropDownList>

和代码(在按钮中点击页面某处的按钮)

int x = lstApps.SelectedIndex;

不管我选择了什么,总是返回 0。是因为自动回发被禁用还是其他原因?

【问题讨论】:

  • 请检查您是否在 page_load 中绑定了 ! (IsPostback) 子句?
  • 刚刚看到。掌心。
  • 我想那一定解决了你的问题

标签: c# asp.net drop-down-menu


【解决方案1】:

我猜!您需要在 Page_Load 事件中使用 IsPostBack 块。

public void Page_Load()
{
  if(!IsPostBack) 
  {
     //put databinding code here.
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2020-06-02
    相关资源
    最近更新 更多