I want to make page Load function in a class. in fact I don't want to use code behind, I want to create a class and define all of my functions (like page load and button clicks ,...) in it .
how I do it?
You can make your class inherit from System.Web.UI.Page, and set your aspx file's Page directive to inherit from your class.
But that's basically the same thing as having a code behind page.
Try ASP.NET webforms MVP pattern that allows to do it more object oriented way but you still need to handle page_events. If you need completely stateless, use ASP.NET MVC.
Create a class say pagebase and inherit you codebehind page with this class now the class code is below.
public class PageBase :System.Web.UI.Page
{
public PageBase ()
{
//
// TODO: Add constructor logic here
//
}
protected override void OnLoad(System.EventArgs e)
{
CheckSecurity();//any function you want to call before
base.OnLoad(e);
}
public virtual void CheckSecurity()
{
//your logic here
}
}
Now this class load function will run before the codebehind page page load runs. Let me know if you have any questions
Related
My web form contains a web control and a asp:HiddenField. I am trying to access that hiddenfield in my asxc.cs file. I defined a public get,set block in my aspx.cs file. In web control, when I tried to call ReportPage.TestID, it does not recognize the Reportpage class. Is it the right way to access the HiddenField in the webcontrol? If so, how should I access the ReportPage class?
public partial class ReportPage : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int TestID
{
get
{
return Convert.ToInt32(TestIDHiddenField.Value);
}
set
{
TestIDHiddenField.Value = TestID.ToString();
}
}
}
You cannot directly access page controls from a user control because of Web Site compilation model. In short, ASP.NET compiles first App_code (its classes are visible from all the site) then user controls (.ascx) and when .aspx pages which use that controls.
A workaround looks like this:
In App_code create an abstract class (MyBasePage for example) which inherits from Page class and add abstract property (TestID in your example),
Create a page which inherits from MyBasePage and implement the property,
In .ascx.cs cast this.Page as MyBasePage and use the property you want.
Is it possible within my code-behind file to set the asynchronous mode of the page directive.
I have no way of directly modifying the <%#Page %> attribute and and struggling to find a way to implement this in my code-behind.
I have tried in my Page_Load method to add Page.AsyncMode = true, but it returns the following error:
is inaccessible due to its protection level
Is there any way to do this? Without being able to directly modify the master page?
No, you cannot change the asynchronous mode of a page in the code-behind. An asynchronous page implements the IHttpAsyncHandler interface, and there is no way to change the interfaces implemented by your page after the .aspx file has been compiled by ASP.NET and your code is running.
Setting the Page.AsyncMode property will not change the asynchronous mode. Its purpose is to let controls on the page know whether the page is running in asynchronous mode or not, so tampering with the property may cause controls to malfunction.
My guess - you're trying to access this property from your master page. But according to documentation, this property is protected bool AsyncMode { get; set; }. Which means it is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
This property is declared in System.Web.UI.Page and is accessible in it and any class derived from it. MasterPage doesn't derive from Page. That's why you cannot access it.
You can easily access it from your page:
public partial class YourPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.AsyncMode = true;
}
}
Because you're MasterPage does not Inherit from your Page, you cannot access the AsyncMode Property.
If you absolutely must edit the value from your MasterPage maybe consider adding a method to your page called "UpdateAsyncMode" and doing the following from your masterpage Page_Load
protected void Page_Load(object sender, EventArgs e)
{
MyPageClass p = this.Page as MyPageClass ;
p.UpdateAsyncMode(true);
}
Alternatively if this is something that needs to be more robust you could create a base class for Pages like the following and have all the web pages in your site extend that base class
public abstract class MyBasePage : System.Web.UI.Page
{
public void UpdateAsyncMode (bool b)
{
this.AsyncMode = b;
}
}
on asp.net C#,
i want to change the theme when page is load, this action requiers PreInit Event, Which Masterpage dont have. what is the solution for that issue?
thanks,
One way is to create an Http Module (Rick van den Bosch blogs)
Aside from an Module, you could consider having all your pages inherit from a base page that defines PreInit and sets the theme accordingly.
Edit for comment.
There isn't all that much to it, just make sure the pages you create inherit from a class similar to the BasePage class below instead of the default Page class and you should be set.
public class BasePage : Page
{
public BasePage()
{
this.PreInit += new EventHandler(BasePage_PreInit);
}
protected void BasePage_PreInit(object sender, EventArgs e)
{
this.Page.Theme = theme; //Garner from appropriate resource
}
}
Hope that helps.
I have a.master and b.aspx .
i have some functions in my aspx page.
how to access that functions in a.master page.
thank you
Let's say, you want to call Foo from b.aspx from a.master. So first thing is that you have make the method internal (or public) and then you can use code such as below in master page is call that method.
var page = (b)this.Page;
page.Foo();
Note that b will be the code behind class name in b.aspx. Note that above code will fail if you use another page c.aspx and use the same master a with it. Generally, I will say that invoking page specific functions from master does not make sense unless functions are present in some base page class and in such case you should be casting to that base page class.
Edit: More elaborate example as requested by Asif:
Consider your content page b.aspx such as
<%# Page Language="C#" MasterPageFile="a.Master" Title="Page B" AutoEventWireup="true"
CodeBehind="b.aspx.cs" Inherits="YourProject.b" %>
And in code behind file (b.aspx.cs), you have a method Foo such as
namespace YourProject
{
public partial class b : System.Web.UI.Page
{
void Foo(string someParameter)
{
Label1.Text = someParameter
}
...
}
}
Now in code behind (a.master.cs) of a.master page
namespace YourProject
{
public partial class a : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
b contentPage = (b)this.Page;
contentPage.Foo("Hello");
}
....
}
}
Of course you can make the method in b.aspx be public to call it in a.master. However I suggest you consider your design carefully. Because it's really weird just like that you call a method of a child class from its parent class (even though it's theoretically possible). Before your modification, ask yourself:
Is it necessary to call this method in the master page? If yes, do I have a better place to put the method?
As others have said, it's possible to do this. However, it's an odd way of doing things. You are probably going to be better off doing whatever you want to do in a different way. The whole idea of a master page is that it "wraps" many kinds of content pages. What if you content page doesn't have the function you want to call?
You could make sure all your content pages have the function, but then why not just put it in the master page?
Perhaps if you descired what you wanted to do a little better, we could advies you on a better way to handle things.
To access, either:
Make that method a static method.
Move your code in App_Code folder.
Move your code out of your web project, into some generic assembly and use that as a reference.
I need to create a code that reads the QueryString and set a value on the Session and on the end of the page I need to clear the Session.
How can I make a code like this to run on all .aspx pages?
Two possibilities are:
Create a class that inherits from System.Web.UI.Page. Insert your code there and all your pages inherit from that class instead of System.Web.UI.Page.
Create a HttpModule
well as i see it you got 2 solutions
Use a master page and do it there
Inherit Page and use that as base class on all your pages
One question why must it be stored in a session? this will give your problems if the same user executes 2 pages at the same time (the first to finish will clear the sesson for the other page)
if you only need the data while the page runs you can just save it in a normal variable, else use the viewState!
Or just use Global.asax: http://en.wikipedia.org/wiki/Global.asax
Create a class that inherit from Page that you will use instead of Page.
Alternatively, you can use a MasterPage if your application design allows that.
By putting the code in a basepage and letting all your .aspx pages inherit from the basepage.
An easy way to include code that is part of all pages is to subclass Windows.Web.UI.Page, and have all your pages inherit from this new class, instead of Windows.Web.UI.Page. The new class can register for page events, independently from each individual page.
Another option, if you don't want it to be part of each page, and you want to ensure that it runs even if a developer doesn't inherit your new page subclass, is to write an HTTPModule. This hooks into the ASP.NET processing pipeline, and you can trigger off pipeline events, such as authentication or displaying pages. You can write an HTTPHandler and run it out of the pipeline as well. (All pages implement IHTTPHandler, somewhere up the chain.)
I used the method of writing a class to inherit from System.Web.UI.Page. However, I did not find the implementation of this method to be obvious. Here is the code that I eventually wrote:
public class BasePage : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Code here will be run when any page loads that inherits from BasePage
}
}
}