The same code runs fine in my App_Code class. Did you try to make the instance of your class before accessing the method Alert (since the method is not static, you cannot access it via the class name like MyCls.Alert)? See my sample code below:
Thanx a lot.....your answer made me go through my code again and see where I was going wrong. Actually "MyCls" was an instance of my app_code class so I was really confused when you said the same code is running fine with you. The problem was with the declaration
of the Page object. I declared it without using the "public" keyword (since the declaration is outside the method). When I declared it like
public Page page = HttpContext.Current.Handler
as
Page;
it works. It was a silly error really. Thanx again for your time and effort.
ok I think I need a fox-nap now...my head's spinning [:S]
sorry about my last post.....fatal error....public isn't public anymore (I dunno how or why it worked the first time)
the declaration ---------
Page page = HttpContext.Current.Handler
as Page;
is working only within the method. Can anyone explain this "always eager to learn" fox how to declare an object in a class and access it from a method...without it being referenced as "null".
I think I do not understand your point here. You do not need to declare variables as public if you want to access them inside the class only. What exactly is the error you are getting? If possible, try to paste the class source code and also how you are
calling it inside the Page.aspx.cs.
I am able to call that method from the app_code class. Below is the sample source code:
DevFox
Member
60 Points
15 Posts
RegisterStartupScript Problem in app_code class
Sep 28, 2006 11:54 AM|LINK
Hi,
Here's my code for running the client script:
*************************************************************************************************
string Script = "<script language='javascript'>alert("This here is an alert")</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(),"Alert", Script);
*************************************************************************************************
It works fine when I use it as is....but when I try to call a method from an app_code class or a dll like this:
*************************************************************************************************
MyCls.Alert("This here is an alert");
My code in app_code class :
Page page = HttpContext.Current.Handler as Page;
public void Alert(String alert)
{
string Script = "<script language='javascript'>alert('" + alert + "')</script>";
page.ClientScript.RegisterStartupScript(this.GetType(),"Alert", Script);
}
*************************************************************************************************
I get this error: Object reference not set to an instance of an object.
I know it has something to do with the Page object but I can't seem to figure it out.
Can anyone help please.
Fox
vivek_iit
All-Star
17778 Points
3189 Posts
MVP
Re: RegisterStartupScript Problem in app_code class
Sep 28, 2006 12:54 PM|LINK
Hi,
The same code runs fine in my App_Code class. Did you try to make the instance of your class before accessing the method Alert (since the method is not static, you cannot access it via the class name like MyCls.Alert)? See my sample code below:
Class1 c = new Class1(); //my app code class
c.Alert("This works");
Hope this helps,
Vivek
Communifire: Social Networking and Business Collaboration Platform
DevFox
Member
60 Points
15 Posts
Re: RegisterStartupScript Problem in app_code class
Sep 30, 2006 08:16 AM|LINK
Hi Vivek,
Thanx a lot.....your answer made me go through my code again and see where I was going wrong. Actually "MyCls" was an instance of my app_code class so I was really confused when you said the same code is running fine with you. The problem was with the declaration of the Page object. I declared it without using the "public" keyword (since the declaration is outside the method). When I declared it like
public Page page = HttpContext.Current.Handler as Page;
it works. It was a silly error really. Thanx again for your time and effort.
Fox
DevFox
Member
60 Points
15 Posts
Re: RegisterStartupScript Problem in app_code class
Sep 30, 2006 09:20 AM|LINK
ok I think I need a fox-nap now...my head's spinning [:S]
sorry about my last post.....fatal error....public isn't public anymore (I dunno how or why it worked the first time)
the declaration ---------
Page page = HttpContext.Current.Handler as Page;
is working only within the method. Can anyone explain this "always eager to learn" fox how to declare an object in a class and access it from a method...without it being referenced as "null".
mooo.....oops
fox
vivek_iit
All-Star
17778 Points
3189 Posts
MVP
Re: RegisterStartupScript Problem in app_code class
Sep 30, 2006 03:58 PM|LINK
Fox,
I think I do not understand your point here. You do not need to declare variables as public if you want to access them inside the class only. What exactly is the error you are getting? If possible, try to paste the class source code and also how you are calling it inside the Page.aspx.cs.
I am able to call that method from the app_code class. Below is the sample source code:
public class Class1
{
public Class1()
{}
Page page = HttpContext.Current.Handler as Page;
public void Alert(String alert)
{
string Script = "<script language='javascript'>alert('" + alert + "')</script>";
page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", Script);
}
}
ASPX.CS Code:
protected void Page_Load(object sender, EventArgs e)
{
Class1 cs = new Class1();
cs.Alert("Hi");
}
Let me know if this does not work at your end.
Regards,
Vivek
Communifire: Social Networking and Business Collaboration Platform
DevFox
Member
60 Points
15 Posts
Re: RegisterStartupScript Problem in app_code class
Oct 01, 2006 07:32 AM|LINK
hi vivek...my code is exactly like yours.....but mine works only if I include the "page" declaration within the method....else its throwing:
"Object reference not set to an instance of an object." When I debug the code...I see that the page object is "null".
As you can see below I have to declare the page object in every method...
public void Popup(String PageName, String FrameName, int width, int height, String menubar, String resizable)
{
string popupScript = "window.open('" + PageName + "', '" + FrameName + "', " +
"'width=" + width + ", height=" + height +
", menubar=" + menubar + ", resizable=" + resizable + "')";
Page page = HttpContext.Current.Handler as Page;
page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", popupScript,true);
}
public void Alert(String alert)
{
string Script = "alert('" + alert + "')";
Page page = HttpContext.Current.Handler as Page;
page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", Script,true);
}
Now everythings running...but my question is how can I declare "page" once in the class and use it in more than one method.
Thanx
Fox
vivek_iit
All-Star
17778 Points
3189 Posts
MVP
Re: RegisterStartupScript Problem in app_code class
Oct 03, 2006 10:45 AM|LINK
Send me your code (if possible) as I have it working at my end. My mail ID is vivekthakur (at) gmail.com.
Vivek
Communifire: Social Networking and Business Collaboration Platform
vivek_iit
All-Star
17778 Points
3189 Posts
MVP
Re: RegisterStartupScript Problem in app_code class
Oct 03, 2006 10:49 AM|LINK
Send me your code (if possible) as I have it working at my end. My mail ID is vivekthakur (at) gmail.com.
Vivek
Communifire: Social Networking and Business Collaboration Platform