ASP.NET page content - where does this belong? - asp.net

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.

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.

Create an ASP.NET user control allowing customizable inner content

I would like to know how to create an ASP.NET user control that allows for inner content (and how to render the inner content.
As a very simple example, let's say we're creating a control to create a div, so the control would be used as follows:
<%# Page Language="C#" %>
<%# Register TagPrefix="custom" TagName="MyDiv"
Src="~\Controls\MyDiv.ascx" %>
<html>
<head />
<body>
<custom:MyDiv id="Div1" runat="server">
... customizable content here ...
</custom:MyDiv>
</body>
<html>
The control should be agnostic to its inner content - i.e. any asp.net content can be added and will be rendered as per usual, and then surrounded with <div> and </div> tags.
(Note: I'm obviously not trying to make a control to simulate div tags, it's just a simple example.)
It's an interesting question. Before I suggest an answer, I think you should look into Master Pages: http://www.asp.net/web-forms/tutorials/master-pages
If that doesn't provide your solution, and you want to stick with the User Control route, it won't work the way you want to do it.

Help in solution more than one form in asp.net as to my logic its compulsory

As per my layout one form(all forms with runat=server) should be in header right top and
the other one I want to place in section's article. So I will have no problem in positioning of contents. How do I solve it?
My design: Login & logout are contained in form at header top-right;
article:Embedding AJAX4 accordian & tab container
but this requires it to be placed in form as <asp:scriptManager should be in form having runat=server.
(article has aside_left & aside_right near to it seperated and header at top seperated which contains the form)
So any Solution for this?
Do I have to change the logic?
I am working with visual studio 2010 asp.net 4 IIS v6 of windows7 x86
If I understand your problem correctly, you can do what you are looking for with Master pages: http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
Create a master page with a content section for the header and another for the rest of the page. The create pages for both of those content sections.
This sounds like you need a MasterPage containing your login and logout buttons, and then a content page containing your 'article'.
Master page (.master)
<form id="form1" runat="server">
<!-- login / logout controls here -->
<asp:ContentPlaceHolder ID="Article" runat="server">
</asp:ContentPlaceHolder>
</form>
Content page (.aspx):
<asp:Content ID="ArticleContent" ContentPlaceHolderID="Article" runat="server">
<!-- Accordion and things here -->
</asp:Content>
If that's not what you're after, then this link provides details on how to implement multiple forms on a page in ASP.NET.

Can content page use ContentPlaceHolderID of master parent of its master page (nested master pages)

I have a 3 level nested master pages and a content page. parent1 is the top parent, parent2 is parent of parent3 and parent3 is the parent of the content page.
I get an error 'Cannot find ContentPlaceHolder xxx...' where xxx is a ContentPlaceholder. It resides in parent2 and content page is trying to fill it.
Can content pages only use their direct parent ContentPlaceHolders or can they also use any of the higher master pages?
There is one way to do this but there is a slight problem with it under certain circumstances if you are relying on any default content from a placeholder.
In your example, you have Parent1.master:
<div id="content">
<h1>Lorem Ipsum, from Parent1</h1>
<asp:ContentPlaceHolder ID="cphContent" runat="server">
<p>I am default content from Parent1...</p>
</asp:ContentPlaceHolder>
</div>
And you also have a nested Parent2.master, which consumes the placeholder from Parent1:
<asp:Content ContentPlaceHolderID="cphContent" runat="server">
<h2>I am some specific stuff from Parent2...</h2>
<asp:ContentPlaceHolder ID="cphContent" runat="server">
<p>I am default content from within Parent2!</p>
<p>We want to create another, nested CPH so that Parent3 can use it!</p>
<p>(It is seemingly OK that we can use the same ID for this CPH<br />
in Parent2 that we did originally in Parent1.)</p>
</asp:ContentPlaceHolder>
</asp:Content>
And so now Parent3.master can consume the placeholder from Parent2. (And also provide another placeholder for eventual content page to consume!) Here it is:
<asp:Content ContentPlaceHolderID="cphContent" runat="server">
<h3>Hello from Parent3!</h3>
<asp:ContentPlaceHolder ID="cphContent" runat="server">
<p>I am more default text in yet another nested placeholder</p>
</asp:ContentPlaceHolder>
</asp:Content>
Your rendered content page would look something like this:
<div id="content">
<h1>Lorem Ipsum, from Parent1</h1>
<h2>I am some specific stuff from Parent2...</h2>
<h3>Hello from Parent3!</h3>
<p>I am the plugged-in content, from the content page!</p>
</div>
One cool thing about this approach, and why we might want to use the same names for these nested CPHs throughout the inheritance chain, is that your eventual content page could change from using any of the Parent master pages 1 through 3 without having to change anything else, so long as they expected to find something called cphContent to consume.
OK, so now you have seen the fun part, but the only thing I mentioned might be a problem, is if you were trying to let any of the "default" text trickle down to any of the grand-children. By this, I mean that if your content page doesn't supply any Content for the "cphContent" placeholder, then only the default from the last master page would be used. The default from Parent1.master is essentially lost beyond Parent2. (Although you could certainly use the default from Parent3.) There may be a way to do this programmatically, but "out of the box" this seems to allow you to do what you asked, if you can live with this caveat.
Best of luck!
I believe content pages can only use the ContentPlaceHolder of the direct parent.
Getting the Values of Controls on the Master Page
At run time, the master page is merged with the content page, so the controls on the master page are accessible to content page code. (If the master page contains controls in a ContentPlaceHolder control, those controls are not accessible if overridden by a Content control from the content page.) The controls are not directly accessible as master-page members because they are protected. However, you can use the FindControl method to locate specific controls on the master page. If the control that you want to access is inside a ContentPlaceHolder control on the master page, you must first get a reference to the ContentPlaceHolder control, and then call its FindControl method to get a reference to the control.
The following example shows how you can get a reference to controls on the master page. One of the controls being referenced is in a ContentPlaceHolder control and the other is not.
Visual Basic Copy Code
' Gets a reference to a TextBox control inside a ContentPlaceHolder
Dim mpContentPlaceHolder As ContentPlaceHolder
Dim mpTextBox As TextBox
mpContentPlaceHolder = _
CType(Master.FindControl("ContentPlaceHolder1"), _
ContentPlaceHolder)
If Not mpContentPlaceHolder Is Nothing Then
mpTextBox = CType(mpContentPlaceHolder.FindControl("TextBox1"), _
TextBox)
If Not mpTextBox Is Nothing Then
mpTextBox.Text = "TextBox found!"
End If
Since you want to Find a nested Content place holder you may have to find the parent then use that instance to find the child

Resources