從零開始完成一個三層架構案例-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
我又對樣式稍作修改,並添加了一個主題,現在一共三個主題。
因爲要交項目報告了,也因爲沒有太多的時間,所以極有可能是最終版本了。沒有添加上的功能,比如圖片上傳作爲頭像、多種選擇,可能就不會添加了,但也說不定。。。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章