locked
Finding a control in a DataList RRS feed

  • Question

  • I have a DataList and I am trying to find a TextBox within the control using the find control method

    Here is what I am using but its not working.

    TextBox txtAlias = (TextBox)DataListSensors.FindControl("txtAlias");

    Any suggestions?


    Jim
    Saturday, April 3, 2010 7:49 PM

Answers

  • Hi,

    Please post your asp.net question in the ASP.NET Forums in future.

    DataList is populated as a DataListItemCollection. Each item in a DataList is a type of DataListItem object. You need to use FindControl method on DataListItem instead of DataList itself.

    For example, To find control in the 4th item

    TextBox txtAlias = (TextBox)DataList1.Items[3].FindControl("txtAlias");  //as index starts from 0

     

    • Marked as answer by Bin-ze Zhao Wednesday, April 7, 2010 8:58 AM
    Sunday, April 4, 2010 3:00 AM
  • Hi,

    See the below code to get TextBox from Datalist 

    Method  1.

    If you need to get all textbox from all row of datalist,

    foreach (DataListItem dli in dataLst.Items)

                {

                    TextBox cb = (TextBox)dli.FindControl("txtName");

                }

     

    Method  2.

    If you need to get textbox from current row (selected row) of datalist,

    protected void dataLst_ItemCommand(object source, DataListCommandEventArgs e)

    {

    TextBox txtName = (TextBox)e.Item.FindControl("txtName");

    }

    Do let me know,if you need more details.


    Cheers, Sanjiv Kumar Sanjiv Blog | LinkedIn
    • Marked as answer by Bin-ze Zhao Wednesday, April 7, 2010 8:58 AM
    Sunday, April 4, 2010 4:17 AM

All replies

  • Hi,

    Please post your asp.net question in the ASP.NET Forums in future.

    DataList is populated as a DataListItemCollection. Each item in a DataList is a type of DataListItem object. You need to use FindControl method on DataListItem instead of DataList itself.

    For example, To find control in the 4th item

    TextBox txtAlias = (TextBox)DataList1.Items[3].FindControl("txtAlias");  //as index starts from 0

     

    Sunday, April 4, 2010 3:00 AM
  • Hi,

    Please post your asp.net question in the ASP.NET Forums in future.

    DataList is populated as a DataListItemCollection. Each item in a DataList is a type of DataListItem object. You need to use FindControl method on DataListItem instead of DataList itself.

    For example, To find control in the 4th item

    TextBox txtAlias = (TextBox)DataList1.Items[3].FindControl("txtAlias");  //as index starts from 0

     

    • Marked as answer by Bin-ze Zhao Wednesday, April 7, 2010 8:58 AM
    Sunday, April 4, 2010 3:00 AM
  • For questions about asp.net, visit forums.asp.net.

    This forum is for language-related questions only.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Sunday, April 4, 2010 3:01 AM
  • Hi,

    See the below code to get TextBox from Datalist 

    Method  1.

    If you need to get all textbox from all row of datalist,

    foreach (DataListItem dli in dataLst.Items)

                {

                    TextBox cb = (TextBox)dli.FindControl("txtName");

                }

     

    Method  2.

    If you need to get textbox from current row (selected row) of datalist,

    protected void dataLst_ItemCommand(object source, DataListCommandEventArgs e)

    {

    TextBox txtName = (TextBox)e.Item.FindControl("txtName");

    }

    Do let me know,if you need more details.


    Cheers, Sanjiv Kumar Sanjiv Blog | LinkedIn
    • Marked as answer by Bin-ze Zhao Wednesday, April 7, 2010 8:58 AM
    Sunday, April 4, 2010 4:17 AM
  • I have text box in datalist which I want to make visible on click of button from selected row.
    Tuesday, May 10, 2011 1:20 PM