Page-wide state with Master pages?
-
I am a bit late to VS 2005. Today I am looking at ways to rebuild one of our websites in .NET 2.0. Rather than convert I am recreating pages as I go. This isn't an issue because they need to be reworked anyway. I opted against using the VS 2005 Web Applications Project tools to emulate 2003 in 2005 and don't want to consider that option. This site is going to be redone as a new site would. That being said... Our current site is made of of pages derived from a BasePage. They host user controls derived from BaseControl. Using these 2 base classes we have every page automatically check user authentication, redirection, and other housekeeping. This is done entirely on BasePage and is available to BaseControls. If that is unclear, when a user accesses a page the BasePage checks several sources for user authentication (Session,Cookies,Database). If not correct, the user is redirected to a SignIn or AccessDenied page. If correct, the user's state is loaded for the page. From there all components on the page can query the state and populate their contents. How would I make this work for a MasterPage setup? The master page (as I see it) will host my UserControls directly (header, footer, side-menu). These user controls need access to the user state. The hosted content pages will also need to access the same state. I don't want each separate component hitting the database loading bits and pieces. Basically:
Request page Page is assembled Check for security If needed, redirect Otherwise, load state for page Header populates with state info Side-menu populates based off state info Content page populates based off state info Send page to client
Something tells me that I am not quite "getting it". Am I supposed to load up the Session with huge state objects? I wouldn't think so. Too many simultaneous users for that. Can I just bulk up the MasterPage to do everything? I am lost. -
I am a bit late to VS 2005. Today I am looking at ways to rebuild one of our websites in .NET 2.0. Rather than convert I am recreating pages as I go. This isn't an issue because they need to be reworked anyway. I opted against using the VS 2005 Web Applications Project tools to emulate 2003 in 2005 and don't want to consider that option. This site is going to be redone as a new site would. That being said... Our current site is made of of pages derived from a BasePage. They host user controls derived from BaseControl. Using these 2 base classes we have every page automatically check user authentication, redirection, and other housekeeping. This is done entirely on BasePage and is available to BaseControls. If that is unclear, when a user accesses a page the BasePage checks several sources for user authentication (Session,Cookies,Database). If not correct, the user is redirected to a SignIn or AccessDenied page. If correct, the user's state is loaded for the page. From there all components on the page can query the state and populate their contents. How would I make this work for a MasterPage setup? The master page (as I see it) will host my UserControls directly (header, footer, side-menu). These user controls need access to the user state. The hosted content pages will also need to access the same state. I don't want each separate component hitting the database loading bits and pieces. Basically:
Request page Page is assembled Check for security If needed, redirect Otherwise, load state for page Header populates with state info Side-menu populates based off state info Content page populates based off state info Send page to client
Something tells me that I am not quite "getting it". Am I supposed to load up the Session with huge state objects? I wouldn't think so. Too many simultaneous users for that. Can I just bulk up the MasterPage to do everything? I am lost.First of all, why not use the "VS 2005 Web Applications Project template"? I use it all the time. I like compiling the apps before deployment. Now about your design. I see no reason to put a user control in the Master Page. Why would you do that? A User Control is something designed to be used on multiple pages, the master page is on every page. So why would you have user controls in a master page? Checking authentication in the master page is fine, then redirect to another page not in the Mater Page. Note you can have more then one master page. I think what you are asking is how to access controls in the master page from your pages and vice versa. Let's say you wanted to handle an event from a control in the Master page on your base page, you would do the following.
ddl = (DropDownList)this.Master.FindControl("ddlMain"); ddl.SelectedIndexChanged += new EventHandler(ddlMain_SelectedIndexChanged); protected void ddlMain_SelectedIndexChanged(object sender, EventArgs e) { //put code here }
Now if you want to access anything in your master page as long as it is not "private"YourApp.Site objMasterPage = YourApp.Site; objMasterPage.CallPublicMethod();
-
First of all, why not use the "VS 2005 Web Applications Project template"? I use it all the time. I like compiling the apps before deployment. Now about your design. I see no reason to put a user control in the Master Page. Why would you do that? A User Control is something designed to be used on multiple pages, the master page is on every page. So why would you have user controls in a master page? Checking authentication in the master page is fine, then redirect to another page not in the Mater Page. Note you can have more then one master page. I think what you are asking is how to access controls in the master page from your pages and vice versa. Let's say you wanted to handle an event from a control in the Master page on your base page, you would do the following.
ddl = (DropDownList)this.Master.FindControl("ddlMain"); ddl.SelectedIndexChanged += new EventHandler(ddlMain_SelectedIndexChanged); protected void ddlMain_SelectedIndexChanged(object sender, EventArgs e) { //put code here }
Now if you want to access anything in your master page as long as it is not "private"YourApp.Site objMasterPage = YourApp.Site; objMasterPage.CallPublicMethod();
Thanks for the reply. It, with the information I've gathered today sets me on the right course. You are absolutely right about the UserControl on the MasterPage. I got caught in old thinking vs. new thinking on that one. In .NET 1.1 I created an ASCX for the header, footer and menu becaues they actually were being created on every page dynamically. I just carried that idea forward. Thanks for setting me right. I won't be doing any events across boundaries like your example but I may in the future and now I'll know. Thanks again. And the YourApp.Site.Method was another piece that I was absolutely needing. I was going to be requiring all content pages to do something like ((MyMasterPage)this.Master).CallMethodHere. Many, many thanks. :)