【问题标题】:ASP.NET DropDownList in ListView's OnItemUpdating method always has a SelectedIndex of 0ListView 的 OnItemUpdating 方法中的 ASP.NET DropDownList 的 SelectedIndex 始终为 0
【发布时间】:2015-05-13 20:43:30
【问题描述】:

在 ListView 的EditTemplate 中,我有一个 DropDownList 控件。 DropDownList 填充在 ListViewItemDataBound 方法中(我发现只有这样才能让列表填充到 EditTemplate 中……DropDownList 仅存在于 EditTemplate 中;它必须是 @987654324 中的标签@)。

列表已填充,一切正常。我遇到的问题是,当单击更新按钮并调用 ItemUpdating 方法时,DropDownList 的 SelectedIndex始终为零,无论当时选择了哪个值。 p>

aspx 代码,减去额外的模板如下所示:

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound" OnItemUpdating="ListView1_ItemUpdating" OnItemEditing="ListView1_ItemEditing">
<EditItemTemplate>
    <tr style="">
    <td><asp:DropDownList CssClass="vaultDropDownList" ID="EditStaffList" runat="server" AutoPostBack="false" CausesValidation="True"></asp:DropDownList></td>
    <td>
       <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
       <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
    </td>
    </tr>
    </EditItemTemplate>
</asp:ListView>

还有代码隐藏:

protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{            
    ListView1.EditIndex = e.NewEditIndex;
}

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.DisplayIndex == ListView1.EditIndex)
    {
        DropDownList ddl = e.Item.FindControl("EditStaffList") as DropDownList;
        BindDropDownList(ddl);
    }
}

protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
   DropDownList staff = ListView1.Items[e.ItemIndex].FindControl("EditStaffList") as DropDownList;

   //SQL update stuff will go here
   ListView1.EditIndex = -1;
}

如果我尝试在Page_Load 方法中填充DropDownList 以使用IsPostBack,下拉控件总是返回null

非常感谢任何帮助!

编辑:ListView 绑定:

protected void DisplayListView(object sender, EventArgs e) 
{ 
    string connString = SQLstringTooLong; 
    SqlConnection sqlConnection = new SqlConnection(connString); 
    DataTable initialTable = new DataTable(); 
    string queryStatement = "SELECT lotsastuff"; 
    SqlDataAdapter dataAdapter = new SqlDataAdapter(queryStatement, sqlConnection); 
    dataAdapter.Fill(initialTable); 
    ListView1.DataSource = initialTable; 
    ListView1.DataBind(); 
}

Edit2:下拉列表绑定方法如下:

public static void CreateStaffList(DropDownList list, string searchGroup) 
{ 
    ArrayList userList = new ArrayList(); 
    //more SQL stuff 
    list.DataSource = userList; 
    list.DataBind(); 
}

【问题讨论】:

  • 我们可以看看您是如何将数据绑定到您的 ListView 的吗?我怀疑你在每个 PostBack 上都绑定了它。
  • 我在 ItemDataBound 方法中绑定它,如上。我怀疑我也是,但不知道如何只在 EditTemplate 中绑定它,而不是在任何地方的 ItemTemplate 中,但 ListView 的 ItemDataBound 方法。
  • ItemDataBound 事件实际上只是数据绑定过程的一部分。我正在寻找的是当您实际设置 DataSource 并调用 DataBind 时。该代码类似于ListView1.DataSource = someDataSource;ListView1.DataBind();。我的猜测是这些行在您的 Page_Load 事件中。我可以看看你是怎么做这个 DataBinding 的吗?
  • 你打赌!绑定在这里:
  • 好的,你在哪里调用DisplayListView方法?

标签: c# asp.net listview


【解决方案1】:

确保您没有在每个 PostBack 上绑定您的 ListView。将其包装在一个条件中以检查它。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DisplayListView();
    }
}

如果您不这样做,您的整个 ListView,包括您的 DropDownLists 将重新绑定。这会导致 DropDownList 将其 SelectedIndex 重置为 0。

【讨论】:

  • 目前无法投票,但想再次表示感谢。
  • 没问题!欢迎使用 StackOverflow!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多