2

I am trying to call a simple JavaScript function on the OnClientClick event, the function executes good in Firefox and IE10 but not at all working in Chrome and opera. I have checked the settings in Chrome V33 and Opera V15 and JavaScript is enabled in it.

This is button declaration in .aspx page

<asp:Button ID="Button2" runat="server" OnClientClick = "getValue()" OnClick="Button2_Click" Text="Label to Textbox" Width="135px" />

This is the button server side onClick event

protected void Button2_Click(object sender, EventArgs e)
{
    string script = "alert(\"Hello!\");";
    ScriptManager.RegisterStartupScript(this, GetType(),"ServerControlScript", script, true);
}

This is the function

document.getElementById('TextBox1').value = document.getElementById('Label2').innerHTML;
alert("foo");

Any assistance will be appreciated, Thank you.

Adam A
  • 14,469
  • 6
  • 30
  • 38
Rahul
  • 1,070
  • 3
  • 21
  • 47

4 Answers4

0

I'm not sure, but it seems there is a conflict between onClientClick() and onClick() functions, it seems IE can resolve it but chrome can't

Plz correct me

"Vinay (above) gave an effective work-around. What's actually causing the button's OnClick event to not work following the OnClientClick event function is that MS has defined it where, once the button is disabled (in the function called by the OnClientClick event), the button "honors" this by not trying to complete the button's activity by calling the OnClick event's defined method."

From OnclientClick and OnClick is not working at the same time?

Community
  • 1
  • 1
stackunderflow
  • 3,811
  • 5
  • 31
  • 43
0

ScriptManager.RegisterStartupScript does not work in Safari/Chrome when used with an updatepanel on a partial page postback

http://codeverge.com/asp.net.ajax-ui/scriptmanager.registerstartupscript-does-not/236335

0

You can try to add just "return null" at the end of the call.

<asp:Button ID="Button2" runat="server" OnClientClick = "getValue(); return null" OnClick="Button2_Click" Text="Label to Textbox" Width="135px" />

Sometimes the browsers get stuck in calls like that expecting a return from the function (also sometimes they don't, I don't know why exactly) and this little trick has been of use for me.

Koalk
  • 59
  • 1
  • 10
  • Ok, if I place return true in the end of my JS function, the client side must have work.??? But before returning anything it must have to get inside the function which is actually not the case. – Rahul Apr 01 '14 at 07:21
  • I have found some problems myself with that kind of functions and I had found two diferent solutions. Put the "return true/null/whatever" in the asignation as I said before. The other solution is to assign the function to the event directly in javascript (asuming you use jquery) `#Button2.onClientClick(function {getValue()})` – Koalk Apr 01 '14 at 07:53
0
document.getElementById('<%=TextBox1.ClientID %>').value = document.getElementById('<%=Label2.ClientID %>').innerHTML;
alert("foo");
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36