在ListView的EditItemTemplate中访问控件

5
我正在使用listview控件。默认情况下,我显示itemtemplate和一个编辑按钮。当按下编辑按钮时,listview会切换到edititemtemplate。我需要根据正在编辑的项填充edititemtemplate中的一个控件 - 我已经尝试在ItemEditing事件(以及几乎所有其他事件)中访问控件(通过FindControl),但是控件似乎不存在。我可以正常访问itemtemplate中的控件,但无法访问edititemtemplate。
请问有人能告诉我如何访问listview的edititemtemplate中包含的控件,并从哪个事件开始这样做?
编辑 我正在尝试使用以下方式访问控件:
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
    ListViewItem item = UnitsLV.Items[e.NewEditIndex];
    ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}

我也尝试过在ItemDataBound和ItemCreated中实现。
ListView的声明如下:
<asp:Content ID="Content1" ContentPlaceHolderID="ColumnA" runat="server">
    <asp:Panel ID="Panel1" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Panel ID="SummaryPnl" runat="server">
                    <asp:ListView ID="UnitsLV" runat="server" DataSourceID="DataLDS" DataKeyNames="u_uid"
                        InsertItemPosition="LastItem" OnItemInserting="UnitsLV_ItemInserting" OnItemDataBound="UnitsLV_ItemDataBound"
                        OnItemCreated="UnitsLV_ItemCreated" onitemediting="UnitsLV_ItemEditing">
                        <ItemTemplate>
                            <tr class="rowA">
                                <td>
                                    <asp:Label runat="server" ID="UnitIDLbl" Text='<%# Eval("u_uid")%>'></asp:Label>
                                </td>
                                <td>
                                    <%# Eval("u_Title")%>
                                </td>
                                <td>
                                    <asp:LinkButton ID="EditBtn" runat="server" CommandName="Edit" CommandArgument='<%#Eval("u_uid") %>'
                                    Text="Edit" />
                                </td>
                                <td>
                                    <asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete" CommandArgument='<%#Eval("u_uid") %>'
                                    Text="Delete" />
                                </td>
                            </tr>
                        </ItemTemplate>
                        <InsertItemTemplate>
                            <tr class="rowB">
                                <td>
                                    <br />
                                    &nbsp;
                                </td>
                                <td>
                                    <br />
                                    <asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
                                </td>
                                <td>
                                    <br />
                                    <asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
                                    <asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
                                </td>
                                <td>
                                    <asp:LinkButton ID="InsertBtn" runat="server" CommandName="Insert" Text="Insert" />
                                </td>
                                <td>
                                    <asp:LinkButton ID="CancelBtn" runat="server" CommandName="Cancel" Text="Cancel" />
                                </td>
                            </tr>
                        </InsertItemTemplate>
                        <EditItemTemplate>
                            <tr class="rowB">
                                <td>
                                    <br />
                                    <asp:Label runat="server" ID="UnitIDLbl" Text='<%# Bind("u_uid")%>'></asp:Label>
                                </td>
                                <td>
                                    <br />
                                    <asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
                                </td>
                                <td>
                                    <br />
                                    <asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
                                    <asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
                                </td>
                                <td>
                                    <asp:LinkButton ID="UpdateBtn" runat="server" CommandName="Update" Text="Update" />
                                </td>
                                <td>
                                    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" Text="Cancel" />
                                </td>
                            </tr>
                        </EditItemTemplate>
                        <LayoutTemplate>
                            <table id="Table2" runat="server" width="100%">
                                <tr id="Tr1" runat="server">
                                    <td id="Td1" runat="server">
                                        <table id="itemPlaceholderContainer" runat="server" border="0" style="" width="100%">
                                            <tr id="itemPlaceholder" runat="server"></tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr id="Tr2" runat="server">
                                    <td id="Td2" runat="server" style=""></td>
                                </tr>
                            </table>
                        </LayoutTemplate>
                    </asp:ListView>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>
</asp:Content>

编辑: 我已经使用类似以下代码的方式迭代遍历了列表视图中的所有控件,但是该控件仍然不可见。 ItemEditing事件是否在显示编辑模板之前触发?如果是,我可以使用哪个事件来访问编辑模板控件?

foreach (Control child in control.Controls)
{
   Control result = Find(child, id);
   if (result != null)
   {
      return result;
   }
}

**编辑:** 在listview的ItemCreated事件中,我可以访问edititemtemplate中的控件,但是它们没有任何内容(我认为数据还没有绑定),因此我无法获取所需的键值对以进行查找以获取需要填充控件的数据。


调用“FindControl”方法的代码将非常有帮助,还有声明式ListView的代码。 - Phaedrus
这可能是一个打字错误,但在您的ItemEditing事件中,您正在查找存在于insertitem模板中的'ListBox2'。您在edititemtemplate中的listbox是'ListBox3'。 - Phaedrus
这是我发布的简化代码中的一个打字错误(而不是真正的代码) - 我现在会更正它。 - marc
6个回答

8

我已经找到了一种做我需要做的事情的方法,但是我对这种方法并不是很满意。

protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (UnitsLV.EditIndex > -1)
    {
        // Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
    }
}

1
我在我的问题和答案这里中对此回答进行了改进。我之前曾经被这个问题困扰了数月,感谢这篇文章为我指明了正确的方向 :) - Chiramisu

4

我不知道之前的版本,但在Visual Studio 2010中,您可以通过"ListView.EditItem"属性轻松访问编辑项,如下所示:

private void myListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
   myListView.EditIndex = e.NewEditIndex;
   myListView.DataBind();

   ListViewItem lvwItem = lvwLista.EditItem;
   ListBox tempLB = (ListBox) lvwItem.FindControl("ListBox3");
}

2
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{        
        ListBox myListBox = (ListBox)(((ListView)sender).EditItem.FindControl("ListBox1")); 
}

1
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e) 
{ 
    ListViewItem item = UnitsLV.Items[e.NewEditIndex]; 
    ListBox tempLB = (ListBox)e.item.FindControl("ListBox3"); 
} 

我相信我在上面的函数中发现了一个错别字。第二行应该是

ListBox tempLB = (ListBox)item.FindControl("ListBox3"); 

我所做的就是用"item"替换了"e.item"。

1

我通常使用ItemDataBound事件...检查ListItemType枚举中的其他选项

    protected void UnitLV_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.EditItem)
        {
                ListBox myListBox = (ListBox) e.Item.FindControl("ListBox3");
        }
    }

0

这与被接受的答案类似,但我认为它的作者真正想表达的是:

protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewDataItem listViewDataItem = e.Item as ListViewDataItem;
    if (UnitsLV.EditIndex == listViewDataItem.DataItemIndex)
    {
        // Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接