![](/img/trans.png)
[英]How to pass a hidden field value on javascript function inside repeater control
[英]How to 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>
我的代碼是
var valComments =document.getElementById('<%=rlbl_Remarks.ClientID%>').innerText;
嗯,問題是中繼器可能有 1 或 15 個“組”
所以,你可以這樣做:
<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>
所以,在上面,我們可以像這樣填充中繼器:
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
然后我們現在有了這個:
當我們點擊行click時,它會調用js,並獲取一個酒店的值。
例如:
所以,即使你在中繼器中只有 1 個設置,它的索引將從 0 開始,每個 label 的名稱都是
"repeater name" + "_" + "label name" + "_" + row index
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.