How to recover the ID of the selected element in Asp:Repeater via javascript?

Asked

Viewed 686 times

1

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

Take a look at mine 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 one asp:HiddenField which stores the ID.

What I need is to get this one back ID to move to the function javascript down 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?

1 answer

2

I managed to solve the problem as follows.

follows the code javascript 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');
           }
       });
}); 
  • Ribeiro de Agusto So you can mark the answer as right.

  • I can only accept my own answer after 2 days. That’s good, will someone have a better idea.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.