访问listview的edititemtemplate中的控件

时间:2022-06-01 19:49:16

I am working with the listview control. By default I am showing the itemtemplate with an edit button. When the edit button is pressed the listview switches to the edititemtemplate. I need to populate one of the controls in the edititemtemplate based on the item being edited - I've tried accessing the control (via FindControl) in the ItemEditing event (and pretty much every other event as well), however the controls just don't seem to exist. I can access controls in the itemtemplate ok, but not the edititemtemplate.

我正在使用listview控件。默认情况下,我使用编辑按钮显示itemtemplate。按下编辑按钮后,列表视图将切换到edititemtemplate。我需要根据正在编辑的项目填充edititemtemplate中的一个控件 - 我尝试在ItemEditing事件中访问控件(通过FindControl)(以及几乎所有其他事件),但是控件只是'似乎存在。我可以在itemtemplate ok中访问控件,但不能访问edititemtemplate。

Can anyone let me know how I can access a control held within the edititemtemplate of a listview, and from which event I should do so?

任何人都可以告诉我如何访问listview的edititemtemplate中的控件,以及我应该从哪个事件中执行此操作?

EDIT I'm trying to access the control using this:

编辑我正在尝试使用以下方法访问控件:

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

I've also tried in ItemDataBound and ItemCreated.

我也尝试过ItemDataBound和ItemCreated。

The listview declaration is:

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>

EDIT: I've iterated over all the controls in the listview using code similar to below, and the control is still not visible. Does the ItemEditing event fire before the edit template is shown? If so, what event can I use to access the edit template controls?

编辑:我使用类似于下面的代码迭代列表视图中的所有控件,并且控件仍然不可见。 ItemEditing事件是否在显示编辑模板之前触发?如果是这样,我可以使用什么事件来访问编辑模板控件?

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

**EDIT: ** I can access the controls in the edititemtemplate in the listview's ItemCreated event, however none they have no content (I'd assume the data hasn't been bound yet), so I can't get the key-value I need to do a lookup to get the data I need to populate the control.

**编辑:**我可以访问listview的ItemCreated事件中的edititemtemplate中的控件,但是没有他们没有内容(我假设数据还没有绑定),所以我无法获得密钥 - 值我需要进行查找以获取填充控件所需的数据。

6 个解决方案

#1


I've figured out a way to do what I need to do, though I'm not terribly happy with it.

我已经找到了一种方法来做我需要做的事情,尽管我对此并不十分满意。

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

#2


I don't know about previous editions but in Visual Studio 2010 you can easily access the edit item trhough the "ListView.EditItem" property like this:

我不知道以前的版本,但在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");
}

#3


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

#4


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

I believe I found a typo in the above function. The second line should be

我相信我在上面的功能中发现了一个拼写错误。第二行应该是

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

What I did was replace "e.item" with "item"

我做的是用“item”替换“e.item”

#5


I usually use the ItemDataBound Event... check the other options in ListItemType Enum

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

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

#6


this is similar to the accepted answer, but I think its author was really driving towards this:

这与接受的答案类似,但我认为它的作者真的是朝着这个方向努力:

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")
    }
}

#1


I've figured out a way to do what I need to do, though I'm not terribly happy with it.

我已经找到了一种方法来做我需要做的事情,尽管我对此并不十分满意。

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

#2


I don't know about previous editions but in Visual Studio 2010 you can easily access the edit item trhough the "ListView.EditItem" property like this:

我不知道以前的版本,但在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");
}

#3


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

#4


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

I believe I found a typo in the above function. The second line should be

我相信我在上面的功能中发现了一个拼写错误。第二行应该是

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

What I did was replace "e.item" with "item"

我做的是用“item”替换“e.item”

#5


I usually use the ItemDataBound Event... check the other options in ListItemType Enum

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

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

#6


this is similar to the accepted answer, but I think its author was really driving towards this:

这与接受的答案类似,但我认为它的作者真的是朝着这个方向努力:

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")
    }
}