Access controls in the edititemtemplate list

I am working with a listview control. By default, I display an itemtemplate with an edit button. When the edit button is clicked, the list opens on the edititemtemplate page. I need to populate one of the controls in the edititemtemplate file based on the element being edited. I tried to access the control (via FindControl) in the ItemEditing event (and almost all other events), however the controls just do not work, t seems to exist. I can access the controls in the itemtemplate element in order, but not in the edititemtemplate file.

Can someone tell me how I can access the control contained in the edititemtemplate of the list, and from which event should I do this?

EDIT I'm trying to access a 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 also tried using ItemDataBound and ItemCreated.

List Announcement:

 <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 checked all the controls in the list using code similar to the one below and the control is still not showing. Does the ItemEditing event fire before the edit template is created? If so, what event can I use to access edit template controls?

 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 file in the ItemCreated event, but none of them have content (I would assume that the data has not yet been bound), so I can not get the key value, which I need to do to get the data needed to populate the control.

+4
source share
6 answers

I figured out a way to do what I need to do, although I'm not very 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") } } 
+8
source

I do not know about previous releases, but in Visual Studio 2010 you can easily access the editing element, i.e. ListView.EditItem property, for example:

 private void myListView_ItemEditing(object sender, ListViewEditEventArgs e) { myListView.EditIndex = e.NewEditIndex; myListView.DataBind(); ListViewItem lvwItem = lvwLista.EditItem; ListBox tempLB = (ListBox) lvwItem.FindControl("ListBox3"); } 
+4
source
 protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e) { ListBox myListBox = (ListBox)(((ListView)sender).EditItem.FindControl("ListBox1")); } 
+2
source
 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"); 

I made the replacement of "e.item" with "item"

+1
source

I usually use the ItemDataBound event ... check other parameters in the ListItemType Enum

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

this seems like an accepted answer, but I think its author really moved 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") } } 
0
source

All Articles