Home > ASP.Net, Javascript > Access the control inside a DataList using javascript

Access the control inside a DataList using javascript


Hi

try this example to access the control inside a DataList using  javascript

<asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>
    </ItemTemplate>
</asp:DataList>

To access the control inside the DataList, try to use the following way,

function getVal()
{
    var datalist = document.getElementById('<%=DataList1.ClientID%>').childNodes[0];
    var tb = datalist.getElementsByTagName("input");

    for (var i=0;i<tb.length;i++)
    {
        //TextBox
        if (tb[i].type=="text")
        {
        alert(tb[i].value);
        }

        //DropDownList
        if (tb[i].type=="select")
        {
        alert(tb[i].options[tb[i].selectedIndex].value);
        }
    }
}

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. Guru
    April 29, 2011 at 9:58 am

    Hey hi,
    I am not able to find textbox in datalist using if (tb[i].type==”text”)
    can you help me please?

  2. Rumit
    October 31, 2011 at 3:35 pm

    I am able to find all the text box ids using below code, but i want particualar text box id

    • Preethy Paul
      September 11, 2012 at 6:57 am

      I am not able to find a buuton in the datalist plz help me

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: