问题
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);
}
回答1:
<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.
回答2:
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.
来源:https://stackoverflow.com/questions/8342583/access-repeater-values-using-javascript