locked
How do I access controls inside ListView ItemTemplate? RRS feed

  • Question

  • User1005758432 posted

    Will somone show me how to access the controls such as Lable, TextBox, or HiddenField controls inside a LstView ItemTemplate? I want to insert this information into a SQL Server database but I couldn't figure out a way to get those information from C# code behind.

    Thursday, October 15, 2009 11:39 AM

Answers

  • User-1360095595 posted

    You could handle the ItemDataBound event, and in there check e.Item.ItemType == ListViewItemType.DataItem then do e.Item.FindControl(etc.).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2009 1:31 PM

All replies

  • User-1360095595 posted

    The basic idea is that you need to use FindControl on a specify Item in the listview.  So you need to find the item in question, maybe via a datakeynames key, or via an event that potentially exposes e.Item as a property.  Then you do FindControl on the item and cast it to the control type.  You'll then have access to the control's properties.

    Thursday, October 15, 2009 11:53 AM
  • User1005758432 posted

    Yes that's the basic idea and I know how to do that for EditItemTemplate, InsertItemTemplate but now I need to access the ItemTemplate. To access the InsertItemTemplate I use the PrePrender method and to access the EditItemTemplate, I had to use the OnPreRender method of the control itself; however, when I'm trying to access the ItemTemplate, there are just more than one item inside the ItemTemplate or AlternatingItemTemplate. How do I find the right item that I want and how do I find it? What method or what property do I use to get inside the ItemTemplate and AlternatingItemTemplate. That's where I get stuck.

    Thursday, October 15, 2009 12:13 PM
  • User-134772610 posted

    Use this event to access the controls from the ItemTemplate

    protected void DataList_ItemCommand(object source, DataListCommandEventArgs e)
            {
    
    
                   TextBox txt=  (e.Item.FindControl("TextBox1") as TextBox).Text
    
            }


    Thursday, October 15, 2009 1:27 PM
  • User-1360095595 posted

    You could handle the ItemDataBound event, and in there check e.Item.ItemType == ListViewItemType.DataItem then do e.Item.FindControl(etc.).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2009 1:31 PM