如何在母版页asp.net中设置动态主题? [英] how to set dynamic theme in master page asp.net?

查看:85
本文介绍了如何在母版页asp.net中设置动态主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net母版页中设置动态主题?

how to set dynamic theme in master page asp.net?

推荐答案

您好,ali2012,

以下链接可以为您提供帮助.
动态主题
如果您有任何问题,只需对其做一些工作.多做一些练习.

祝你有美好的一天!!!
Hi ali2012,

The following link helps you.
Dynamic Thems
If you have any problem,just do some work on it.Do more practice.

Have a good day!!!


此处查看主题母版页和主题. [^ ]
See the topic Master Pages and themes here[^]


创建桌子

Create table

CREATE TABLE USERS
(
	ID int IDENTITY(1,1) ,
	Name varchar(50) ,
	Email varchar(100) ,
	CreateDate datetime 
)




/*************************** gridwiththemes.aspx *********************/

< asp:ObjectDataSource ID ="ThemeDataSource" runat =服务器" SelectMethod ="GetThemes"
TypeName ="ThemeManager"></asp:ObjectDataSource>

< br/>
< br/>
< asp:DropDownList ID ="themelist" runat ="server" DataTextField ="name" DataValueField ="name"
OnSelectedIndexChanged ="themelist_SelectedIndexChanged" DataSourceID ="ThemeDataSource"
OnDataBound ="themelist_DataBound" AutoPostBack ="true">
< div>
< br/>
< asp:按钮ID ="btnsubmit"文本="Submit" runat =服务器"/>
</div>
< div>
< asp:GridView ID ="gvUser" DataKeyNames ="ID" runat ="server" AutoGenerateColumns ="false">
<专栏>
< asp:TemplateField>
< ItemTemplate>
< a id ="linkDelete" runat ="server" href =#">
< img src ="Update.jpg" alt ="></a>
< asp:Button runat ="server" CommandName ="Delete" ID ="btnDelete" Style ="display:none;" />
</ItemTemplate>
</asp:TemplateField>
< asp:TemplateField>
< ItemTemplate>
< asp:LinkBut​​ton ID =选择" Text ="Edit" runat ="server" CommandName ="Select"></asp:LinkBut​​ton>
</ItemTemplate>
</asp:TemplateField>
< asp:BoundField DataField ="NAME" HeaderText ="NAME" SortExpression ="NAME"/>
< asp:BoundField DataField ="EMAIL" HeaderText ="EMAIL" SortExpression ="EMAIL"/>
< asp:BoundField DataField ="CREATEDATE" HeaderText ="CREATEDATE" SortExpression ="CREATEDATE"/>
</列>
</asp:GridView>
</div>

/************************gridwiththemes.aspx.cs******************** *******/


使用系统;
使用System.Data;
使用System.Configuration;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.Data.SqlClient;
公共局部类gridwiththemes:BasePage
{
SqlConnection con =新的SqlConnection(ConfigurationManager.AppSettings ["MyConnection"].ToString());
SqlCommand cmd =新的SqlCommand();
DataTable dt = new DataTable();
受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!IsPostBack)
{
BindGrid();
}
}
受保护的void themelist_DataBound(对象发送者,EventArgs e)
{
themelist.SelectedValue = Page.Theme;
}
受保护的void themelist_SelectedIndexChanged(对象发送者,EventArgs e)
{
Session ["themename"] = themelist.SelectedValue;
Session.Add("MyTheme",Session ["themename"].ToString());
Server.Transfer(Request.FilePath);
}
私有void BindGrid()
{
con.Open();
cmd.Connection = con;
cmd.CommandText =从用户中选择*";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
SqlDataAdapter da =新的SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
gvUser.DataSource = dt;
gvUser.DataBind();
con.Close();
}
}





/***************************************************** ***************
创建App_Code文件夹并获取三个.cs文件
1. BasePage.cs
2. Theme.cs
3. ThemeManager.cs

/*************
1. BasePage.cs

使用系统;
使用System.Data;
使用System.Configuration;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;


公共类BasePage:System.Web.UI.Page
{
受保护的重写void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
如果(Session ["MyTheme"] == null)
{
Session.Add("MyTheme","first");
Page.Theme =((string)Session ["MyTheme"]);
}
其他
{
Page.Theme =((string)Session ["MyTheme"]);
}
}
}



/*************
1. Theme.cs

使用系统;
使用System.Data;
使用System.Configuration;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;


公共课主题
{
私人字串_name;
公共字符串名称
{
得到{return _name; }
设置{_name = value; }
}
公共主题(字符串名称)
{
名称=名称;
}
}


/*************
3. ThemeManager.cs

使用系统;
使用System.Data;
使用System.Configuration;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.Collections;
使用System.Collections.Generic;
使用System.IO;


公共类ThemeManager
{
公共静态List< Theme> GetThemes()
{
DirectoryInfo dInfo =新的DirectoryInfo(
System.Web.HttpContext.Current.Server.MapPath("App_Themes"));
DirectoryInfo [] dArrInfo = dInfo.GetDirectories();
列表<主题> list = new List< Theme>();
foreach(dArrInfo中的DirectoryInfo sDirectory)
{
主题临时=新主题(sDirectory.Name);
list.Add(temp);
}
返回列表;
}
}



/**********************

创建文件夹App_Themes
创建两个皮肤文件...您可以根据需要添加更多内容.
********************/
我们已经拿了两个皮肤文件
1.首先
2.秒

/****************头皮****************************** /

< asp:GridView runat ="server" AutoGenerateColumns ="False" BorderWidth ="1" CssClass ="GridViewStyle1" GridLines ="both" cellpadding ="3">
< AlternatingRowStyle Horizo​​ntalAlign = Center/>
< RowStyle Horizo​​ntalAlign = Center/>
< HeaderStyle Horizo​​ntalAlign = Center CssClass ="HeaderStyle1"/>
< PagerStyle Horizo​​ntalAlign = right CssClass ="PagerStyle1"/>
</asp:GridView>


< asp:Button runat =服务器" CssClass ="btnbghome"></asp:Button>

/****************第二种皮肤****************************** /





/***********************gridwiththemes.aspx*********************/

<asp:ObjectDataSource ID="ThemeDataSource" runat="server" SelectMethod="GetThemes"
TypeName="ThemeManager"></asp:ObjectDataSource>

<br />
<br />
<asp:DropDownList ID="themelist" runat="server" DataTextField="name" DataValueField="name"
OnSelectedIndexChanged="themelist_SelectedIndexChanged" DataSourceID="ThemeDataSource"
OnDataBound="themelist_DataBound" AutoPostBack="true">
<div>
<br />
<asp:Button ID="btnsubmit" Text="Submit" runat="server" />
</div>
<div>
<asp:GridView ID="gvUser" DataKeyNames="ID" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a id="linkDelete" runat="server" href="#">
<img src="Update.jpg" alt=""></a>
<asp:Button runat="server" CommandName="Delete" ID="btnDelete" Style="display: none;" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="select" Text="Edit" runat="server" CommandName="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
<asp:BoundField DataField="EMAIL" HeaderText="EMAIL" SortExpression="EMAIL" />
<asp:BoundField DataField="CREATEDATE" HeaderText="CREATEDATE" SortExpression="CREATEDATE" />
</Columns>
</asp:GridView>
</div>

/************************gridwiththemes.aspx.cs***************************/


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class gridwiththemes : BasePage
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyConnection"].ToString());
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void themelist_DataBound(object sender, EventArgs e)
{
themelist.SelectedValue = Page.Theme;
}
protected void themelist_SelectedIndexChanged(object sender, EventArgs e)
{
Session["themename"] = themelist.SelectedValue;
Session.Add("MyTheme", Session["themename"].ToString());
Server.Transfer(Request.FilePath);
}
private void BindGrid()
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "select * from USERS";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
gvUser.DataSource = dt;
gvUser.DataBind();
con.Close();
}
}





/****************************************************************
Create App_Code folder and take three .cs file
1. BasePage.cs
2. Theme.cs
3. ThemeManager.cs

/*************
1. BasePage.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class BasePage : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Session["MyTheme"] == null)
{
Session.Add("MyTheme", "first");
Page.Theme = ((string)Session["MyTheme"]);
}
else
{
Page.Theme = ((string)Session["MyTheme"]);
}
}
}



/*************
1. Theme.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class Theme
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public Theme(string name)
{
Name = name;
}
}


/*************
3. ThemeManager.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using System.IO;


public class ThemeManager
{
public static List<Theme> GetThemes()
{
DirectoryInfo dInfo = new DirectoryInfo(
System.Web.HttpContext.Current.Server.MapPath("App_Themes"));
DirectoryInfo[] dArrInfo = dInfo.GetDirectories();
List<Theme> list = new List<Theme>();
foreach (DirectoryInfo sDirectory in dArrInfo)
{
Theme temp = new Theme(sDirectory.Name);
list.Add(temp);
}
return list;
}
}



/**********************

Create folder App_Themes
Create two skin file...your can take more as per requirement.
********************/
we have took two skin file
1. first
2. second

/**************** first.skin ******************************/

<asp:GridView runat="server" AutoGenerateColumns="False" BorderWidth="1" CssClass="GridViewStyle1" GridLines="both" cellpadding="3">
<AlternatingRowStyle HorizontalAlign=Center />
<RowStyle HorizontalAlign=Center />
<HeaderStyle HorizontalAlign=Center CssClass="HeaderStyle1" />
<PagerStyle HorizontalAlign=right CssClass="PagerStyle1" />
</asp:GridView>


<asp:Button runat="server" CssClass="btnbghome"></asp:Button>

/**************** second.skin ******************************/


<asp:GridView runat="server" AutoGenerateColumns="False" BorderColor="#EBE4DE" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" EmptyDataText="There are no data records to display." ForeColor="Black" GridLines="Vertical" style="margin-top:2em;" width="100%"  skinid="glossygrid1">
<FooterStyle BackColor="#FAEBE7" />
<RowStyle BackColor="#EBE4DE"/>
<PagerStyle BackColor="#EBE4DE" ForeColor="#F369B1" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#FAEBE7" Font-Bold="True" ForeColor="573922" />
<HeaderStyle BackColor="#FAEBE7" Font-Bold="True" ForeColor="#573922" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Transparent" />
</asp:GridView>


<asp:Button runat="server" CssClass="btnbghome"></asp:Button>









/******************************获取CSS文件********









/**************************Take css file ********

.GridViewStyle1 { font-family: "Lucida Grande", "Trebuchet MS", Verdana, Helvetica, sans-serif; font-size: 11px; /*table-layout: auto;    */
    /*border-collapse: collapse;*/
    border:1px solid #000000; }
.GridViewStyle1 th td { border:1px solid #000000; }
.GridViewStyle1 td { border:1px solid #CCCCCC; border-bottom:1px solid #000000; border-left:1px solid #000000; border-right:1px solid #000000; }
.HeaderStyle1,
.PagerStyle1 /*Common Styles*/ { background-image: url(../../backoffice/assets/glossyback1.gif); background-position:center; background-repeat:repeat-x; background-color:#384F8D; height:15px; color:#FFFFFF; }
.HeaderStyle1 th { padding: 3px; color: #FFFFFF; border-top:1px solid #000000; border-left:1px solid #000000; border-right:1px solid #000000; }
.HeaderStyle1 a { text-decoration:none; color:#000000; display:block; text-align:left; font-weight:bold; }
.PagerStyle1 a {  color:#FFFFFF; display:block; text-align:left; font-weight:bold; padding:2px 2px 2px 2px;}
.PagerStyle1 span { font-weight:bold; color:#fff; text-decoration:none; padding:2px 2px 2px 2px; }
.RowStyle1 td,
.AltRowStyle1 td,
.SelectedRowStyle1 td,
.EditRowStyle1 td /*Common Styles*/ { padding: 3px; }
.RowStyle1 td { background-color: #ffffff; height: 15px; }
.AltRowStyle1 td { background-color: #B4D0F2; height: 15px; }
.SelectedRowStyle1 td { background-color: #ffff66; height: 15px; }
/* Grid Table Style End */

/*Glassy GridViewCSS Start*/
.GridViewStyle { font-family: "Lucida Grande", "Trebuchet MS", Verdana, Helvetica, sans-serif; font-size:small; table-layout: auto; border-collapse: collapse; border: #1d1d1d 5px solid; }
/*Header and Pager styles*/
.HeaderStyle,
.PagerStyle /*Common Styles*/ { background-image: url(../../backoffice/assets/HeaderGlassBlack.jpg); background-position:center; background-repeat:repeat-x; background-color:#1d1d1d; }
.HeaderStyle th { padding: 5px; color: #ffffff; }
.HeaderStyle a { text-decoration:none; color:#ffffff; display:block; text-align:left; font-weight:normal; }
 .PagerStyle table. {
 text-align:center;
 margin:auto;
}


这篇关于如何在母版页asp.net中设置动态主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆