zoukankan      html  css  js  c++  java
  • 根据用户喜欢的爱好选择不同风格CSS(ViewState)

    使用ASP.NET控件和页面视图状态类(StateBag)的一个实例ViewState 
    新建一个ASP.NET网站,在打开的默认Defalut.aspx中添加DropDownList控件,将其属性AutoPostBack设置为True,默认是false,在Items集合属性中添加如图所示:
     
    点击确定后,下来列表就有两个值红色和绿色。
     
    查看源:

    <%@ 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>
            <asp:DropDownList ID="ddpFavorite" runat="server" AutoPostBack="True"
                onselectedindexchanged="ddpFavorite_SelectedIndexChanged">
                <asp:ListItem Value="1">红色</asp:ListItem>
                <asp:ListItem Value="2">绿色</asp:ListItem>
            </asp:DropDownList>
        </div>
        </form>
    </body>
    </html>

    新建样式表red.css

    body
    {
     background-color:Red;
    }

    新建样式表blue.css

    body
    {
     background-color:Blue;
    }
    打开Default.aspx.cs
    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Web.UI.HtmlControls;
    public partial class _Default : System.Web.UI.Page
    {
        string Favorite
        {
            get
            {
                if (ViewState["Favorite"] != null)
                {//判断视图状态是否为空
                    return ViewState["Favorite"].ToString();
                }
                else
                {//默认为红色
                    ViewState["Favorite"] = "1";
                }
                return "1";
            }
            set
            {
                ViewState["Favorite"] = value;
            }
           
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //创建对象
            HtmlGenericControl link = new HtmlGenericControl();
            //Attributes集合
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            link.TagName = "link";
            //判断用户选择值
            if (Page.IsPostBack)
            {//回传
                string csspath = String.Empty;
                switch (Favorite)
                {
                    case "2":
                        csspath = "red.css";
                        break;
                    case "1":
                        csspath = "blue.css";
                        break;
                }
                link.Attributes.Add("href", csspath);
            }
            else if(!Page.IsPostBack)
            {//不回传
                string csspath = String.Empty;
                switch(Favorite)
                {
                    case "1":
                        csspath = "red.css";
                        break;
                    case "2":
                        csspath = "blue.css";
                        break;
                }
                link.Attributes.Add("href", csspath);
            }
            this.Controls.Add(link);
        }

    在Default.cs设计中双击DropDownList
        protected void ddpFavorite_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取列表控件中选定的值
            Favorite = this.ddpFavorite.SelectedValue;
        }
    }
    按Ctrl+F5运行,Ok!简单的一个根据选择,背景风格css转换就完成了!

     
     
     
     
     
     
  • 相关阅读:
    Nginx负载均衡+代理+ssl+压力测试
    Nginx配置文件详解
    HDU ACM 1690 Bus System (SPFA)
    HDU ACM 1224 Free DIY Tour (SPFA)
    HDU ACM 1869 六度分离(Floyd)
    HDU ACM 2066 一个人的旅行
    HDU ACM 3790 最短路径问题
    HDU ACM 1879 继续畅通工程
    HDU ACM 1856 More is better(并查集)
    HDU ACM 1325 / POJ 1308 Is It A Tree?
  • 原文地址:https://www.cnblogs.com/scsuns520/p/1630952.html
Copyright © 2011-2022 走看看