Home All Groups Group Topic Archive Search About

Register script block from app_code

Author
21 Feb 2006 8:58 AM
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

Author
21 Feb 2006 12:21 PM
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

Show quoteHide quote
"SimonZ" <simon.zu***@studio-moderna.com> wrote in message
news:eyQegTsNGHA.964@tk2msftngp13.phx.gbl...
>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
>
>
>
>
>
Are all your drivers up to date? click for free checkup

Author
21 Feb 2006 1:56 PM
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
Show quoteHide quote
net> wrote in message news:eOJNZFuNGHA.456@TK2MSFTNGP15.phx.gbl...
> 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
>
> --
> http://www.openmymind.net/
>
>
>
> "SimonZ" <simon.zu***@studio-moderna.com> wrote in message
> news:eyQegTsNGHA.964@tk2msftngp13.phx.gbl...
>>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
>>
>>
>>
>>
>>
>
>

Bookmark and Share