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.
Related
I have a custom control in ASP.NET (VB.NET in code behind), defined with an ASCX:
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="MyControl.ascx.vb" Inherits="Mynamespace.Controls.MyControl" %>
<!-- some html and other custom controls-->
And in code behind:
Namespace Controls
Public Class MyControl
Inherits System.Web.UI.UserControl
This is set in a library. A different project uses that control in a page:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="mypage.aspx.vb"
Inherits="myproject.mypage" culture="auto" meta:resourcekey="Page" uiculture="auto"
Transaction="RequiresNew" MasterPageFile="Mynamespace.Master"
Theme="ThemeBase2" StylesheetTheme="ThemeBase2" %>
<%# Register tagprefix="Controls" tagname="MyControl" src="../Controls/MyControl.ascx" %>
<%-- some asp.net --%>
<Controls:MyControl ID="mycontrol1" runat="server"
MyCustomProperty="value" />
However, when I build, I get an error saying
'MyCustomProperty' is not a member of 'System.Web.UI.UserControl'.
And in the designer.vb page I see:
Protected WithEvents mycontrol1 As Global.System.Web.UI.UserControl
How do I ensure it becomes:
Protected WithEvents mycontrol1 As Global.Mynamespace.Controls.MyControl
?
Make sure that MyControl is defined inside Global.Mynamespace.Controls.MyControl. It inherits this namespace, but it seems that this is supposed to be the namespace in which it is defined. Also, make sure that MyCustomProperty is defined, of course.
Your ascx file is not accessible because it is in a library
You need to save the ascx file as an embedded resource of your library and load it as extern resource in your web application.
You can consult this link for more informations.
If you want share yours controls, i advise to create UserControl instead of CustomControl. Unfortunately, there is more work because the designer is not usable
To manage page title on page's,I have a master page where i am taking ContentPlaceHolder.
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /> </title>
and on every page i write
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Some Title Here
</asp:Content>
Now my client ask me for remove title on all page's and keep it on master page but not remove content place holder code on all page's and master page so that in future if any requirement then we can insert data in to them.
So my problem is without removing them on master page and pages i am not able to put title on master page.So how can i handle this situation?
Thanks Guys.. I got solution
if you want to set part of the title from within the master page. For example, you might want the title of every page to end with a suffix, “ – MySite”.
If you try this (notice the – MySite tacked on):
<%# Master ... %>
<html>
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="titleContent" runat="server" /> - MySite
</title>
</head>
And run the page, you’ll find that the – MySite is not rendered. This appears to be a quirk of the HtmlHead control. This is because the title tag within the HtmlHead control is now itself a control.
The fix is pretty simple. Add your text to a LiteralControl like so.
<%# Master ... %>
<html>
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="titleContent" runat="server" />
<asp:Literalrunat="server" Text=" - MySite" />
</title>
</head>
If you want a good solution to overriding the page title:
Create a class of your own that inherits from the System.Web.Mvc.ViewPage.
Have your view pages inherit from that class:
Write a Page_Load handler in your new class that does something like this:
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.Title = "Company Name | " + Page.Title
End Sub
You also don't need a content place holder to change the title. The <head> tag is already a runat server control. Setting the Page.Title in the page load (or earlier event) work just fine.
You could also put a runat server script tag in your master page to accomplish this task too.
Easiest way:
Move the current ContentPlaceHolder somewhere to your HTML, and wrap it in a <asp:PlaceHolder runat="server" visible="false"/>. When you'll be needing it later on, just move the ContentPlaceHolder back again.
Use the OnPreRender event on the master page to set the title, overriding what has been set on each page.
why not add attribute Visible=false to ContentPlaceHolder of Master Page
I think this is the easiest way to handle your situation.
Happy coding.
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.
Is there a difference between these two scenarios:
(1) Accessing a property on a master page from a regular child
(2) Accessing a property on a master page from a nested master page
I tried to access a textbox in the master page from a content page like this:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
defaultTextbox.Text = a.Text; // defaultTextBox is a textbox control inside default.aspx
it works, but then when I applied the same method on a nested master page:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
myTextBox.Text = a.Text; // myTextBox is a textbox control inside child.master
this does not work, am I missing something? I call both codes inside regulare page_load handler...
I also noticed I cannot set textbox value inside the nested master page from code behind, there is definitely something im missing, what is it?
To shed light on this issue, here is an example:
Nested Master Page:
<%# Master Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:textbox id="tx2" runat="server" text="this is two"></asp:textbox>
<asp:contentplaceholder id="newstuff" runat="server"></asp:contentplaceholder>
</asp:Content>
Code behind:
Response.Wrote(tx2.Text);
I get NOTHING, why what did I miss? note that I also tried the recursive find control:
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
still nothing
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
//base content place holder id
Label objLabel3 = (Label)cp.FindControl("lblNested");
//lblNested is id in nested master page
I read few things here:
http://www.odetocode.com/Articles/450.aspx
and found out that the nested page in the middle never calls Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following:
protected override void OnLoad(EventArgs e)
{
myTextBox.Text = "anything";
base.OnLoad(e);
}
This should work without any problems, so something else must be wrong. I just tried it inside a simple test project and I have no problems finding a control on the master page in both cases.
I would check (again) if you refer to the correct master page inside your nested master page. What you could also check is the runtime type of the Master property inside your nested master page. It should be the type of your master page.
EDIT: I thought the issue was about finding a control in the root master page from a nested master page and this should work without any problems. For finding a control inside a content placeholder in a nested master page, take a look at the following forum post.
You can have absolute control of your content in both master page and nested page from your content page using the directives:
<%# MasterType VirtualPath="your_master.master" %>
<%# Reference VirtualPath="~/your_master.master" %>
See the excellent article from K. Scott Allen in Ode To Code
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>