Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Friends,

I have a web application in visual studio 2008 and using and Image button to save my details,

<asp:ImageButton ID="imgSave" runat="server" ImageUrl="~/images/save.png" onclick="imgSave_Click" />

But the problem is that click event of above button is not fire

C#
protected void imgSave_Click(object sender, ImageClickEventArgs e)
   {
       //save code
   }


But While m using a link button on the same page

<asp:LinkButton ID="lnkSave" runat="server" OnClick="lnkSave_Click"><img src="~/images/save.png" />

C#
protected void lnkSave_Click(object sender, EventArgs e)
   {
       //save code
   }


it is working fine for me. I am not getting what is the issue with image button.

Thanks in advance
Parveen Rathi
Posted

Tried this..?

Add event handler for image button from code behind.

override protected void OnInit(EventArgs e)
        {
           this.imgSave.Click += 
               new System.Web.UI.ImageClickEventHandler(this.imgSave_Click);
            base.OnInit(e);
        }
 
Share this answer
 
v2
 
Share this answer
 
This is a page lifecycle problem.

You're adding the ImageButton to the page (and registering your handler) during the PreRender phase, which occurs after control events have been dispatched. Therefore, your handler will never be called.

Try adding the button during the Load phase, if at all possible.
C#
protected void Page_Init(object sender, EventArgs e)
    {
        ImageButton imgButton;

        imgButton = new ImageButton();

        imgButton.ID = "prgcode";
        imgButton.ImageUrl = "~/images/2.jpg";
        imgButton.ToolTip = "test";

        imgButton.Click += new ImageClickEventHandler(imgButton_click);
        Page.Form.Controls.Add(imgButton);

    }
        private void imgButton_click(object sender, ImageClickEventArgs e)

        {
            Response.Write("adfa");
        }


Please check the following link:
http://www.aspsnippets.com/Articles/Creating-Dynamic-Button-LinkButton-and-ImageButton-in-ASP.Net.aspx[^]

Hope this may help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900