从零开始完成一个三层架构案例-3(步骤02)

步骤02

2. 操作步骤(继续上个文档的操作)


2.15 创建一个新的D层类库项目(MySQL)

删掉默认带的Class1.cs,然后修改程序集名称和默认命名空间名称。
image.png

新建一个DLayer类
image.png

添加对Entity项目的引用
image.png

添加对MySQL的引用(这一步要求已经成功安装MySQL)
  C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies\v4.5
image.png

image.png

image.png

在MySQL WorkBench上新建数据库跟数据表
image.png

同样的,添加测试数据。
image.png

修改新项目的DLayer.cs的代码
引入需要的命名空间后,直接将原来对SQLserver的D层项目的DLayer代码直接复制过来即可,然后将conn、comm和dataadapter修改为对应的MySQL的类即可。
image.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using MySql.Data.MySqlClient;
using YYYDemo.Entity;

namespace YYYDemo.P_DLayer_MySQL
{
    public class DLayer
    {
        //创建数据库连接
        private MySqlConnection CreateConnection()
        {
            return new MySqlConnection(
                "Database=数据库名称;DataSource=127.0.0.1;User=root;Pwd=root;port=3306"
                );
        }
        //获取全部的账户信息,以Owner实体类类型返回
        public IList<Owner> GetAllAccount()
        {
            List<Owner> owners = new List<Owner>();

            MySqlConnection conn = CreateConnection();
            conn.Open();
            MySqlDataAdapter dAdp = new MySqlDataAdapter(
                "SELECT * FROM owner", conn
                );
            DataSet dSet = new DataSet();
            dAdp.Fill(dSet, "owner");
            for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
            {
                Owner owner = new Owner();
                owner.Oid = Convert.ToInt32(dSet.Tables[0].Rows[i][0].ToString());
                owner.Name = dSet.Tables[0].Rows[i][1].ToString();
                owner.Sex = Convert.ToChar(dSet.Tables[0].Rows[i][2].ToString());
                owner.Phone = dSet.Tables[0].Rows[i][3].ToString();
                owner.BuildNum = dSet.Tables[0].Rows[i][4].ToString();
                owner.Unit = dSet.Tables[0].Rows[i][5].ToString();
                owner.RoomNum = dSet.Tables[0].Rows[i][6].ToString();
                owners.Add(owner);
            }
            conn.Close();
            return owners;
        }
        //添加一个账户
        public void AddAnAccount(Owner owner)
        {
            MySqlConnection conn = CreateConnection();
            MySqlCommand cmd = new MySqlCommand(
                string.Format(
                "INSERT INTO owner(name,sex,phone,buildnum,unit,roomnum) " +
                "VALUES(N'{0}','{1}',N'{2}',N'{3}',N'{4}',N'{5}')"
                , owner.Name, owner.Sex, owner.Phone, owner.BuildNum, owner.Unit, owner.RoomNum),
                conn
                );
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        //通过手机号获得一个账户的信息,以Owner实体类类型返回;
        //这个函数是为了在B层通过手机号获取账户的id
        public Owner GetAnAccountByPhone_TO_Owner(string phone)
        {
            MySqlConnection conn = CreateConnection();
            conn.Open();
            MySqlDataAdapter dAdp = new MySqlDataAdapter(
                string.Format("SELECT * FROM owner WHERE phone=N'{0}'", phone),
                conn);
            DataSet dSet = new DataSet();
            dAdp.Fill(dSet, "owner");

            Owner owner = new Owner();
            owner.Oid = Convert.ToInt32(dSet.Tables[0].Rows[0][0].ToString());
            owner.Name = dSet.Tables[0].Rows[0][1].ToString();
            owner.Sex = Convert.ToChar(dSet.Tables[0].Rows[0][2].ToString());
            owner.Phone = dSet.Tables[0].Rows[0][3].ToString();
            owner.BuildNum = dSet.Tables[0].Rows[0][4].ToString();
            owner.Unit = dSet.Tables[0].Rows[0][5].ToString();
            owner.RoomNum = dSet.Tables[0].Rows[0][6].ToString();
            conn.Close();
            return owner;
        }
        //通过id获得一个账户的信息,以Owner实体类类型返回
        public Owner GetAnAccountByIndex_TO_Owner(int index)
        {
            MySqlConnection conn = CreateConnection();
            conn.Open();
            MySqlDataAdapter dAdp = new MySqlDataAdapter(
                string.Format("SELECT * FROM owner WHERE Oid={0}", index),
                conn);
            DataSet dSet = new DataSet();
            dAdp.Fill(dSet, "owner");

            Owner owner = new Owner();
            owner.Oid = Convert.ToInt32(dSet.Tables[0].Rows[0][0].ToString());
            owner.Name = dSet.Tables[0].Rows[0][1].ToString();
            owner.Sex = Convert.ToChar(dSet.Tables[0].Rows[0][2].ToString());
            owner.Phone = dSet.Tables[0].Rows[0][3].ToString();
            owner.BuildNum = dSet.Tables[0].Rows[0][4].ToString();
            owner.Unit = dSet.Tables[0].Rows[0][5].ToString();
            owner.RoomNum = dSet.Tables[0].Rows[0][6].ToString();
            conn.Close();
            return owner;
        }
        //修改一行信息
        public void UpdateAnAccount(Owner owner)
        {
            MySqlConnection conn = CreateConnection();
            MySqlCommand cmd = new MySqlCommand(
                string.Format(
                    "UPDATE owner SET name=N'{0}',sex='{1}',phone=N'{2}',buildnum=N'{3}',unit=N'{4}',roomnum=N'{5}' " +
                    "WHERE Oid={6}; ", owner.Name, owner.Sex, owner.Phone, owner.BuildNum, owner.Unit, owner.RoomNum, owner.Oid),
                conn
                );
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        //删除一行信息
        public void DeleteAnAccount(int index)
        {
            MySqlConnection conn = CreateConnection();
            MySqlCommand cmd = new MySqlCommand(
                string.Format("DELETE FROM owner WHERE Oid = {0}", index),
                conn);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

2.16 修改web.config,并设置两个D层项目的输出路径

image.png
未修改之前:
image.png

修改为下图所示:
 
 
image.png

修改两个D层项目的输出路径


看了一个评论,在这说明一下这个输出路径问题。

image.png

image.png


image.png

2.17 新建类库项目DAction(抽象数据访问层项目)

添加对Entity项目的引用
image.png

修改程序集名称和默认命名空间
image.png

添加一个新类(DAction_Class),并删掉原来的Class1.cs
(注:这里新建的类其实需要修改为接口interface类型,所以我这里为其取名为 DAction_Class 不是很合理)
image.png

修改DAction_Class的代码
image.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using YYYDemo.Entity;

namespace YYYDemo.DAction
{
    public interface DAction_Class
    {
        IList<Owner> GetAllAccount();
        void AddAnAccount(Owner owner);
        Owner GetAnAccountByPhone_TO_Owner(string phone);
        Owner GetAnAccountByIndex_TO_Owner(int index);
        void UpdateAnAccount(Owner owner);
        void DeleteAnAccount(int index);
    }
}

2.18 新建类库项目DFactory(数据访问层工厂)

image.png

修改程序集名称和默认命名空间
image.png

删掉原来的Class1.cs,新建一个新类DFactory_Class
image.png

添加对DAction项目的引用
image.png

添加下图所示的引用:
image.png

编辑DFactory_Class.cs的代码

作者:Bboy-AJ-任杰  
来源:CSDN  
原文:https://blog.csdn.net/u013201439/article/details/51161230


 

不要跟着课本的写,实现不了。我这里搜了好长时间,才解决的这个问题。

image.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Configuration;
using YYYDemo.DAction;
using System.Reflection;

namespace YYYDemo.DFactory
{
    public sealed class DFactory_Class//数据访问层工厂
    {
        private DFactory_Class() { }//数据访问层工厂构造器
        public static DAction_Class DriveDLayer()
        {
            string path = ConfigurationManager.AppSettings["DAL"];
            string className = path + ".DLayer";
            return Assembly.Load(path).CreateInstance(className) as DAction_Class;
        }
    }
}

2.19 修改两个数据访问层的代码

先让两个D层项目都添加对DAction项目的引用
image.png

P_DLayer_MySQL项目的DLayer.cs代码:
只是引入命名空间后,让其继承自 名为 DAction_Class 的这个接口
image.png

P_DLayer项目的DLayer.cs代码:(一样的操作)
image.png

2.20 修改B层项目

为其修改添加的引用,修改之前:
image.png

修改之后:(注意这里去掉了对P_DLayer项目的引用)
image.png

修改BLayer.cs的代码
image.png
也就是新增一个DAction接口类型的函数之后,将之前对 D层项目的类的引用,变成对接口类型变量的引用。

下面这几个函数中的引用变了,那两个验证函数之前并没有调用D层代码,所以不用变。
image.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//using YYYDemo.P_DLayer;
using System.Text.RegularExpressions;//正则表达式
using System.Runtime.InteropServices;//为了实现一个弹窗
using YYYDemo.Entity;
using YYYDemo.DAction;
using YYYDemo.DFactory;

namespace YYYDemo.P_BLayer
{
    public class BLayer
    {
        private DAction_Class DriveDLayer()
        {
            return DFactory_Class.DriveDLayer();
        }

        //提示框,这两行照着写上即可,不必理解原理,会使用就行
        [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr handle, String message, String title, int type);
        public IList<Owner> GetAllAccount()
        {
            //return new DLayer().GetAllAccount();
            return DriveDLayer().GetAllAccount();
        }
        string errorStr = "";//这个也可以设置成局部变量,只不过让RegularCheck_Add(Update)_TO_Bool函数多传递一个参数而已。
        //添加账户时正则表达式对输入数据的验证
        private bool RegularCheck_Add_TO_Bool(Owner _owner)
        {
            errorStr = "";
            bool flag = true;
            string rStr_Name = "^[\u4e00-\u9fa5]{2,4}$";//2-4个中文
            string rStr_Phone = @"^\d{11}$";//11位数字
            string rStr_BuildNum_Unit_RoomNum = @"^\d{1,3}$";//1-3位数字

            if (!Regex.IsMatch(_owner.Name, rStr_Name))
            {
                errorStr += "姓名应为2-4个汉字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.Phone, rStr_Phone))
            {
                errorStr += "号码应为11位数字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.BuildNum, rStr_BuildNum_Unit_RoomNum))
            {
                errorStr += "楼号应为1-3位数字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.Unit, rStr_BuildNum_Unit_RoomNum))
            {
                errorStr += "单元号应为1-3位数字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.RoomNum, rStr_BuildNum_Unit_RoomNum))
            {
                errorStr += "房间号应为1-3位数字!\n";
                flag = false;
            }

            var owners = GetAllAccount();

            bool equal_Phone = false;
            bool equal_Position = false;

            for (int i = 0; i < owners.Count; i++)
            {

                if (_owner.Phone == owners[i].Phone)
                    equal_Phone = true;

                if (
                    _owner.BuildNum == owners[i].BuildNum &&
                    _owner.Unit == owners[i].Unit &&
                    _owner.RoomNum == owners[i].RoomNum
                    )
                    equal_Position = true;
            }
            if (equal_Phone)
            {
                errorStr += "联系电话不能重复!\n";
                flag = false;
            }
            if (equal_Position)
            {
                errorStr += "住房位置不能重复!\n";
                flag = false;
            }
            
            return flag;
        }
        //添加一个账户
        public string AddAnAccount(Owner owner)
        {
            if (RegularCheck_Add_TO_Bool(owner))
            {
                errorStr = "新增业主信息成功!";
                DriveDLayer().AddAnAccount(owner);
            }
            else
                errorStr += "\n新增业主信息失败!";
            return errorStr;
        }
        //通过手机号获取该行数据的索引,在U层中为下面的GetAnAccountByIndex_TO_DataSet()函数提供作用
        public int GetIndexByPhone_TO_Int(string phone)
        {
            return Convert.ToInt32(
                DriveDLayer().GetAnAccountByPhone_TO_Owner(phone).Oid);
        }
        //通过索引获取该行数据的全部信息,以Owner实体类类型返回
        public Owner GetAnAccountBydIndex_TO_Owner(int index)
        {
            return DriveDLayer().GetAnAccountByIndex_TO_Owner(index);
        }
        //修改账户时正则表达式对输入数据的验证
        //修改个人信息,需要验证 手机号 和 住房位置 是否 *跟别人* 重复;还需验证数据是否合理
        private bool RegularCheck_Update_TO_Bool(Owner _owner)
        {
            errorStr = "";
            bool flag = true;
            string rStr_Name = "^[\u4e00-\u9fa5]{2,4}$";
            string rStr_Phone = @"^\d{11}$";
            string rStr_BuildNum_Unit_RoomNum = @"^\d{1,3}$";

            if (!Regex.IsMatch(_owner.Name, rStr_Name))
            {
                errorStr += "姓名应为2-4个汉字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.Phone, rStr_Phone))
            {
                errorStr += "号码应为11位数字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.BuildNum, rStr_BuildNum_Unit_RoomNum))
            {
                errorStr += "楼号应为1-3位数字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.Unit, rStr_BuildNum_Unit_RoomNum))
            {
                errorStr += "单元号应为1-3位数字!\n";
                flag = false;
            }
            if (!Regex.IsMatch(_owner.RoomNum, rStr_BuildNum_Unit_RoomNum))
            {
                errorStr += "房间号应为1-3位数字!\n";
                flag = false;
            }
            
            var owners = GetAllAccount();

            bool equal_Phone = false;
            bool equal_Position = false;

            for (int i = 0; i < owners.Count; i++)
            {
                if (owners[i].Oid != _owner.Oid)
                {
                    if (_owner.Phone == owners[i].Phone)
                        equal_Phone = true;

                    if (
                        _owner.BuildNum == owners[i].BuildNum &&
                        _owner.Unit == owners[i].Unit &&
                        _owner.RoomNum == owners[i].RoomNum
                        )
                        equal_Position = true;
                }

            }
            if (equal_Phone)
            {
                errorStr += "联系电话不能重复!\n";
                flag = false;
            }
            if (equal_Position)
            {
                errorStr += "住房位置不能重复!\n";
                flag = false;
            }
            
            return flag;
        }
        //修改一个账户的信息
        public string UpdateAnAccount(Owner owner)
        {
            if (RegularCheck_Update_TO_Bool(owner))
            {
                errorStr = "修改业主信息成功!";
                DriveDLayer().UpdateAnAccount(owner);
            }
            else
                errorStr += "\n修改业主信息失败!";
            return errorStr;
        }
        //删除一个账户
        public string DeleteAnAccount(int index)
        {
            errorStr = "删除业主信息成功!";
            DriveDLayer().DeleteAnAccount(index);
            return errorStr;
        }

    }
}

修改完代码之后最好也重新生成一下解决方案吧。

以后在切换数据库的时候,只需要在web.config中将需要的数据库的值显示出来,其余的数据库的值隐藏即可。
image.png

这里使用MySQL数据库,运行测试。
image.png
运行正常。

目前为止的项目之间的引用关系图:
 
image.png


分割线三

在这之前的 2.15-2.20 ,就是完成了对不同数据库的支持,吐血制作啊。搞了一晚上。

2019-5-24 20:50:36


2.21 添加主题文件夹

2019-5-28 10:24:10
image.png

image.png

image.png

image.png

2.22 修改ShowAll.aspx中的GridView的属性样式设置

回到ShowAll.aspx页面,修改GridView的样式
①:修改格式
image.png

image.png

image.png

②:所有列的居中设置
image.png

对所有字段进行如下的居中设置。(通过属性对于样式的设置会映射到.aspx代码)
image.png

③:在设计面板的GridView的属性面板上,进行如下设置:
image.png

image.png

④:
现在在VS的设计界面,显示为下图所示:
image.png

源码:
先为GridView添加一个SkinID
image.png

<asp:GridView SkinID="ShowAll_GridView" ID="gView" runat="server"
                AllowPaging="True"
                AutoGenerateColumns="False" OnPageIndexChanging="gView_PageIndexChanging"
                OnRowCommand="gView_RowCommand"
                CellPadding="4" ForeColor="#333333" GridLines="None"
                Font-Names="楷体" Font-Size="16pt" HorizontalAlign="Center" Width="100%"
                >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Sex" HeaderText="性别" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Phone" HeaderText="联系电话" SortExpression="Phone" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="BuildNum" HeaderText="楼号" SortExpression="BuildNum" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Unit" HeaderText="单元" SortExpression="Unit" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="RoomNum" HeaderText="房间号" SortExpression="RoomNum" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>

                    <asp:ButtonField CommandName="Y_Update" HeaderText="操作" ShowHeader="True" Text="修改">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:ButtonField>
                    <asp:ButtonField CommandName="Y_Delete" HeaderText="操作" ShowHeader="True" Text="删除" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:ButtonField>
                </Columns>
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>

将上面的选中代码复制到Skin1文件上,但是要删掉   和   和  
在Skin1.skin文件中的代码如下:

<asp:GridView SkinID="ShowAll_GridView" runat="server"
                AllowPaging="True"
                AutoGenerateColumns="False" 
                CellPadding="4" ForeColor="#333333" GridLines="None"
                Font-Names="楷体" Font-Size="16pt" HorizontalAlign="Center" Width="100%"
                >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Sex" HeaderText="性别" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Phone" HeaderText="联系电话" SortExpression="Phone" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="BuildNum" HeaderText="楼号" SortExpression="BuildNum" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Unit" HeaderText="单元" SortExpression="Unit" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="RoomNum" HeaderText="房间号" SortExpression="RoomNum" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>

                    <asp:ButtonField CommandName="Y_Update" HeaderText="操作" ShowHeader="True" Text="修改">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:ButtonField>
                    <asp:ButtonField CommandName="Y_Delete" HeaderText="操作" ShowHeader="True" Text="删除" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:ButtonField>
                </Columns>
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>

然后在ShowAll.aspx页面撤销对其进行的样式修改(因为要实现样式与页面分离,所以在页面上不应该有对样式的设置)
image.png

在下图的代码行中,添加Theme属性
image.png

截止到目前ShowAll.aspx的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowAll.aspx.cs"
    Inherits="P_ULayer.ShowAll" Theme="主题1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>小区业主列表</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            
            <div id="Div_lbl_Topic">
                <asp:Label runat="server" ID="lbl_Topic" Text="小 区 业 主 列 表"></asp:Label>
            </div>

            <!--<div>
                <asp:Button ID="btn1" runat="server" Text="  1  " OnClick="btn1_Click" />
                <asp:Button ID="btn2" runat="server" Text="  2  " OnClick="btn2_Click" />
            </div>-->

            <div>
                <br /><br />
                <asp:HyperLink Target="_self" NavigateUrl="~/AddAccount.aspx" runat="server" ID="hLink_AddAccount" Text="新增业主"></asp:HyperLink>
                <br /><br /><br /><br />
            </div>

            <asp:GridView SkinID="ShowAll_GridView" ID="gView" runat="server"
                AllowPaging="True"
                AutoGenerateColumns="False"
                OnPageIndexChanging="gView_PageIndexChanging"
                OnRowCommand="gView_RowCommand">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" />
                    <asp:BoundField DataField="Sex" HeaderText="性别" />
                    <asp:BoundField DataField="Phone" HeaderText="联系电话" SortExpression="Phone" />
                    <asp:BoundField DataField="BuildNum" HeaderText="楼号" SortExpression="BuildNum" />
                    <asp:BoundField DataField="Unit" HeaderText="单元" SortExpression="Unit" />
                    <asp:BoundField DataField="RoomNum" HeaderText="房间号" SortExpression="RoomNum" />

                    <asp:ButtonField CommandName="Y_Update" HeaderText="操作" ShowHeader="True" Text="修改" />
                    <asp:ButtonField CommandName="Y_Delete" HeaderText="操作" ShowHeader="True" Text="删除" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

2.23 为 主题1 添加CSS

在主题1文件夹下面,添加CSS样式表,并拖进一张图片来,当做背景图
image.png

修改StyleSheet1.css:

body {
    width: 100%;
    background-attachment: fixed;
    background-image: url(BgImage3.jpg);
    background-repeat: no-repeat;
    background-size: 100% 100%;
}

#form1 {
    width: 60%;
    margin: 0 auto;
    font-family: "楷体";
}

#Div_lbl_Topic {
    margin: 5% auto 0;
    width: 80%;
    font-size: 80px;
    text-align: center;
    color: cyan;
}

#hLink_AddAccount {
    font-size: 36px;
    color: blue;
    float: right;
}

运行测试:
image.png

现在已经通过css和aspx的属性设置完成了对ShowAll.aspx页面的样式设置,下面为内容页进行样式设置。

2.24 为内容页进行样式设置

之前在设计的时候,为母版页的TextBox控件进行了字体16的样式设置,现在全部删除
image.png

继续修改CSS样式
image.png

#hLink_AddAccount {
    font-size: 36px;
    color: blue;
    float: right;
}

/*上面为ShowAll*/

body {
    width: 100%;
    background-attachment: fixed;
    background-image: url(BgImage3.jpg);
    background-repeat: no-repeat;
    background-size: 100% 100%;
}
#form1 {
    width: 60%;
    margin: 0 auto;
    font-family: "楷体";
}
#Div_lbl_Topic { 
    margin: 5% auto 0;
    width: 80%;
    font-size: 80px;
    text-align: center;
    color: cyan;
}

/*下面为两个内容页*/
#Div_hLink_TO_01 {
    font-size: 40px;
    margin-left: 5%;
    margin-top: 2%;
}
#tableUI {
    font-size: 30px;
    font-family: "楷体";
    margin: 0 auto;
    width: 100%;
}
.td_Left {
    text-align: right;
}
#Div_btnSubmit {
    margin: 0 auto;
    width: 100px;
}
.CSS_class_btnSubmit {
    font-size: 32px;
    font-family: "楷体";
    color: black;
    background-color: cyan;
}



修改Skin1.skin
image.png

<asp:GridView SkinID="ShowAll_GridView" runat="server"
                AllowPaging="True"
                AutoGenerateColumns="False" 
                CellPadding="4" ForeColor="#333333" GridLines="None"
                Font-Names="楷体" Font-Size="16pt" HorizontalAlign="Center" Width="100%"
                >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Sex" HeaderText="性别" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Phone" HeaderText="联系电话" SortExpression="Phone" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="BuildNum" HeaderText="楼号" SortExpression="BuildNum" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Unit" HeaderText="单元" SortExpression="Unit" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="RoomNum" HeaderText="房间号" SortExpression="RoomNum" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>

                    <asp:ButtonField CommandName="Y_Update" HeaderText="操作" ShowHeader="True" Text="修改">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:ButtonField>
                    <asp:ButtonField CommandName="Y_Delete" HeaderText="操作" ShowHeader="True" Text="删除" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:ButtonField>
                </Columns>
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>


<asp:TextBox Font-Size="16pt" Font-Names="楷体" runat="server"></asp:TextBox>

然后在两个内容页的第一行代码上都添加Theme属性
image.png

image.png

运行测试:
image.png

image.png

image.png

2.25 随意再添加几个主题

这里就随便设置啦,只要保存在不同的主题文件夹下即可(如下图所示),然后修改ShowAll.aspx  AddAccount.aspx  AlterAccount.aspx 三个页面的Theme属性为"主题2"即可
image.png

image.png

下图是我设置的主题2:
image.png

image.png

image.png

2.26 实现主题的动态切换

修改ShowAll.aspx的代码
image.png

然后在两个主题的Css中修改新增的控件的样式
image.png

image.png

在ShowAll.aspx.cs修改代码。
image.png

在AlterAccount.aspx.cs修改代码。image.png

在AddAccount.aspx.cs修改代码。image.png

2019-5-28 14:03:31

运行测试:(自己测试跳转就可以)
image.png

我这里正常。2019-5-28 19:27:54


分割线四

2019-5-30 00:27:15
我又对样式稍作修改,并添加了一个主题,现在一共三个主题。
因为要交项目报告了,也因为没有太多的时间,所以极有可能是最终版本了。没有添加上的功能,比如图片上传作为头像、多种选择,可能就不会添加了,但也说不定。。。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值