views:

920

answers:

3

I have an updatepanel that has an Image button:

<asp:ImageButton ID="btnChangeMedium" OnClick="Change_Click" runat="server" />

When the page loads, based on some logic, I set the onclick attributes in the code behind.

btnChangeMedium.Attributes.Add("onclick", "ChangeMedium();");

Works greate in IE. In FF, the imagebutton causes a postback! By trial and error and by searching the web, I found the culprit to be the onclick. One blog entry suggested that if the OnClientClick is set, then the postback should not happen, so I changed the imagebutton to:

    <asp:ImageButton ID="btnChangeMedium" OnClick="Change_Click" runat="server"
 OnClientClick="#"/>

I was able to get it to not do a postback on the imagebutton click, but unfortunately, it did not solve my problem fully. The HTML rendered now is:

    <input type="image" id="ctl00_WorkSpaceContent_ctlParent_btnChangeMedium"
src="../../images/sec.gif" onclick="#;ChangeMedium();"/>

So, the ChangeMedium() function is never called. Any workarounds to this problem?

Any help is really appreciated

Thanks!

+1  A: 

What you should do is remove the OnClientClick and put the following:

btnChangeMedium.Attributes.Add("onclick", "ChangeMedium();return false;");
Nissan Fan
If I remove the OnClientClick, in FF, there is a full postback even though the imagebutton is inside an UpdatePanel (this was my original problem)
I had to look at my own code on this. remove the Attributes.Add altogether. Simply put in OnclientClick="ChangeMedium();return false;".
Nissan Fan
The JS that is assigned on OnClick is determined only on the serverside. The ChangeMedium() was an example. It could be any JS code that is determined based on business logic, unfortunately, I can't set it in the HTML :(
Not a problem. Simply create a function that determines the JavaScript:<input type="image" id="ctl00_WorkSpaceContent_ctlParent_btnChangeMedium"src="../../images/sec.gif" onclick='<%=SomeFunction() %>'/>Notice the single quotes I used. Create a function in your code behind as such:public string SomeFunction(){... determine what function appears}Voila. You may need to pass in whatever parameters to the function to determine what JavaScript they get.
Nissan Fan
Unfortunately, that doesn't work either. As soon as I remove the OnClientClick="#", I start getting postbacks when the imagebutton is clicked
A: 

Try this:

 <asp:ImageButton ID="btnChangeMedium" OnClick="Change_Click" runat="server"
 OnClientClick="javascript:void(0);ChangeMedium();"/>
rick schott
+1  A: 

I am having a similar issue where defining the OnClientClick="#" does not make a difference. I consistently getting multiple postbacks in FF while only one in IE. I do not have any triggers defined on the update panel either.

Roma