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
Can I have a unique key as foreign key?
What is server side session management?
How Can assign alias name for ASP.NET Web API Action?
What are Session states available and its Uses?
What is a web farm?
Explain the working of passport authentication.
How you will improve web application performance?
What is content page in asp net?
What is data reader in asp.net?
Explain the main function of url routing system in asp.net mvc? : asp.net mvc
Which is an advantage of application service providers?
Differentiate between a hyperlink control and a linkbutton control.
Is it possible to migrate visual interdev design-time controls to asp.net?
What are the difference between overriding and overloading?
What is the difference between a candidate key and primary key?