How to retrieve the ID of the selected element in asp: Repeater via javascript?

1

I have a asp:Repeater and need to retrieve in javascript the ID of the item referring to the record displayed in asp:Repeater .

See how my asp:Repeater

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_OnItemDataBound">
    <ItemTemplate>
        <div id="div" runat="server" style="margin: 5px 0 5px 0; min-height: 10px;
            padding-bottom: 10px; width: 100%">
            <div style="width: 40%; float: left; position: relative">
                <asp:HiddenField runat="server" ID="hdnID" Value='<%# Eval(" ID ") %>' />
                <asp:Label runat="server" ID="lblNome" Text='<%# Eval(" Nome ") %>' />
            </div>
            <div style="width: 30%; float: left; position: relative">
                <asp:TextBox runat="server" ID="txtData" ReadOnly="True" CssClass="Data" />
            </div>
            <div style="width: 30%; float: left; position: relative">
                <asp:LinkButton runat="server" ID="btnInformacoes" Text="Informações" Visible="False" />
            </div>
        </div>
        <div class="linha1">
        </div>
    </ItemTemplate>
</asp:Repeater>

See that I have a asp:HiddenField that stores the ID .

What I need is to retrieve this ID to move to the javascript function below.

$.ajax
            ({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "listas.aspx/Listar",
                data: "{'id':'" + "58" + "'}", // nesta linha, no lugar do 58, quero passar o ID que esta no meu asp:Repeater.
                dataType: "json",
                success: function (data) {
                    alert('Sucesso');
                },
                error: function (result) {
                    alert('Erro');
                }
            }); 

Does anyone have any idea how I can do this?

    
asked by anonymous 19.01.2015 / 16:44

1 answer

2

I was able to solve the problem as follows.

Follow the javascript code with the solution:

$(".elemento").click(function () {
    var index = this.id.substr(this.id.length - 1, 1);
    var id = $("[id*='hdnID']")[index].value;
    $.ajax
       ({
           type: "POST",
           contentType: "application/json; charset=utf-8",
           url: "listas.aspx/Listar",
           data: "{'id':'" + id + "'}",
           dataType: "json",
           success: function (data) {
                alert('Sucesso');
           },
           error: function (result) {
                alert('Erro');
           }
       });
}); 
    
19.01.2015 / 22:47