Suggested Videos
Part 149 - Passing data from master page to content page
Part 150 - Default content in contentplaceholder of a master page
Part 151 - Assigning a master page dynamically
In this video, we will understand the order of events execution, when we have a content page with a master page and a user control.
1. Create a new asp.net webforms application and name it WebFormsDemo.
2. Add a UserControl with name="TestUserControl.ascx". Copy and paste the following html. This is a very simple user control, with just an h1 tag with some static text.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs"
Inherits="WebFormsDemo.TestUserControl" %>
<h1>This is a Test User Control</h1>
3. Add a MasterPage and name it Site.Master. Copy and paste the following code in the code-behind file.
public partial class Site : System.Web.UI.MasterPage
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Master Page Init Event<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Master Page Load Event<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Master Page PreRender Event<br/>");
}
}
4. Right click on the MasterPage, and select Add Content Page. This should add "WebForm1.aspx" with a content control in it. Flip WebForm1.aspx to design mode. Drag and drop a textbox control and TestUserControl onto the content page. Associate event handler methods for OnInit, OnLoad and OnPreRender events for the textbox and the usercontrol. At this point the HTML should look as shown below.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebFormsDemo.WebForm1" %>
<%@ Register src="TestUserControl.ascx"
tagname="TestUserControl" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<asp:TextBox ID="TextBox1" runat="server"
OnInit="TextBox1_Init" OnLoad="TextBox1_Load"
OnPreRender="TextBox1_PreRender">
</asp:TextBox>
<br />
<uc1:TestUserControl ID="TestUserControl1" runat="server"
OnInit="TestUC1_Init" OnLoad="TestUC1_Load"
OnPreRender="TestUC1_PreRender"/>
</asp:Content>
5. Finally copy and paste the following code in the code-behind file of WebForm1.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Content Page Init Event<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Content Page Load Event<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Content Page PreRender Event<br/>");
}
protected void TextBox1_Init(object sender, EventArgs e)
{
Response.Write("TextBox Init Event<br/>");
}
protected void TextBox1_Load(object sender, EventArgs e)
{
Response.Write("TextBox Load Event<br/>");
}
protected void TextBox1_PreRender(object sender, EventArgs e)
{
Response.Write("TextBox PreRender Event<br/>");
}
protected void TestUC1_Init(object sender, EventArgs e)
{
Response.Write("UserControl Init Event<br/>");
}
protected void TestUC1_Load(object sender, EventArgs e)
{
Response.Write("UserControl Load Event<br/>");
}
protected void TestUC1_PreRender(object sender, EventArgs e)
{
Response.Write("UserControl PreRender Event<br/>");
}
}
Run the application and notice that, the events are executed in the following order.
TextBox Init Event
UserControl Init Event
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
TextBox Load Event
UserControl Load Event
Content Page PreRender Event
Master Page PreRender Event
TextBox PreRender Event
UserControl PreRender Event
So, in general the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one.
Please note that the master page is merged into the content page and treated as a control in the content page. Master page is merged into the content page during the initialization stage of page processing.
Two of our valuable youtube channel subscribers were asked the following question in an interview.
Question 1: We have a master page, content page and a user control, with the following events
Master init & master load
Content init & Content load
User control init & User control load
When we run the page containing above things, in what sequence the above events are fired.
Answer: Here is the order
User control init
Master init
Content Init
Content load
Master load
UserControl Load
Part 149 - Passing data from master page to content page
Part 150 - Default content in contentplaceholder of a master page
Part 151 - Assigning a master page dynamically
In this video, we will understand the order of events execution, when we have a content page with a master page and a user control.
1. Create a new asp.net webforms application and name it WebFormsDemo.
2. Add a UserControl with name="TestUserControl.ascx". Copy and paste the following html. This is a very simple user control, with just an h1 tag with some static text.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs"
Inherits="WebFormsDemo.TestUserControl" %>
<h1>This is a Test User Control</h1>
3. Add a MasterPage and name it Site.Master. Copy and paste the following code in the code-behind file.
public partial class Site : System.Web.UI.MasterPage
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Master Page Init Event<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Master Page Load Event<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Master Page PreRender Event<br/>");
}
}
4. Right click on the MasterPage, and select Add Content Page. This should add "WebForm1.aspx" with a content control in it. Flip WebForm1.aspx to design mode. Drag and drop a textbox control and TestUserControl onto the content page. Associate event handler methods for OnInit, OnLoad and OnPreRender events for the textbox and the usercontrol. At this point the HTML should look as shown below.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebFormsDemo.WebForm1" %>
<%@ Register src="TestUserControl.ascx"
tagname="TestUserControl" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<asp:TextBox ID="TextBox1" runat="server"
OnInit="TextBox1_Init" OnLoad="TextBox1_Load"
OnPreRender="TextBox1_PreRender">
</asp:TextBox>
<br />
<uc1:TestUserControl ID="TestUserControl1" runat="server"
OnInit="TestUC1_Init" OnLoad="TestUC1_Load"
OnPreRender="TestUC1_PreRender"/>
</asp:Content>
5. Finally copy and paste the following code in the code-behind file of WebForm1.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Content Page Init Event<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Content Page Load Event<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Content Page PreRender Event<br/>");
}
protected void TextBox1_Init(object sender, EventArgs e)
{
Response.Write("TextBox Init Event<br/>");
}
protected void TextBox1_Load(object sender, EventArgs e)
{
Response.Write("TextBox Load Event<br/>");
}
protected void TextBox1_PreRender(object sender, EventArgs e)
{
Response.Write("TextBox PreRender Event<br/>");
}
protected void TestUC1_Init(object sender, EventArgs e)
{
Response.Write("UserControl Init Event<br/>");
}
protected void TestUC1_Load(object sender, EventArgs e)
{
Response.Write("UserControl Load Event<br/>");
}
protected void TestUC1_PreRender(object sender, EventArgs e)
{
Response.Write("UserControl PreRender Event<br/>");
}
}
Run the application and notice that, the events are executed in the following order.
TextBox Init Event
UserControl Init Event
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
TextBox Load Event
UserControl Load Event
Content Page PreRender Event
Master Page PreRender Event
TextBox PreRender Event
UserControl PreRender Event
So, in general the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one.
Please note that the master page is merged into the content page and treated as a control in the content page. Master page is merged into the content page during the initialization stage of page processing.
Two of our valuable youtube channel subscribers were asked the following question in an interview.
Question 1: We have a master page, content page and a user control, with the following events
Master init & master load
Content init & Content load
User control init & User control load
When we run the page containing above things, in what sequence the above events are fired.
Answer: Here is the order
User control init
Master init
Content Init
Content load
Master load
UserControl Load
If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website
0 comments:
Post a Comment