Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET inject javascript in user control nested in update panel

I'm trying to load javascript code with a user web control into a page via a the Page.LoadControl method during an asyncron post back of an update panel.

I've tried the specially for that scenario designed methods of the scriptmanager, but the javascript just doens't get returned to the user.

To explain my scenario a bit better:

Master Page has the script manager and one page loads the user control via Page.LoadControl-method during an async post back. The custom control injects in the pre-render event handler the javascript. Is that a matter of timing to inject the js or is it just not possible to do so?

like image 959
derSteve Avatar asked Nov 19 '08 10:11

derSteve


2 Answers

For that you can do

string scr;
scr = "<script src='/scripts/myscript.js'></script>"
Page.ClientScript.RegisterStartupScript(GetType(Page), "key", scr, false)

HTH

like image 196
rams Avatar answered Oct 17 '22 22:10

rams


If you don't want to hard-code your JavaScript, but instead include it from a file, call ScriptManager.RegisterClientScriptInclude and then call your initialization function in ScriptManager.RegisterStartupScript.

protected void Page_Load(object sender, EventArgs e)
{
   ScriptManager.RegisterClientScriptInclude(
      this, GetType(), "formatterScript", ResolveUrl("~/js/formatter.js"));
   ScriptManager.RegisterStartupScript(
      this, GetType(), "formatTableFunction", "formatTable()", true);
}
like image 3
Even Mien Avatar answered Oct 18 '22 00:10

Even Mien