Oh hi Mark
Oh hi Mark

Reputation: 203

Asp.net Image Button does not have an onClick event

This code is inside a GridView,

<asp:TemplateField HeaderText="Edit" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
      <ItemTemplate>
        <asp:Image ID="imgEdit" ImageUrl="~/edit.png" runat="server"/> 
       </ItemTemplate>
</asp:TemplateField>

My problem is that if I try to create an onClick event inside the <asp:Image attribute it is not available. What might be the issue?

Upvotes: 2

Views: 2023

Answers (2)

Izzy
Izzy

Reputation: 6866

<asp:Image /> is used to display images only, hence you're not able to use OnClick Event. The control you're looking for is <asp:ImageButton /> which will allow you to use the OnClick Event.

<asp:ImageButton runat="server" OnClick="MyClickFunction" />

Upvotes: 2

sbouaked
sbouaked

Reputation: 980

You can use an ImageButton like that :

<%@ Page Language="C#" AutoEventWireup="True" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>ImageButton Sample</title>
    <script language="C#" runat="server">

          void ImageButton_Click(object sender, ImageClickEventArgs e) 
          {
             Label1.Text = "You clicked the ImageButton control at the coordinates: (" + 
                           e.X.ToString() + ", " + e.Y.ToString() + ")";
          }

       </script>

    </head>

    <body>

       <form id="form1" runat="server">

          <h3>ImageButton Sample</h3>

          Click anywhere on the image.<br /><br />

          <asp:ImageButton id="imagebutton1" runat="server"
               AlternateText="ImageButton 1"
               ImageAlign="left"
               ImageUrl="images/pict.jpg"
               OnClick="ImageButton_Click"/>

          <br /><br />

          <asp:label id="Label1" runat="server"/>

       </form>

    </body>
    </html>

If is on Client side you can simply use a <a href="">..</a>

more at : https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.imagebutton.onclick(v=vs.110).aspx

Upvotes: 2

Related Questions