Accessing controls in the edititemtemplate of a listview

前端 未结 6 1909
半阙折子戏
半阙折子戏 2021-01-06 10:38

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 edititemte

相关标签:
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")
        }
    }
    
    0 讨论(0)
  • 2021-01-06 11:25

    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:

    private void myListView_ItemEditing(object sender, ListViewEditEventArgs e)
    {
       myListView.EditIndex = e.NewEditIndex;
       myListView.DataBind();
    
       ListViewItem lvwItem = lvwLista.EditItem;
       ListBox tempLB = (ListBox) lvwItem.FindControl("ListBox3");
    }
    
    0 讨论(0)
  • 2021-01-06 11:25
    protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {        
            ListBox myListBox = (ListBox)(((ListView)sender).EditItem.FindControl("ListBox1")); 
    }
    
    0 讨论(0)
  • 2021-01-06 11:27
    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"

    0 讨论(0)
  • 2021-01-06 11:28

    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")
        }
    }
    
    0 讨论(0)
  • 2021-01-06 11:31

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

        protected void UnitLV_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                    ListBox myListBox = (ListBox) e.Item.FindControl("ListBox3");
            }
        }
    
    0 讨论(0)
提交回复
热议问题