.net - Access Repeater values using JavaScript - Stack Overflow

i searched a lot on NET, to get the solution, but i could not find Can anyone tell me how to access the

i searched a lot on NET, to get the solution, but i could not find

Can anyone tell me how to access the label and textbox values of repeater control inside using the javascript ?

This is my code

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <table id="t1" width="200px:" style="background-color: skyblue" runat="server">
            <tr>
                <td>
                    <asp:TextBox ID="TextBox3" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'
                        runat="server" />
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                    <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'></asp:Label>
                    <asp:Label ID="lblname" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ename")%>'></asp:Label>
                    <br />
                    <br />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

Now i want to access the label, textbox of repeater using javascript

@Diodeus

I tried your code

function submitAll() {
        var thisLabel = $('.myLabel').eq(0);
        alert(thisLabel);
    }

But i got the result in alert as

[object Object]

and @deostroll

I tried your code this way

But not getting anything

    function GetData() {
        var arrTables = document.getElementById('myDiv').getElementsByTagName('table');
        var tbl = arrTables[0];
        var td = tbl.childNodes[0].childNodes[0].childNodes[0];
        var txt = td.childNodes[0];
        alert(txt.value);        
    }

i searched a lot on NET, to get the solution, but i could not find

Can anyone tell me how to access the label and textbox values of repeater control inside using the javascript ?

This is my code

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <table id="t1" width="200px:" style="background-color: skyblue" runat="server">
            <tr>
                <td>
                    <asp:TextBox ID="TextBox3" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'
                        runat="server" />
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                    <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'></asp:Label>
                    <asp:Label ID="lblname" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ename")%>'></asp:Label>
                    <br />
                    <br />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

Now i want to access the label, textbox of repeater using javascript

@Diodeus

I tried your code

function submitAll() {
        var thisLabel = $('.myLabel').eq(0);
        alert(thisLabel);
    }

But i got the result in alert as

[object Object]

and @deostroll

I tried your code this way

But not getting anything

    function GetData() {
        var arrTables = document.getElementById('myDiv').getElementsByTagName('table');
        var tbl = arrTables[0];
        var td = tbl.childNodes[0].childNodes[0].childNodes[0];
        var txt = td.childNodes[0];
        alert(txt.value);        
    }
Share Improve this question edited Dec 1, 2011 at 15:53 Dipa Ahuja asked Dec 1, 2011 at 14:03 Dipa AhujaDipa Ahuja 391 gold badge1 silver badge7 bronze badges 1
  • 1 Please provide the relevant HTML you are asking about. – jfriend00 Commented Dec 1, 2011 at 14:06
Add a ment  | 

2 Answers 2

Reset to default 2
<asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'></asp:Label>

IDs must be unique, so you can't apply the same ID to all of the labels in your repeater. Use CSS class names instead.

<asp:Label CssClass="myLabel" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'></asp:Label>

Since jQuery es with .NET you can use it instead of plain JavaScript to access these elements more easily.

var thisLabel = $('.myLabel').eq(0) where 0 is the index of the element since there can be many.

Wrap the repeater in a div with some id, say myDiv.

<div id="myDiv">
<!-- your repeater code -->
<asp:Repeater ID="Repeater1" runat="server"...>
<ItemTemplate>
<table>...</table>
</ItemTemplate>
</asp:Repeater>
</div>

Do a

var arrTables = document.getElementById('myDiv').getElementsByTagName('table');

Now arrTables is just array of all table elements inside the div. Find the ordinal of the desired table: For e.g. sake I am taking first table.

var tbl = arrTables[0];

Find the corresponding td of the table element:

var td = tbl.childNodes[0].childNodes[0].childNodes[0];

The above may vary based on how your browser loads the DOM and stuff. Hence I say you debug and find out for your self.

Once you get the td reference:

var txt = td.childNodes[0];

This will give the textbox. txt.nextSibling will give the label...and so on.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745042450a4607892.html

相关推荐

  • .net - Access Repeater values using JavaScript - Stack Overflow

    i searched a lot on NET, to get the solution, but i could not find Can anyone tell me how to access the

    22小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信