How to identify content page loads in a master page dynamically? - asp.net

I have a master page 'Master1' and i have used a content place holder 'content1' and loads
pages 'Page1' and 'page2' in the 'content1'. Is there any way to identify which page is loaded to the content place holder whether it is 'Page1' or 'Page2' dynamically.

I think maybe you have misunderstood how master pages work?
In you master page you define the guts of your page that you want all pages to share, like headers and footers etc. The you define a content holder:
<asp:ContentPlaceHolder runat="Server" ID="MainContent">
</asp:ContentPlaceHolder>
The contents of Page1.aspx etc will be injected into that ContentPlaceHolder.
Then in your aspx pages you simply define which master page it should use:
<%# Page Language="C#" MasterPageFile="~/MyMasterPage.Master" ... />
and then define the content that should be injected in to the place holder in the master:
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
put all your html in here
</asp:Content>
Note the id used for the ContentPlaceHolderID property, so that it gets injected into the right place - the master page could have several place holders. So both Page1.aspx and Page2.aspx can be loaded in to the place holder, all you have to do is navigate to whichever page you want to display. Nothing needs to be done on the master page.

Try this
ViewContext.RouteData.GetRequiredString("action")

Related

Form tag inside master page and content page

I have one master page and several content pages , I need to put a form tag inside a master page to handle sending data from my html elements and do this for my content pages as well .but I need to understand what is the best way to structure for such this scenario and what would be the effect of form tag of master page on content pages ? is it possible to put form tag in content pages when the master page has this tag inside itself ? I appreciate if I have in detail explanation ?
The <form runat="server"> element lives in the master page by default when you add a new one to your project; all child pages are implemented using ContentPlaceHolders.
For example, the master page: -
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
You can have as many ContentPlaceHolders as you need (often only one is needed though). If you then add a "child page using master page", the page-specific content is added inside the relevant <asp:Content> element - these are added by default once you have specified the master page to use when adding a "child page using master page": -
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<!-- your markup, controls etc.. -->
</asp:Content>
Have a read of the MSDN docs for more - http://msdn.microsoft.com/en-us/library/aa581781.aspx

MaterPage re-design in asp.net. how to create diff layouts for header and footer

I am being told to re-design MasterPage. My company uses AbleCommerce system and let me tell you its very tricky. I have a different layouts templates created in HTML by AbleCommerece long time ago and now i need to re-design my masterpage.
ex:
OneColumn.html
<div id="wrapper">
[[layout:header]]
<div id="Content">
<div id="MainContent">
[[layout:content]]
</div>
</div>
[[layout:footer]]
</div>
RightSidebar.html
[[layout:header]]
<div id="outerContentWrapper">
<div id="innerContentWrapper">
[[layout:content]]
[[layout:rightsidebar]]
</div>
</div>
<div id="footerbar">[[layout:footer]]</div>
MasterPage:
only one ContentPlaceHolder in MasterPage
<asp:ContentPlaceHolder ID="PageContent" runat="server">
</asp:ContentPlaceHolder>
ContentPage
<cb:ScriptletPart ID="ShowProduct" runat="server" Layout="One Column" Header="Standard Header" Content="Show Product Page" Footer="Standard Footer" Title="Show Product" AllowClose="False" AllowMinimize="false" />
Now, Content Page looks for One Coulmn.html and loads the html then One Column Loads the Standered Header.html page which reference header webuser control and layout loads Show Product Page.html page which reference another set of webuser controls and so on..
Issue: by following this design we now have over 100 html files which references Asp.Net UserControls. So whenever we want to create new page on our website, we have to create .aspx and then include **<cb:ScriptletPart>**, create new set of HTML files and then USerControls.
I want to get rid of this system and Load userControls directly inside the .aspx page, that's easy BUT then i dont know how can i inform masterpage to use One Column Layout OR anyother Layout.
Is there way to tell masterpage from content page to use the layout sepcified by public property in content page. OR anyother suggestion to deal this kind of situation.
To access controls within the master page, there are two approaches:
By means of public properties and methods
If you observed, the content page doesn’t have the header and title tags.
So if we want to modify the title of the content page –and this is obligatory- we need to access the title tag.
Primarily we need to change the title tag so it is accessible from the code page.
Simple, we need to add id and runat attributes as follows:
<title id="sometitle" runat="server"></title>
Then, in the master page, add this property:
public string SomeTitle
{
set
{
sometitle.Text = value;
}
}
And finally within the content page, we add this snippet on the Load event.
MasterPage masterPage = MasterPage)this.Master;
masterPage.mainTitle = "Hello World";
Please Note:
Because the Master property returns a reference of type MasterPage, we need to cast it to MasterPage type. We can avoid this by adding the #MasterType directive in the content page as follows:
<%# MasterType TypeName=" MasterPage " %>
MasterPage myMaster =this.Master;
I hope that helps.

ASP.NET Master Page not updating Child Page

I have made a site that is based off of a MasterPage and after creating a few pages I added a new <asp:ContentPlaceHolder> but it seems the existing pages wont update to include it, however new pages do.
Is there a way to force an update, or is there a proper way to do this? I just saved the Master Page and hoped for the best.
You need to add the new <asp:ContentPlaceHolder> to your old aspx pages manually.
in your master page:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="new_one" runat="server"></asp:ContentPlaceHolder>
in your old aspx pages, add:
<asp:Content ID="Content3" ContentPlaceHolderID="new_one" runat="server">
</asp:Content>
If you create a new content placeholder in your master page, your content pages don't NEED to have a corresponding <asp:Content>. They only need one if you want to replace the default content placed in the master page.
And if you're going to update each page with its own content, then adding the <asp:Content> tags is likely a relatively small inconvenience.
Unfortunately there is not a way for Visual Studio to search out all your content pages and update them with new <asp:Content> controls.
However, you could do it fairly quickly by finding using the id of an existing <asp:Content> control, and do a ctrl+shift+F and replace with your new <asp:Content> plus what you searched for.
For example, if your content pages already all had a
<asp:Content ID="Content3" ContentPlaceHolderID="old_one" ... />
then you could search for that and replace with
<asp:Content ID="Content4" ContentPlaceHolderID="new_one" runat="server" /><asp:Content ID="Content3" ContentPlaceHolderID="old_one" ... />
Not exactly pretty, but it could save some time.

How to add master page to already created webform?

I have a asp.net webform application. Now I have to add master page in this application, but I don’t know how to merge or add new created master page with old webforms? How to handle html in webforms like <head> , <body> ? Any link in this regard will be helpful.
1- Define the fixed elements in your design, and put them inside the newly created master page
2- Define the dynamic ones, and add asp:ContentPlaceHolder for them ( most commonly one for HEAD, one for main content in your BODY , and one for side content "if applicable")
<asp:ContentPlaceHolder ID="CphHead" runat="server">
</asp:ContentPlaceHolder>
3- In your pages, add MasterPageFile="~/MASTER_PAGE_PATH" inside the Page directive.
4- Add asp:Content sections inside your pages which will hold the dynamic content in your pages, and don't forget to reference the correct ContentPlaceholder ID.
<asp:Content ID="HeadContent" ContentPlaceHolderID="CphHead" runat="server">
// Your content goes here...
</asp:Content>
5- Copy your page content inside these asp:content sections, and BOOOOM....you are done.
at the top of the new page in the '<%# page #>' tag add 'MasterPageFile="~/Site.Master"'
then add the needed placeholders
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
of course modify these to the names you are using
you can add the content holder tag in master page. So when you add 'MasterPageFile="~/Site.Master"' then you able to add content of other pages.

ASP.NET page content - where does this belong?

I am working on a web application that has a single master page and several content pages. Each page will have a small sidebar to the right of the main content with some brief content. However, that brief content is specific to the page you are on. I can't decide whether to put that on each individual page, or in the master page in a MultiView with some logic in code-behind to specify which view is shown based on which page you are on.
Which seems more elegant? I'm still fairly new with ASP.NET and I'm trying to get a good feel for proper architecture, etc.
You can create multiple content placeholders in a single masterpage. So in your case I would create two. One for the article's content and one for the sidebar like:
<!-- some html-->
<asp:contentplaceholder id="ArticleContents" runat="server">
</asp:contentplaceholder>
<!-- some more html-->
<asp:contentplaceholder id="ArticleSidebar" runat="server">
</asp:contentplaceholder>
<!-- even more html-->
then you could have the article contents and the sidebar contents both in the same page and place it in the correct spot using something like
<asp:Content ID="article" ContentPlaceHolderID="ArticleContents" Runat="Server">
Your article
</asp:Content>
<asp:Content ID="sidebar" ContentPlaceHolderID="ArticleSidebar" Runat="Server">
Your sidebar
</asp:Content>
If you want some manageability in the case of your site getting larger and needing more of these custom sidebars then I would not put anything beyond standard layout in the master page.
What is wrong with having an additional ContentPlaceHolder in the side bar and just adding the content into Content controls on each content page?
Your approach seems overly complex to me.
You could also put that additional code into a user control (or two). The content page would then include the user control appropriate to that page for the right sidebar.

Resources