[英]How to want to get label value inside the repeater control in JavaScript variable
I want to get label value inside the repeater control in JavaScript variable我想在 JavaScript 变量中的转发器控件中获取 label 值
<asp:Repeater ID="rep_tskAttachments" runat="server" OnItemDataBound="rep_tskAttachments_ItemDataBound">
<asp:Label ID="rlbl_Remarks" runat="server" Text='<%# Eval("REMARKS") %>'></asp:Label>
</asp:Repeater>
My Code Is我的代码是
var valComments =document.getElementById('<%=rlbl_Remarks.ClientID%>').innerText;
Well, the problem is the repeater might have 1 or 15 "sets"嗯,问题是中继器可能有 1 或 15 个“组”
So, you can do it like this:所以,你可以这样做:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lblHotel" runat="server"
width="200px"
Text='<%# Eval("HotelName") %>' >
</asp:Label>
<asp:Button ID="Button1" runat="server" Text="Row click (client side)"
OnClientClick='<%# "myrowfun(" + Container.ItemIndex.ToString + ");return false;" %>'
/>
<br />
</ItemTemplate>
</asp:Repeater>
<script>
function myrowfun(ix) {
alert("row click index = " + ix)
lblID = "Repeater1_lblHotel_" + ix
alert(lblID)
// jQuery
alert("Hotel name from repeater = " + $('#' + lblID).text())
// javascript without jQuery
MyLabel = document.getElementById(lblID)
alert("Hotel = " + MyLabel.innerText)
}
</script>
So, in above, we might fill the repeater like this:所以,在上面,我们可以像这样填充中继器:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using con As New SqlConnection(My.Settings.TEST3)
Using cmdSQL =
New SqlCommand("SELECT TOP 5 ID, FirstName, LastName, HotelName from tblHotels ORDER BY HotelName", con)
con.Open()
Repeater1.DataSource = cmdSQL.ExecuteReader
Repeater1.DataBind()
End Using
End Using
End Sub
And then we now have this:然后我们现在有了这个:
When we click on the row click, it calls the js, and gets the one hotel value.当我们点击行click时,它会调用js,并获取一个酒店的值。
eg:例如:
so, even if you have only 1 set in the repeater, it will start at 0 for the index, and each label will have a name of所以,即使你在中继器中只有 1 个设置,它的索引将从 0 开始,每个 label 的名称都是
"repeater name" + "_" + "label name" + "_" + row index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.