Include Content in .aspx masterpage - asp.net

In my current project we have 5 different masterpages, there are some common elements in each and its really annoying making the change in all 5, it kind of defeats the point of masterpages.
I have tried having parent and child master pages but that caused other problems for a different day.
Is there a way to include dynamic content in a masterpage?
I'm looking for something similar to the php and coldfusion include().

You can put user controls (.ASCX) in your master pages. Is this what you were attempting to accomplish?
Like so...
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="WebForms.master.cs"
Inherits="Tunafish.Web.Views.Shared.WebForms" %>
<%# Register Src="~/Content/Controls/SiteNavigation.ascx" TagName="Nav"
TagPrefix="sc" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
<sc:Nav runat="server" />

Have you looked into User Controls? .ascx

Custom web controls are the way to go:
http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx

As mxmissile and JMP suggested, user controls are the way to go, but you might want to be thorough in your usage of them. When you include the master page, make sure you add the following markup along with the page declaration:
<%# MasterType
virtualpath="~/myMasterPages/Master.master"
%>
This will allow you to call functions/objects in your master pages so you can make changes to controls or have access from the page itself to various other objects. I have a property in my base usercontrol class called "ParentForm" that is a reference to the page it sits in. For user controls in the master page, I ended up having the same property and in the setter of that property I translate it down to the user controls.

You could set the masterpages to inherit from a class that dynamically inserts content or script to the page OnPreRender in code. It may seem out there but I have had to use this method.

Related

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.

ASP.Net <%# %> and <%= %> rules?

Can someone explain to me the rules around what can and cannot be evaluated/inserted into markup using the <%# %> and <%= %> tags in asp.net?
When I first discovered I could inject code-behind variables into mark-up using <%= I thought 'great'. Then I discovered that if such tags are present you can then not add to the controls collection of the page (that's a whole different question!). But <%# tags are ok.
Is there a way I can inject a code-behind variable or function evaluation into the page using <%# ? Thanks.
<%%> are code blocks. You can put any server side code in them. This is a shortcut for <script runat="server"></script>.
<%=%> is for outputting strings. This is a shortcut for <script runat="server">Response.Write()</script>.
See here for more detail about <%%> and <%=%>.
<%#%> are used for data binding expressions.
See the index page for asp.net Page syntax.
The <%# inline tag is used for data binding, so if you want to use a code-behind variable inside it, you will have to bind the page or control the variable resides in:
Page.DataBind();
You can include this statement in the Page_Load or Page_PreRender event.
See this article for more information about the use of inline tags in ASP.Net, and this article for more about server-side databinding.

a page can have only one server-side form tag

I am using master page and when I run this page, it shows the following error message:
a page can have only one server-side form tag
How can I solve this problem?
I think you did like this:
<asp:Content ID="Content2" ContentPlaceHolderID="MasterContent" runat="server">
<form id="form1" runat="server">
</form>
</asp:Content>
The form tag isn't needed. because you already have the same tag in the master page.
So you just remove that and it should be working.
It sounds like you have a form tag in a Master Page and in the Page that is throwing the error.
You can have only one.
Does your page contain these
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
</asp:content>
tags, and are all your controls inside these? You should only have the Form
tags in the MasterPage.
Here are some of my
understanding and suggestion:
Html element can be put in the body of html pages and html page does
support multiple elements, however they can not be nested each
other, you can find the detailed description from the W3C html
specification:
The FORM element
http://www.w3.org/MarkUp/html3/forms.html
And as for ASP.NET web form page, it is based on a single server-side form
element which contains all the controls inside it, so generally we do not
recommend that we put multiple elements. However, this is still
supported in ASP.NET page(master page) and I think the problem in your
master page should be caused by the unsupported nested element, and
multiple in the same level should be ok. e.g:
In addition, if what you want to do through multiple forms is just make our
page posting to multiple pages, I think you can consider using the new
feature for cross-page posting in ASP.NET 2.0. This can help us use button
controls to postback to different pages without having multpile forms on
the page:
Cross-Page Posting in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/lib...39(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...40(VS.80).aspx
Use only one server side form tag.
Check your Master page for <form runat="server"> - there should be only one.
Why do you need more than one?
Sometime when you render the current page as shown in below code will generate the same error
StringWriter str_wrt = new StringWriter();
HtmlTextWriter html_wrt = new HtmlTextWriter(str_wrt);
Page.RenderControl(html_wrt);
String HTML = str_wrt.ToString();
so how can we sort it?
please remove " runat="server" " from "form" tag then it will definetly works.

Proper use of MasterPages

I've been looking into different methods of implementing masterpages.
Use the masterpage only for layout, include common controls on every page
Include controls on the masterpage, use a masterpage abstract base class and override it's properties in the masterpage class. This caused the masterpage events to no longer wire up. I could probably fix this, but it's a long way to go just for a single textbox value.
use good 'ol Page.Master.FindControl()
I've read that findcontrol should be avoided (uses magic "label1" strings, supposedly uses too many resources) and masterpages are only for layout. If masterpages are only for layout, do I copy and paste common controls across 100's of pages?
What's the best practice that deals with displaying and accessing common site controls (like a search)? Considering the alternatives, using findcontrol to get a masterpage control doesn't seem that bad.
MasterPages are classes just like a normal Page object. That means you can expose internal controls through public properties to allow child Pages to access without having to resort to Master.FindControl(). To do this you just need to set the MasterType property in the page (I think it may work even without setting this but with this you get intellisense support and avoid having to do casts).
Here's a basic example (sorry it's in VB - this is copying and pasting from an old project):
Master page (.master):
<%# Master Language="VB" CodeFile="default.master.vb" Inherits="DefaultMaster" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<ASP:TextBox ID="Search" RunAt="Server"/>
<ASP:ContentPlaceHolder ID="Content" RunAt="Server"/>
</form>
</body>
</html>
Master code-behind (.master.vb):
Partial Class DefaultMaster : Inherits MasterPage
Public ReadOnly Property SearchBox() As TextBox
Get
Return Search
End Get
End Property
End Class
Accessing page (.aspx):
<%# Page Language="VB" MasterPageFile="default.master" CodeFile="page.aspx.vb" Inherits="ExamplePage" %>
<%# MasterType TypeName="DefaultMaster" %>
<ASP:Content ContentPlaceHolderID="Content" RunAt="Server">
<p>This is some content on the page.</p>
</ASP:Content>
Accessing page code-behind (.aspx.vb):
Partial Class ExamplePage : Inherits Page
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles MyBase.Load
Master.SearchBox.Text = "This page now has access to the master's search box."
End Sub
End Class
Agreeing that master pages are only for layout, would a sensible approach not to farm out the common controls to usercontrols and include them in the master page this way.

ASP.NET MVC - View with master page, how to set title?

What is prefered way of setting html title (in head) for view when using master pages?
One way is by using Page.Title in .aspx file, but that requires in master page which can mess with HTML code. So, lets assume no server side controls, only pure html. Any better ideas?
UPDATE: I would like to set title in view NOT in the controller or model.
In our master pages, we created both an "init" ContentPlaceHolder, and a "title" ContentPlaceHolder. If someone wants to programatically set Page.Title, they can set it in CSharp in the init placeholder, or they can override the "title" placeholder using tags.
Master Page
<asp:ContentPlaceHolder id="init" runat="server"></asp:ContentPlaceHolder>
<head runat="server">
<asp:ContentPlaceHolder ID="title" runat="server">
<title><%=this.Page.Title%></title>
</asp:ContentPlaceHolder>
</head>
View Page
Could either override the entire "title" content placeholder:
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
<title>Home Page</title>
</asp:Content>
or programatically set the page title.
<asp:Content ID="Content1" ContentPlaceHolderID="init" runat="server">
<%this.Title = "Home Page";%>
</asp:Content>
Make sure you remove the Title="" from the Page directive at the top, or you won't be able to programatically change Page.Title.
I see a lot of people that use the <%= ViewData["Title"] %> option.
I suppose you could also insert a ContentPlaceHolder named Title and then just use that on your page, but in all the MVC examples I've seen, they use the first option.
When I create a new MVC project it has files in there and uses a master page. Looking at that it seems it passes the title to the ViewData as ViewData["Title"] and in the master page, in the <head> there is a script block that outputs ViewData["Title"].
I ended up using a code-behind file to implement Page.Title="..." in the Page_Load() method.
I didn't like doing this, however attempts to implement the change directly in the .aspx page did not work, as it resulted in two <title> tags being present, the one I generated, and the one generated by the Master file the page inherited from.
Ideally, my page code should have overridden the master file's <title> value, but I guess this is just one of those quirks that ASP.Net MVC still has, and one that may already be fixed in a newer version of the ASP.Net MVC Framework (we're still on ASP.Net MVC Beta)
You could do this in your Master Page:
<title>
<%:MyTitle + " :: " %>
<asp:ContentPlaceHolder ID="TitleContent" runat="server">
</asp:ContentPlaceHolder>
</title>
where MyTitle = some value from your web.config or hard text like "My Web"
Now in the view pages (Index for example):
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%:"My Home Page"%>
Now when you browse your home page, the title would be "My Web :: My Home Page".
I have a base view class that sets the page title from a resource file. Works pretty good for me.
I created a class called Application with a Title property (using get/set):
public static class Application
{
static string title;
public static string Title
{
get { return title; }
set { title = value; }
}
}
I then set the property on the Page_Load event for each page:
Application.Title = "Silly Application";
then i just reference that property on the master page:
<div id="divApplicationTitle">
<asp:HyperLink runat="server" NavigateUrl="~/Default.aspx"><asp:Image ID="imgApplicationImage" runat="server" SkinID="skinApplicationLogo" /><%=Application.Title%></asp:HyperLink>
</div>
There is a Title property of the #Page directive for content pages.
For ASP.NET content pages just add Title="" in the <%# %> Placeholder.
We ended up with
<head runat=server visible=false>
in master page.
This way we can read from Page.Title (Page.Title requires head element to exist, otherwise it throws an exception, checked that with reflector). We then use our own head element - MVC way.
You could always use javascript in your view page:
<script type="text/javascript>
document.title = "Hello World";
</script>

Resources