How can you change a Master page dynamically in which event
of page life cycle?

Answer Posted / ashutosh tripathi

Unfortunately there is not built in support to change page
themes at runtime. Here is a simple code which can be used
to change page themes at runtime:
At first though we may say we can easily achieve this by
coding it in Page_Preinit Event as shown below.

protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme = "Black"

}

But problem with this is we cant assign value from dropdown
box because Page_Preinit event is fired much before
dropdown has changed value.

To resolve this issue, just use the following steps:
1-Create one session variable which will hold current theme
value
2-On selection change event of dropdown combo box , assign
value form combo box to session variable.
3-During Page_preInit Event assign this variable value to
Page.Theme property.
4-Stop page loading and reload same page again using
server.transfer method as shown below


protected void Page_PreInit(object sender, EventArgs e)
{
string thm;
thm = (string)Session["themeSelected"];
if (thm != null)
{
Page.Theme = thm;
DropDownList1.Text = thm;
}
else
{
Session["themeSelected"] = DropDownList1.Text;
Page.Theme = "Blue";
}

protected void DropDownList1_SelectedIndexChanged(object
sender, EventArgs e)
{
Session["themeSelected"] = DropDownList1.Text;
Server.Transfer(Request.FilePath);

}


Is This Answer Correct ?    8 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How is it possible for .NET to support many languages?

485


Where code pages are used?

886


What parameters can you pass in the url of the api?

844


What is the server of asp.net?

723


How to find last error which occurred in Asp.net ?

922


What is difference between cookies and cache?

741


What is query string?

769


What are main return types supported in Web API?

989


How Session use Cookies in State Management?

783


What is the latest version of asp.net?

737


Is asp.net a programming language or framework?

754


Explain a program using code nuggets to create a simple application? : asp.net mvc

744


What are asp.net web forms?

740


Which object encapsulates state or data of a user?

821


What parameters can you pass in the url of the api? Can get and post use the same url?

736