How to use ScriptManager.RegisterStartupScript from within a dll

4.3k views Asked by At

In various project I used the following code to open a document in a window, from an aspx page :

public void viewDocument(string strLink, System.Web.UI.Control ctrlPanel)
{
    string strScript;
    strScript = "var w=window.open('" + strLink + "','zz');window.focus();";
    ScriptManager.RegisterStartupScript(ctrlPanel, ctrlPanel.GetType(), "ShowInfo", strScript, true);
}  

and everything is working fine.
I decided to create a toolbox, i.e. a project that contains only functions that can be usefull in many projects. That toolbox is now a dll referenced by the projects.
My problem : my viewDocument function doesn't work anymore. I do not have any javascript error, nothing. In debug mode, I can see that it runs through the "ScriptManager.RegisterStartupScript" command, but no window is opened.
Can someone explain me what I missed ?

1

There are 1 answers

1
VDWWD On BEST ANSWER

Use this snippet. Then you don't need to send System.Web.UI.Control as variable.

public void viewDocument(string strLink)
{
    Page page = HttpContext.Current.CurrentHandler as Page;

    string strScript = "var w=window.open('" + strLink + "','zz');window.focus();";
    ScriptManager.RegisterStartupScript(page, page.GetType(), "ShowInfo", strScript, true);
}