Monday, March 26, 2012

Want to call a javascript function from code behind

Hi.

I am trying to call a javascript function on button click, buttton is present in update panel. It is not working.

On button click event handler i m writing the following code

Response.Write("<script>functionname();</script>");

I have added script reference in script manager and given the path.

Response.Write("<script>functionname();</script>") --> this works if button is outside update panel.

Help me...

Why not try placing your javascript function name in the button's OnClientClick parameter? That way it calls it directly without requiring a post back...

OnClientClick = "functionname();"


PhunkNugget:

If you do that, which runs first, the javascript in the client or the

click event code in the code behind? may be related, so it's

important to know..I also need to invoke a modal popup

extender from code in a codebehind, not from a click event..

I'm watching this thread closely.. Geeked

Thanks in advance..


The OnClientClick runs first.


Kanwar,

You can't use Response.Write() inside an update panel. It conflicts with the ajax somehow (very annoying). You can attach the script to your button's onclick event in page load such as the following:

string myString="<script language='Javascript'>\n";

myString+="function fuctionname()\n";

myString+="{\n";

myString+="whatever your function does...\n";

myString+=

"}\n";myString+="</script>\n";

Page.RegisterClientScriptBlock(

"MYFUNCTION", myString);

btnMyButton.Attributes.Add(

"onClick","functionname();");

Then when you click on the MyButton, the javascript function will be called.

No comments:

Post a Comment