Changing Theme by Code

Changing Theme by Code in Asp.NET

The theme to be applied to the page can also be determined at runtime, by codes. The necessary codes for this should be written to the PreInit event of the Page class.

The PreInit event is the first to occur and runs before the page starts booting on the server.

void Page_PreInit(object sender, EventArgs e) 

   Page.Theme = "Theme1"; 
}

asp.net kod ile tema değiştirme

If we have more than one theme and we want to give the user a choice;

For example, if we want to make a selection from a RadioButtonList as seen in the picture above and apply the theme according to the selection, we could normally write a code like the one below.

void Page_PreInit(object sender, EventArgs e) 

   Page.Theme = RadioButtonList1.SelectedValue; 
}

But these codes will not work!!! Because the page is not loaded yet when the Page_PreInit event occurs, the properties of the controls on the page cannot be accessed. In other words, the RadioButtonList1 we want to get the selected theme information has not been created yet.

Instead, we can reach the solution with different methods, the simplest way is to use QueryString.

In this example, let's use two buttons and apply a different theme to the page with each of the buttons:

protected void Page_PreInit(object sender, EventArgs e)
{
    if (Request.QueryString["secilenTema"] != null) Page.Theme = Request.QueryString["secilenTema"];
    else Page.Theme = "Tema1";
}
 
protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("default.aspx?secilenTema=Tema1");
}
 
protected void Button2_Click(object sender, EventArgs e)
{
    Response.Redirect("default.aspx?secilenTema=Tema2");
}

 

Changing the theme with asp.net code, allowing the visitor to choose a theme, dynamically changing the theme, changing the c# theme with code

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 676 times.

Online Users: 195



changing-theme-by-code