none
需要一点小小的源代码 RRS feed

  • 问题

  • 有谁能给偶一个例子,点击网页上一个地方,可以动态改变该网页上的某些控件的SkinID啊?
    比如

    <form><asp:Button ID="haha" runat="server" Text="haha" SkinID="SmallText"/></form>
    单击该网页变成
    <form><asp:Button ID="haha" runat="server" Text="haha" SkinID="loveme"/></form>
    然后会显示不同的skin,谢谢哦。

    maiyude
    2009年5月31日 7:39

答案

  • 要在Page_Init事件里面写。。在其他的里面写的话是会出错的。。
    低调,低调
    哈哈,书出了,可以点这里购买哦。。
    2009年5月31日 11:34
    版主
  • -------------------------------------------------------------------------------------------------
    -- SkinFile1.skin
    -------------------------------------------------------------------------------------------------
    <asp:GridView runat="server" SkinId="gridviewSkin1" BackColor="Blue" >
    </asp:GridView>
    <asp:GridView runat="server" SkinId="gridviewSkin2" BackColor="Green">
    </asp:GridView>
    -------------------------------------------------------------------------------------------------
    -- Aspx
    -------------------------------------------------------------------------------------------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200905311946.aspx.cs" Inherits="DataList_X200905311946" Theme="SkinFile1" %>
    <!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>动态切换指定控件的 SkinId</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="fGridView" runat="server"></asp:GridView>
            <asp:Button ID="fButton" runat="server" onclick="Button_Click" Text="Change SkinId" />
        </form>
    </body>
    </html>
    
    
    -------------------------------------------------------------------------------------------------
    -- Aspx.cs
    -------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    /// <summary>
    /// 标题: 动态切换指定控件的 SkinId
    /// </summary>
    public partial class DataList_X200905311946 : System.Web.UI.Page
    {
        #region " Employee "
    
        private DataTable Employee
        {
            get
            {
                if (Session["Employee"] == null)
                {
                    DataTable fEmployee = new DataTable();
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(int))); // 员工 Id
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(string))); // 员工名称
    
                    fEmployee.Rows.Add(new object[] { 0, "X.X.Y" });
                    fEmployee.Rows.Add(new object[] { 1, "Chole" });
                    fEmployee.Rows.Add(new object[] { 2, "Tony" });
                    fEmployee.Rows.Add(new object[] { 3, "John" });
                    fEmployee.Rows.Add(new object[] { 4, "Chirs" });
                    fEmployee.Rows.Add(new object[] { 5, "Li" });
    
                    Session["Employee"] = fEmployee;
                }
    
                return (DataTable)Session["Employee"];
            }
        }
    
        #endregion
    
        private string CurSkinId
        {
            get 
            {
                if (Session["GridSkin"] == null)
                    Session["GridSkin"] = "gridviewSkin1";
                return (string)Session["GridSkin"];
            }
            set 
            {
                Session["GridSkin"] = value;
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fGridView.DataSource = this.Employee;
                this.DataBind();
            }
        }
    
        protected override void OnPreInit(EventArgs e)
        {
            this.fGridView.SkinID = this.CurSkinId;
            base.OnPreInit(e);
        }
    
        protected void Button_Click(object sender, EventArgs e)
        {
            if (this.CurSkinId == "gridviewSkin1")
                Session["GridSkin"] = "gridviewSkin2";
            else
                Session["GridSkin"] = "gridviewSkin1";
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    2009年5月31日 14:51
    版主

全部回复

  • protected void Button1_Click(object sender, EventArgs e)
    {
      haha.SkinID = "loveme";
    }


    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="修改Skin" />

    【孟子E章】
    2009年5月31日 7:44
    版主
  • http://blog.csdn.net/sunscz/articles/725207.aspx
    知识改变命运,奋斗成就人生!
    2009年5月31日 7:53
    版主
  • 能够直接改最好了,可是报错的。。。要写在priinit里面的。但是preinit写上去没反应的。

     if (Request.QueryString["page_theme"] != null)
            {
                Page.Theme = Request.QueryString["page_theme"].ToString();

            }
            else
            {
                Page.Theme = "Default";
           
            }

            //动态设置控件外观
            if (Page.Theme != "" && Request.QueryString["button_skinid"] != null)
                Button1.SkinID = Request.QueryString["button_skinid"].ToString(); ///实际上偶设的这句话能运行但不能正确显示。。


    maiyude
    2009年5月31日 9:03
  • 谢谢啊,偶需要一个单独能改skin的方法的。
    maiyude
    2009年5月31日 9:04
  • button 事件 
    Session["page_theme"] = xxx;
    priinit 写
    if (Session["page_theme"] != null)
     Page.Theme = Session["page_theme"].ToString();

    知识改变命运,奋斗成就人生!
    2009年5月31日 9:07
    版主
  • 谢谢,那么有没有改skinid的呢?有点不好意思了。
    maiyude
    2009年5月31日 10:01
  • 要在Page_Init事件里面写。。在其他的里面写的话是会出错的。。
    低调,低调
    哈哈,书出了,可以点这里购买哦。。
    2009年5月31日 11:34
    版主
  • -------------------------------------------------------------------------------------------------
    -- SkinFile1.skin
    -------------------------------------------------------------------------------------------------
    <asp:GridView runat="server" SkinId="gridviewSkin1" BackColor="Blue" >
    </asp:GridView>
    <asp:GridView runat="server" SkinId="gridviewSkin2" BackColor="Green">
    </asp:GridView>
    -------------------------------------------------------------------------------------------------
    -- Aspx
    -------------------------------------------------------------------------------------------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200905311946.aspx.cs" Inherits="DataList_X200905311946" Theme="SkinFile1" %>
    <!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>动态切换指定控件的 SkinId</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="fGridView" runat="server"></asp:GridView>
            <asp:Button ID="fButton" runat="server" onclick="Button_Click" Text="Change SkinId" />
        </form>
    </body>
    </html>
    
    
    -------------------------------------------------------------------------------------------------
    -- Aspx.cs
    -------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    /// <summary>
    /// 标题: 动态切换指定控件的 SkinId
    /// </summary>
    public partial class DataList_X200905311946 : System.Web.UI.Page
    {
        #region " Employee "
    
        private DataTable Employee
        {
            get
            {
                if (Session["Employee"] == null)
                {
                    DataTable fEmployee = new DataTable();
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(int))); // 员工 Id
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(string))); // 员工名称
    
                    fEmployee.Rows.Add(new object[] { 0, "X.X.Y" });
                    fEmployee.Rows.Add(new object[] { 1, "Chole" });
                    fEmployee.Rows.Add(new object[] { 2, "Tony" });
                    fEmployee.Rows.Add(new object[] { 3, "John" });
                    fEmployee.Rows.Add(new object[] { 4, "Chirs" });
                    fEmployee.Rows.Add(new object[] { 5, "Li" });
    
                    Session["Employee"] = fEmployee;
                }
    
                return (DataTable)Session["Employee"];
            }
        }
    
        #endregion
    
        private string CurSkinId
        {
            get 
            {
                if (Session["GridSkin"] == null)
                    Session["GridSkin"] = "gridviewSkin1";
                return (string)Session["GridSkin"];
            }
            set 
            {
                Session["GridSkin"] = value;
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fGridView.DataSource = this.Employee;
                this.DataBind();
            }
        }
    
        protected override void OnPreInit(EventArgs e)
        {
            this.fGridView.SkinID = this.CurSkinId;
            base.OnPreInit(e);
        }
    
        protected void Button_Click(object sender, EventArgs e)
        {
            if (this.CurSkinId == "gridviewSkin1")
                Session["GridSkin"] = "gridviewSkin2";
            else
                Session["GridSkin"] = "gridviewSkin1";
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    2009年5月31日 14:51
    版主