Click here to Skip to main content
15,897,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ImageButton in MasterPage and it's not firing OnClick Event.
CSS
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/diary1.png"
                      style="z-index: 1; left: 315px; top: 28px;  position: absolute"
                      onclick="ImageButton1_Click" PostBackUrl="~/Default.aspx" />


related onclick event:

XML
<script runat="server">

   

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

        Response.Redirect("Default.aspx");
    }
</script>



Even after googling a lot am not getting it done.
Posted
Comments
Richard C Bishop 29-May-13 15:20pm    
How do you now it is not firing?
praveen iThesisArt 29-May-13 15:24pm    
There is no postback nothing.
Richard C Bishop 29-May-13 15:26pm    
Well your button is set to runat="server". So if there is no code on the server side, nothing will happen. Why do you have "script" tags surrounding your event handler? That code needs to be on the server.
praveen iThesisArt 29-May-13 15:29pm    
even when am having same code in masterpage.master.cs its not working ..
praveen iThesisArt 29-May-13 15:36pm    
Its behavior OnClienClick is also having code : Response.Redirect("Default.aspx");

none is working at all.

1 solution

The problem is with LinkButton.PostBackUrl Property [^].
Quote:
The PostBackUrl property allows you to perform a cross-page post using the LinkButton control. Set the PostBackUrl property to the URL of the Web page to post to when the LinkButton control is clicked. For example, specifying Page2.aspx causes the page that contains the LinkButton control to post to Page2.aspx. If you do not specify a value for the PostBackUrl property, the page posts back to itself.

You need to delete that property in order to post back to the same page.
Otherwise it will post back to the page specified in that property.
 
Share this answer
 
Comments
praveen iThesisArt 29-May-13 15:57pm    
I already once tried deleting that property . Nothing at all is doing any good. If you can see both on my button click and postback i just want to go from one page to another ( ie from page1.aspx to default.aspx) nothing else .
Ideally it should work the way it is detailed in MSDN.
Please check once again. Because putting the PostBackUrl property and onclick event will not work simultaneously.

So, check it carefully, put a debugger on Page_Load and on that ImageButton1_Click event. Check whether it is going to Page_Load or not and then to ImageButton1_Click event.

Let me know what are the findings and what is the next problem. One more thing. Check the rendered html of that image button in browser and post here. I need to check that.
praveen iThesisArt 29-May-13 16:17pm    
rendered image button in browser :
<img id="ctl00_Main_Image10" src="img/diary1.png" style="border-width:0px;z-index: 1; left: 315px; top: 28px; position: absolute" />
Here there is no onclick attribute ? And you said you have written the onclick attribute ?
Where is it ?
praveen iThesisArt 30-May-13 5:18am    
Sir , I really dunno but as you can see am using the code provided earlier but its not getting properly rendered may be.. Am not sure whats wrong with the code.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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