Access Repeater values using JavaScript

前端 未结 2 1946
执笔经年
执笔经年 2021-01-16 12:52

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 th

相关标签:
2条回答
  • 2021-01-16 13:16

    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.

    0 讨论(0)
  • 2021-01-16 13:25
    <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 comes 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.

    0 讨论(0)
提交回复
热议问题