简体   繁体   中英

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

<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"

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.

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

"repeater name" + "_" + "label name" + "_" + row index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM