Friday, June 8

Dynamically Change Themes and Style Sheet

Here is I am using Three WebPages
Change Css.aspx
ChangeTheme.aspx
Default.aspx


Source Code of Change Css.aspx:-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Change Css.aspx.cs" Inherits="Change_Css" %>

<!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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
</body>
</html>

 Code Behind of Change Css.aspx:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Change_Css : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["TxtValue"] = TextBox1.Text;
        Response.Redirect("Default.aspx");
    }
}


Source Code of  Default.aspx :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 id="Test" >
   
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            style="font-weight: 700; color: #FFFFCC; text-align: right; background-color: #00CC00"
            Text="Return" />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
   
    </div>
    </form>
</body>
</html>


  Code Behind of  Default.aspx :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        if (Session["TxtValue"] != null)
        {
            Literal link = new Literal();
            if ("1" == Session["TxtValue"].ToString())
            {
                link.Text = "<link href=\"Css/StyleSheet.css\" rel=\"stylesheet\" type=\"text/css\" />";
            }
            else if ("2" == Session["TxtValue"].ToString())
            {
                link.Text = "<link href=\"Css/StyleSheet2.css\" rel=\"stylesheet\" type=\"text/css\" />";
            }
            else if ("3" == Session["TxtValue"].ToString())
            {
                link.Text = "<link href=\"Css/StyleSheet3.css\" rel=\"stylesheet\" type=\"text/css\" />";
            }
            base.Page.Header.Controls.Add(link);


            base.OnInit(e);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Change Css.aspx");
    }
}

Source Code of   ChangeTheme.aspx  :-

<%@ Page Language="C#" AutoEventWireup="true" Theme="Theme1" CodeFile="ChangeTheme.aspx.cs" Inherits="ChangeTheme" %>

<!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 id="Test">
   
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
   
    </div>
    Change Theme:--<asp:DropDownList ID="DropDownList1" runat="server"  AutoPostBack="true"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>Theme1</asp:ListItem>
        <asp:ListItem>Theme2</asp:ListItem>
        <asp:ListItem>Theme3</asp:ListItem>
    </asp:DropDownList>
    </form>
</body>
</html>

 Code Behind of   ChangeTheme.aspx  :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ChangeTheme : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {

        string theme = "Theme1";

        if (Page.Request.Form.Count > 0)
        {

        

            theme = Request["DropDownList1"].ToString();



        }

        Page.Theme = theme;



    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}


Thanks Shibashish mohanty






1 comment:

  1. What a good topic!I really appreciate your opinions.



    ----------------------------------------------
    Rc Helicopter|Rc Helicopters|Mini Rc Helicopter

    ReplyDelete

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty