简体   繁体   中英

Accessing Eval value inside Gridview Using JQuery OR JavaScript

I have gridview with four columns . All the data inside is generated in RowDataBound event using Literals eg.

      <asp:TemplateField HeaderText="" >
            <ItemTemplate>
                 <asp:HiddenField ID="ID"  Value='<%#Eval("id")%>' runat="server" />  

                <asp:Literal ID="ltrImage" runat="server"></asp:Literal>

            </ItemTemplate>
        </asp:TemplateField>  

       and

      <asp:TemplateField HeaderText="" HeaderStyle-HorizontalAlign="Center"
            ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Literal ID="lrtBrief" runat="server"></asp:Literal>      
            </ItemTemplate>
        </asp:TemplateField>    

In one of these Literals I am creating a <a> tag and I want to find the <%#Eval("id")%> value in click event of the <a> tag.

I did try something like this but didn't work

$('.contactLink').click(function () {

    var grd = $('#MainContent_cphMain_DisplayResults1_gvDisplay');
    var txt = $(grd).find("cells[1].innerHTML").html();

});

HTML Looks Like this

<td align="center" style="width:5%;">
<input id="MainContent_cphMain_DisplaySearchResults1_gvListing_Listing_0" type="hidden" value="1" name="ctl00$ctl00$MainContent$cphMain$DisplaySearchResults1$gvListing$ctl02$Listing">
  <table class="contact">
  <tbody>
    <tr>
      <td valign="top">
      <h3>Test</h3>
      <h3>
      <a id="hlContact" class="contactLink format"  runat="server">Contact</a>
      <a id="hlViewF" class="viewLink format" href="CategoriesSearch.aspx?ID=1" runat="server">View </a>
      </td>
    </tr>
  </tbody>
  </table>
</td>

Any help will be appreciate

Thanks

Note that you're missing a closing h3 around your two links.

$(".contactLink").click(function() {
    var value = $(this).closest("table") // Get the <a>'s closest <table>
                       .siblings(":hidden") // <hidden>
                       .val(); 
});

Fiddle for reference.

if I understand your question right:

In one of these Literals I am creating a tag and I want to find the <%#Eval("id")%> value in click event of the tag.

I would say something like this would do the trick

$('a').live("click", function () {
alert($(this).attr("id"))
});

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