【问题标题】:Asp.net DropDownList not binding inside a ListViewAsp.net DropDownList 未绑定在 ListView 内
【发布时间】:2015-03-19 15:54:56
【问题描述】:

我需要在 ListView 的 ItemTemplate 中绑定一个 Asp.net DropDownList。我正在使用 LINQ 使用 LINQ db 上下文查询数据,如下所示:

.cs

protected void ListView_AllTickets_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    DataClassesDataContext db = new DataClassesDataContext();
    DropDownList ddl_spList = (DropDownList)e.Item.FindControl("DropDownList_SpList");

    //Getting all service providers users
    var spusers = (from x in db.User1s where x.usertype == "200" select x);

    ddl_spList.DataSource = spusers;

    ddl_spList.DataTextField = "email";

    ListView_AllTickets.DataBind();

}

.aspx

<asp:DropDownList ID="DropDownList_SpList" runat="server" class="form-control" ClientIDMode="AutoID"> </asp:DropDownList>

注意我是如何找到控件然后将其绑定到 LINQ 查询的结果的。当我使用调试器时,数据检索成功,返回的数据中存在“email”字段。但是,出于某种原因,即使在 DataBind() 语句之后,ListView_AllTickets 的项目计数也会为 0。

【问题讨论】:

  • 试试 ddl_spList.DataBind() 可能吗?
  • @Maximus2012 有效!

标签: c# asp.net linq listview data-binding


【解决方案1】:

您需要添加这一行:

 ddl_spList.DataBind();

您正在重新绑定ListView_AllTickets,但这是父对象并且已经被绑定(因此您正在使用此方法处理事件)。是不是打错字了?

改为绑定ddl_spList

【讨论】:

  • 天哪!我花了半天时间试图弄清楚我的代码出了什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多