Click event for ItemTemplate inside asp: GridView

I have a gridview with the following markup:

<asp:GridView ID="gdvResxKeyValue" runat="server" Width="100%" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:Image ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

I need to have a handler for the image click event. Is there an easier way to do this?

+4
source share
3 answers

You can use the image button instead of the image. Try this code.

  <asp:GridView ID="gdvResxKeyValue" runat="server" Width="100%" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" OnClick="YourEventName" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

You just need to specify the name of your server on the server side.

+4
source

Use the asp button and set its display:none style

 <asp:Image ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" onclick="ClickImage(this)" /> ....... <asp:Button ID="hiddenButton" runat="server" OnClick="hiddenButton_Click" style="display:none"></asp:Button> <script type="text/javascript"> function ClickImage(imageControl) { document.getElementById('<%=hiddenButton.ClientID%>').click(); } </script> 

This will trigger an event on the server side and you can do your work there.

+1
source

Try the following:

jquery solution

 <asp:Image ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" onclick="this.next().click()"/> <asp:LinkButton Text="text" runat="server" OnClick="call_method"/> 
0
source

Source: https://habr.com/ru/post/1498908/


All Articles