473,513 Members | 10,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Register script block from app_code

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

Feb 21 '06 #1
2 6298
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" <si*********@studio-moderna.com> wrote in message
news:ey*************@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


Feb 21 '06 #2
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:eO*************@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" <si*********@studio-moderna.com> wrote in message
news:ey*************@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



Feb 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
14124
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
2
11406
by: Arjen | last post by:
Hello, I want that the client script is added at the and of the HTML output it is now at the beginning. Here is my code: private void Page_Load(object sender, System.EventArgs e) {...
4
3043
by: Cliff Harris | last post by:
Is there a way for me to globally add a javascript block to all pages? I was hoping I could use one of the application events, but I don't seem to have access to the page object to use...
3
2273
by: Goh | last post by:
Hi, May I know how to register a java script inside the <head> tag not the body? I found only got method that able register at the body like RegisterStartupScript and RegisterClientScriptBlock....
8
6329
by: Umut Tezduyar | last post by:
I know that, in asp.net 2.0, the assembly for the web site is splitted into pieces and each time you build it, it generates a random name for assembly. My question is, if i create a custom web...
1
1350
by: lourivas | last post by:
Hi, I create a class with a property called MyProperty that needs to register a client script if the value is set to true. When i change the property value to true i always receive a message...
2
1565
by: teckdaddy | last post by:
Hey all,.. New Kid on da block ,...Need some help with setting up a script to register my customers,...thing is most of the site is backended by yahoo,...and i dont know how to get around that its...
3
7876
by: dixonjm | last post by:
Hi, In my page load event i have:- RegisterClientScripts(); if (!Page.IsClientScriptBlockRegistered("BannerScript")) if (!IsPostBack) { ...
53
8317
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.