Home » Asp.net

Call javascript function with C#

I don't know if this is the right place to post this kind of problem.

I wonder if it's impossible to call a javascript function in codebehind (C#). I have integrate Ajax on my page and I am using the updatepanel so one part of the page never do postBack. This leads to that my javascript doesn't run. I need to call it in C#.


10 Answers Found


Answer 1

You can't call javascript from inside C#, but if you need to call it after your code runs, you can.


Using the ScriptManager class, I believe the path is ScriptManager.RegisterStartUpScript.  (I say 'believe' because I have an older version of AJAX right now).

The javascript function you'll want to register is "setTimeout('MyFunctionName(param1, param2, etc.)', 100);"  What this does is it tells the page to run that function (MyFunctionName) 100 milliseconds after the page loads. Notice that the function call must be in quotes.

ScriptManager.RegisterStartUpScript(this, this.GetType(), "timeout", functionScript, True);

Answer 2

Sure you can. What you need to do is register your script in the scriptmanager's DataItem list:


this.Page).RegisterDataItem(myControl, myJavaScript);

You will then need to create a client-side script that listens for a response, gets the dataitem and fires the script:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(onLoading); //Hook an eventhandler to the onloading event

// Eventhandler method
onLoading(sender, args) {
     var dataItems = args.get_dataItems(); //Get all dataitems
     if(dataItems[myControlId]) {   //If this dataitem exists
           eval(dataItems[myControlId]);   //Run the script

myControlId is the ClientID of the control you used to register the DataItem with on the serverside


Answer 3

I just discovered a MUCH easier way of doing this:


.RegisterClientScriptBlock(Button1, Button1.GetType(),
"Hello", "alert('Hello World');", true);

where Button1 is a control that must be inside the UpdatePanel that caused the postback.

For some reason scriptblocks will only be registered and sent back to the client for evaluation if they come from a control in the updatepanel. This is not really cool though, since we can have other controls that cause an asyncronous postback, that want to perform the same thing. I guess this was meant for re-initializing controls on the client-side if they got re-rendered by the UpdatePanel.

If you can't live with this limitation, you have to use my first example.


Answer 4


Ok, since I want the script to run directly after the postBack ps2goat's method sounds like a better choice. (I have to much controls that updates the panel).

 I hope I get this right. In the end of a method in my C# codebehind I typed this.

 ScriptManager.RegisterStartupScript(this, this.GetType(), "timeout", "setTimeout('accordioninit()', 100);", true);

My function is accordioninit() and nothing happens. I also tried with alert(). What's wrong? It's like the method doesn't run at all.




Answer 5

It's nothing wrong with the code. I tested it without Ajax and it's works fine. Sadly it doesn't work when I update my content in the updatePanel. I also tried a simple method with a label and then changing the text in the label from serverside to "<script>myfunction()</script>" but it's the same problem, nothing happens. If I wrote the Ajax call by myself I could have added some code to call the function last of all. It must be a simple way to solve this or am I screwed?



Answer 6

ps2goat's method will only work when the page loads (ie. first page load, or full page postback).

You have to use either of my methods to fire the script during an async partial page postback.


Answer 7

I see. Now it works fine. Thanks a lot!

Answer 8


ps2goat's method will only work when the page loads (ie. first page load, or full page postback).

You have to use either of my methods to fire the script during an async partial page postback.


No, my method should work everytime.  I use the one I posted during async post backs.  Let's say a button is pushed, and an error occurs during that code.  I use the setTimeout function to call an alert() box, using the ScriptManager.RegisterStartupScript() function.

If you want to use a function with a control that isn't on the update panel, use Page.ClientScript.RegisterStartupScript(). 

This should work everytime, because you are only calling the setTimeout() function at a certain point, and not every postback.  So, when you do post back, that previous setTimeout() call is not re-registered unless you explicitly register it again.


Answer 9

I should clarify:  By 'work everytime', I mean that whenever the Page is currently posting back, which includes full- and async-post backs. It doesn't need to be the first page load (not from what I've tested). 

If you want to register a static script (i.e., called by a control event such as onchange), you can use the same methodology and add the attribute to the control's event during every Page Load.


Answer 10

thanks for your post .... it solved my problem



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure