using page-object in .cs-file

A

Alexander Widera

I have a file "file1.cs" with a method:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public static void justdoit()
{
string script = "<script language=\"JavaScript\"> alert("it works");
</script>";
Page.RegisterStartupScript("frreload", script);
}



If I use this method directly in an aspx-page it works.... But now the
problem: If I write this Method in a seperate file, because I want to re-use
this method in other aspx-pages, I get an error while compiling the file: "
error CS0120: An object reference is required for the nonstatic filed,
method, or property 'System.Web.UI.Page.RegisterStartupScript(string,
string)'

Where can I get the current "page-object", so that I can use the function
RegisterStartupScript ?

Thanks for help.
 
K

Karl Seguin

I believe your only option is to pass the page in as a parameters


public static void justdoit(Page page){
string script = "...";
pageRegisterStartupScript("fireload", script);
}
 
K

Kevin Spencer

It helps to think in terms of classes rather than files. They are only files
as far as you're concerned, and only when you're writing code. To the app.,
they are classes, and they behave like classes. Sometimes all it takes to
see something clearly is to think of it in the right way.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
M

Matt Berther

Hello Alexander,

public static void JustDoIt()
{
string script = @"
<script language='javascript'>
alert('it works');
</script>";

Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
page.RegisterStartupScript("frreload", script);
}
}
 
A

Alexander Widera

thank you very much
Matt Berther said:
Hello Alexander,

public static void JustDoIt()
{
string script = @"
<script language='javascript'>
alert('it works');
</script>";

Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
page.RegisterStartupScript("frreload", script);
} }
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top