ASP.NET Page / MasterPage life cycle - asp.net

Trying to understand how after MasterPage render, the content of Page already exists in HtmlTextOutput?
ASP.NET, first calling Page.Render, then Page renders it's children controls and MasterPage is in theory "child control" of page. If so, how then after MasterPage.Render completes it contains the output for all other child controls of Page? How Master.Render, "calls" render of page itself and it's child controls?
Example:
MasterPage:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MAIN.master.cs" Inherits="Demo.MAIN" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
ContentPlaceHolder - BEFORE
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
ContentPlaceHolder - AFTER
</div>
</form>
</body>
</html>
public partial class MAIN : System.Web.UI.MasterPage
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
//At this point HtmlTextWriter already contains the output of all page child controls...how it's happen?
}
Page:
<%# Page Title="" Language="C#" MasterPageFile="~/MAIN.Master" AutoEventWireup="true" CodeBehind="MAIN_PAGE.aspx.cs" Inherits="Demo.MAIN_PAGE" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
CONTENT
<TL:MyButton id="ServerControl" Text="btn" runat="server"/>
</asp:Content>
public partial class MAIN_PAGE : MyPage
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}

Related

error in aspx page. You can only have one <title> element within the <head> element

net site with master and child pages. I am generating title via .cs code file on each child page. It works fine on the desktop. However, when I try to browse via mobile, it throws the above error. Below is code for master page.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="xyz.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/styles.css" type="text/css" >
</head>
<body>
<form runat="server">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
****child page code behind ***
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HtmlTitle title = new HtmlTitle();
title.Text = "child page title";
Header.Controls.Add(title);
}
}
*** child page ****
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="childpage.aspx.cs" Inherits="xyz._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
what is the best way to handle this. Appreciate your thoughts on this.

Implementation Plugin (UserControl) based system in ASP WebForm

I have many UserControls and I wanna load them depended on query string in one aspx page.
I tired 2 following ways:
Add a Placeholder in aspx page and in Page_Load/Page_Render event:
UserControl uc = (UserControl)LoadControl("/PATH/ProductGroups.ascx");
phMain.Controls.Add(uc);
Add a MasterPage, then add an aspx and using of masterpage, then register the UserControl on the aspx
But in both, I got following error:
The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
BTW, I have used telerik TextEditor in the loaded UserControl.
How I can handle that?
I think the only way that I have, multiple aspx per UserContorl, without MasterPage, this is sucks, as you know ! because I have to fix HTML/CSS/JS in all pages, one by one and this is not pro !
NOTE: without using telerik TextEditor, everything working fine, but I need this one, also this wat (loading UserControl in PlaceHolder) is not really good way.
I'm looking for something like DNN.
I tried to replicate this error, but i couldn't. It is just working fine for me. Here is what I tried in VS2013.
1) Added MasterPage
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="Test.Site1" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
2)Added WebUserControl
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="Test.WebUserControl1" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<telerik:RadEditor runat="server" ID="RadEditor1" SkinID="DefaultSetOfTools" Height="675px">
</telerik:RadEditor>
3) Added WebForm
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Test.WebForm3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:PlaceHolder runat="server" ID="phMain" />
</asp:Content>
4) Loaded the control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Test
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControl uc = (UserControl)LoadControl("~/WebUserControl1.ascx");
phMain.Controls.Add(uc);
}
}
}
Here is the resolution for the error "Control collection cannot be modified"

how to make a asp.net page without any payload just a string in the sourcecode?

normaly a asp.net page looks like:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="my text"></asp:Label>
</div>
</form>
</body>
</html>
and then i get displayed "my text".
the problem is, now i need to make a page and just display the "my text" without the html stuff in the background. so when i look in the sourcecode it should just be my text
of course i need the asp.net in the background to change the label text.
is this possible?
Just delete everything except the page tag, and on Page_Load write my text in the response
In the aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
In the .cs:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("My text")
}
Or you can add a literal to the page (set it's value in the code behind) and remove the html stuff like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
<asp:Literal ID="literal1" runat="server" />

master page implementation in asp.net

i am using vb-2008 to create my application. i created master page in asp but i am not able to use it on other pages. i used :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" MasterPageFile="~/Mail.Master" Inherits="webform1._Default" %>
i created master page as:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Mail.master.cs" Inherits="master1.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<asp:Image ID="imghead" runat="server" ImageUrl="~/images/images1.jpeg" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
but this is not showing master page on other page where it is implemented..
how can i implement the master page..
Now you need to create ASPX pages that have the masterpage assigned and fill up the content placeholders
your new page called, for example, default.aspx will contain:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" MasterPageFile="~/Mail.Master" Inherits="webform1._Default" %>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
<!-- Add code here to add to the HeadContent section -->
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<!-- Add code here to add to the MainContent section -->
<asp:Image ID="imghead" runat="server" ImageUrl="~/images/images1.jpeg" />
</asp:ContentPlaceHolder>
A MasterPage only holds the PlaceHolders for where other pages will inject content.
There is a hole Video on MasterPages that you can see here:
ASP.NET WebForms Part 5: MasterPages

how to setup masterpage programmatically?

the function
private void SetUpMasterPage(){
this.MasterPageFile = "~/MasterPages/NestedMasterPageTest2.Master";
}
is called on the OnPreInit... This works when the masterpagefile is the base masterpage... But how are we going to make it work for the nested masterpage?
we actually tried
this.Master.MasterPageFile = "~/MasterPages/Base.Master";
but it throws an exception... =(
So, to make things clearer, on the aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test._Default"%>
<asp:Content ID="testContent" ContentPlaceHolderID="body" runat="server">
This is a test!
</asp:Content>
on the base.Master
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Base.master.cs" Inherits="Test.Base" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>The title</title>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="body" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and on the default.aspx.cs
protected override void OnPreInit(EventArgs e){
this.MasterPageFile = "~/MasterPages/Base.Master";
}
it is working...
but when I use a nested master page
<%# Master Language="C#" MasterPageFile="~/MasterPages/Base.Master" AutoEventWireup="true" CodeBehind="NestedMasterPageTest2.master.cs" Inherits="Test.MasterPages.NestedMasterPageTest2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="server">
</asp:Content>
then changed the default.aspx.cs
protected override void OnPreInit(EventArgs e){
this.MasterPageFile ="~/MasterPages/NestedMasterPageTest2.Master";
}
and I changed the default.aspx
<asp:Content ID="testContent" ContentPlaceHolderID="Content2" runat="server">
This is a test which uses a nested master page!
</asp:Content>
it returns an error that says Cannot find ContentPlaceHolder 'Content2' in the master page '/MasterPages/NestedMasterPageTest2.Master', verify content control's ContentPlaceHolderID attribute in the content page. But Content2 is on NestedMasterPageTest2. What really must have happened here?
Your nested master page has Content control but no ContentPlaceHolder control. Try adding it:
<asp:ContentPlaceHolder ID="cplh" runat="server">
</asp:ContentPlaceHolder>
Then, in Default.aspx:
<asp:Content ID="cnt1" ContentPlaceHolderID="cplh" runat="server">
This is a test which uses a nested master page!
</asp:Content>
You need to add an <asp:ContentPlaceholder> tag to your nested master page:
<asp:Content ID="basebody" ContentPlaceHolderID="body" runat="server">
<asp:ContentPlaceHolder ID="Content2" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>

Resources