【问题标题】:DropDownlist returning first item alwaysDropDownlist 总是返回第一项
【发布时间】:2012-08-20 03:16:37
【问题描述】:

aspx 页面有一个 UC。在 aspx.cs 中

if(!isPostBack){
    UC.DataBind();
 }

这个 UC 还有一个用户控件 UC1。在 UC.ascx.cs 中

 public void DataBind(){
   UC1.DataBind();
 }

在 UC1 中

public void  DataBind(){
   List<Registeration> list = ... VAlues from database;
   dropdownlist1.DataSource = list;
   DropDownList1.DataTextField = 'username';
   DropDownList1.DataValueField = 'username';
   DropDownList1.databind();
}  

public void button1_click(){
    string selectedValue = DropDownList1.SelectedItem.Text;
 } 

问题:字符串 selectedValue 的值总是从第一项出来......

评论:DropDownList 在 updatePanel 中

【问题讨论】:

    标签: asp.net drop-down-menu


    【解决方案1】:

    除了 DataTextField 之外,尝试填充 DropDownLost1.DataValueField。因为它没有被分配,所以它可能对于呈现的 DropDown 中的所有项目都是空的。要检查这一点,您可以查看 FireBug 中呈现的 HTML 以了解显示的内容。

    【讨论】:

    • 所以渲染的 HTML 包含每个 ListItem 的 Text 和 Value?
    • 我唯一能想到的另一件事是 UC.DataBind() 或 UC1.DataBind() 在另一个位置被调用。在我看来,其他一切都很好。
    【解决方案2】:

    看起来你做的一切都是正确的,但你还需要在你的用户控件上添加isPostBack()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多