Setting Page Async mode to true from Code-Behind - c#

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;
}
}

Related

web control access web form methods

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.

defining page load function in classes in asp.net

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

How can I set MasterPageFile for MasterPage dynamically? [duplicate]

Okay, so we all know about changing a master page dynamically in a page's OnPreInit event.
But what about a nested master page? Can I change a master's master?
There is no OnPreInit event exposed in the MasterPage class.
Any ideas?
Just tested this and it works from the PreInit of the Page that is using the nested MasterPage.
protected void Page_PreInit(object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}
Obviously you will need to ensure that the ContentPlaceholderIds are consistent across the pages you are swapping between.
We combine Andy's method with a "BasePage" class - we create a class that inherits from System.Web.UI.Page, and then all our pages inherit from this class.
Then, in our base page class, we can perform the relevant checks to see which root master page should be used - in our case we have a "Presentation" master, and an "Authoring" master - the presentation version has all the navigation and page furniture, along with heavy display CSS, while the authoring master has some extra JS for the authoring tools, lighter CSS, and no navigation (it's what we use when the user is actually authoring a page, rather than modifying the site layout).
This base page can then call Page.Master.MasterPageFile and set it to the Authoring master if that is the correct state for the page.
Just in case anyone stumbles across this and tears their hair out with a "Content controls have to be top-level controls in a content page or a nested master page that references a master page" error when trying Andy's code, get rid of the this.Master. So, the code becomes:
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "/Site2.Master";
}
Edit As Zhaph points out below, the code I have ^^ there will only change the current page's master, not the master's master. This is the code Hainesy was talking about when he mentioned "we all know about changing a master page dynamically" (which I didn't, d'oh). If you happen to get to this page by googling "stackoverflow change master page" (which is what I did) then this is possibly the code you're looking for :-)
To add on to the answer of Zhaph - Ben Duguid, (+1 by the way):
Here is example code that sets the master page of the nested master page. All pages inherit from this BasePage, so this code only exists in one place.
public class BasePage : System.Web.UI.Page
{
private void Page_PreInit(object sender, System.EventArgs e)
{
if (Request.Browser.IsMobileDevice)
{
if (Page.MasterPageFile == "~/master/nested.master"))
{
Page.Master.MasterPageFile = "~/master/mobile.master";
}
else
{
MasterPageFile = "~/master/mobile.master";
}
}
}
}

MasterPage Load Theme

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.

access function in aspx page to master page

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.

Categories

Resources