37717

Asp.net Image Button does not have an onClick event

Question:

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?

Answer1:

<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" />

Answer2:

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 : <a href="https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.imagebutton.onclick(v=vs.110).aspx" rel="nofollow">https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.imagebutton.onclick(v=vs.110).aspx</a>

Recommend

  • Configuration Error 'Facebook.FacebookConfigurationSection'
  • ASP.NET pass a javascript value in server side
  • Asp controls Id generation inside repeater
  • The name 'Html' does not exist in the current context
  • unable to get intellisense for knockout.js file
  • Android: adding components to RelativeLayout
  • Error inflating class ImageButton
  • Updating product post meta data in admin meta box field
  • Universal Image Loader reuse images
  • Display validation errors inside
  • Hide HTML elements without javascript, only CSS
  • NUnit 3.0 TestCase const custom object arguments
  • Center align outputs in ipython notebook
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Android activity accessing service's static reference before the service is ready
  • Set the selected item in dropdownlist in MVC3
  • D3 nodes and links from JSON with nested arrays of children
  • Switching to Release Build causes runtime error in Web Reference
  • Is calc() supported in html email?
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Submit form in a displaytag pagination
  • javascript inside java/jsp code
  • htaccess rewriting URLs with multiple forward slashes
  • ActionScript 2 vs ActionScript 3 performance
  • Display Images one by one with next and previous functionality
  • To display the title for the current loaction in map in iphone
  • jQuery tmpl and DataLink beta
  • Web-crawler for facebook in python
  • How can I estimate amount of memory left with calling System.gc()?
  • Traverse Array and Display in markup
  • A cron job substitute?
  • How to set the response of a form post action to a iframe source?
  • unknown Exception android
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?