Access Repeater values using JavaScript

有些话、适合烂在心里 提交于 2019-12-01 12:31:52
<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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!