-4

Possible Duplicate:
Calling ASP.NET Code Behind function from Javascript
using javascript function to be called from code behind under specific condition

this is a javascript function.

     function openWinContentTemplate() {
     $find("RadWindow_ContentTemplate").show();
     }

I want to call this javascript function from code behind on button click under condition as:

    protected void button_click()
    {
     if(true)
        //call javascript function here
    }
Community
  • 1
  • 1

2 Answers2

2

You can't call in codebehind. Codebehind is on the server, and your script runs on the client. Use the RegisterStartupScript in your codebehind to invoke the script when the browser gets the response.

groundh0g
  • 408
  • 3
  • 9
1

you can try like this ....

You can call Js functions as a script registered on the codebehind itself

Page.RegisterStartupScript("key","value")

key is the name you want to give the script

Example

"PageClose"

value is the

stringBuilder str = new StringBuilder()
str.Append("<script = language='javascript'>");
str.Append("window.close();");
str.Append("</script>")

here instead of using the window.close you coud append your js function as a string, ideally i put this string builder class and build the script in the constructor if i need it always in the page.

Then use this in the event handler you want to execute the script

Page.RegisterStartUp("PageClose",str.ToString());

This would place the javascript before the closing tag of the page thats rendered

Page.ClientScriptBlock("PageClose",str.ToString());

This would place the JS function after the opening tag of the page thats rendered

Hope this helps

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • its not working .Page.RegisterStartUp("PageClose",str.ToString()); This is not working as Page.RegisterStartUp doesnot work as there is not such method. – smriti Chandhok Nov 13 '11 at 06:22