Register script block from app_code

S

SimonZ

I have function in my app_code folder.

Is it possible to register client script block from app_code function?

public static void CheckSessionModal()

{

String scriptValue="";

scriptValue = "<script language=\"vbscript\">" + Environment.NewLine +
"window.returnValue=-1"+

"window.close"+Environment.NewLine+"</script>";

Page.ClientScript.RegisterStartupScript(GetType(), "scriptName",
scriptValue);

}



From every page in my program I call this function from page_onload event:

functions.CheckSessionModal()

But when I do this, I get an error message:

Error 2 An object reference is required for the nonstatic field, method, or
property 'System.Web.UI.Page.ClientScript.get'
c:\inetpub\wwwroot\CP\App_Code\functions.cs 452 13 http://localhost/CP/

Error 3 An object reference is required for the nonstatic field, method, or
property 'object.GetType()' c:\inetpub\wwwroot\CP\App_Code\functions.cs 452
53 http://localhost/CP/

How can I do that, any idea?

Thank you,

Simon
 
K

Karl Seguin [MVP]

Probably because "Page" doesn't exist, you can access it via code something
like:

if (HttpContext.Current == null)
{
throw new Exception("Method must be called from a page context");
}

Page page = HttpContext.Current.Handler as Page;
if (page == null)
{
throw new Exception("Method must be called from a page context");
}

you now have access to a "page" variable

Karl
 
S

SimonZ

when I call function, I add page and type as a parameter:

functions.CheckSessionModal(GetType(), Page)

It works.

But I guess, your way is better. Thanks,

Simon




"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top