在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原程序,可以从下面地址下载:http://download.cnblogs.com/insus/ASPDOTNET/Ref_Org.rar 数据库SQL Server 2008 R2,数据名为[Demo],它有四张表[UnitCode1], [UnitCode2], [UnitCode3], [UnitCode4],每个表有几个字段[Unit1~4],[Description],[CreateBy],[CreateDate],[UpdateBy],[UpdateDate],此四个表的主键分别为[Unit1],[Unit2],[Unit3]和[Unit4],其余字段名称四张表都一样。数据库还有各个表的相关的存储过程。 程序中有一个接口,是为了设置网页标题。数据库四张表对应的类别,程序应用了母版,有5个网页,Default.aspx, UnitCode1.aspx, UnitCode2.aspx, UnitCode3.aspx和UnitCode4.aspx 。每个网页分别也是对各自的表进行添加,显示,更新以及删除记录的功能。 首先我们对比四个类别中,属性部分,只有 private string _Unit1; public string Unit1 { get { return _Unit1; } set { _Unit1 = value; } }
BusinessBase objBusinessBase = new BusinessBase();
BaseUnitCode.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; /// <summary> /// Summary description for BaseUnitCode /// </summary> namespace Insus.NET { public class BaseUnitCode { private string _Description; private string _CreateBy; private string _UpdateBy; public string Description { get { return _Description; } set { _Description = value; } } public string CreateBy { get { return _CreateBy; } set { _CreateBy = value; } } public string UpdateBy { get { return _UpdateBy; } set { _UpdateBy = value; } } BusinessBase objBusinessBase = new BusinessBase(); public BaseUnitCode() { // // TODO: Add constructor logic here // } protected DataTable GetAll(string procedureName) { return objBusinessBase.GetDataToDataSet(procedureName).Tables[0]; } protected void Insert(string paramName, string paramValue, string procedurename) { Parameter[] parameter = { new Parameter (paramName,SqlDbType.NVarChar,-1,paramValue), new Parameter ("@Description",SqlDbType.NVarChar,-1,_Description), new Parameter ("@CreateBy",SqlDbType.NVarChar,-1,_CreateBy) }; objBusinessBase.ExecuteProcedure(procedurename, parameter); } protected void Update(string paramName, string paramValue, string procedurename) { Parameter[] parameter = { new Parameter (paramName,SqlDbType.NVarChar,-1,paramValue), new Parameter ("@Description",SqlDbType.NVarChar,-1,_Description), new Parameter ("@UpdateBy",SqlDbType.NVarChar,-1,_UpdateBy) }; objBusinessBase.ExecuteProcedure(procedurename, parameter); } protected void Delete(string paramName, string paramValue, string procedurename) { Parameter[] parameter = { new Parameter (paramName,SqlDbType.NVarChar,-1,paramValue) }; objBusinessBase.ExecuteProcedure(procedurename, parameter); } } }
下面是重构好的类别:
UnitCode1.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; /// <summary> /// Summary description for UnitCode1 /// </summary> namespace Insus.NET { public class UnitCode1 : BaseUnitCode { private string _Unit1; public string Unit1 { get { return _Unit1; } set { _Unit1 = value; } } public UnitCode1() { // // TODO: Add constructor logic here // } public DataTable GetAll() { return GetAll("usp_UnitCode1_GetAll"); } public void Insert() { Insert("@Unit1", _Unit1, "usp_UnitCode1_Insert"); } public void Update() { Update("@Unit1", _Unit1, "usp_UnitCode1_Update"); } public void Delete() { Delete("@Unit1", _Unit1, "usp_UnitCode1_Delete"); } } }
UnitCode2.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; /// <summary> /// Summary description for UnitCode1 /// </summary> namespace Insus.NET { public class UnitCode2 : BaseUnitCode { private string _Unit2; public string Unit2 { get { return _Unit2; } set { _Unit2 = value; } } public UnitCode2() { // // TODO: Add constructor logic here // } public DataTable GetAll() { return GetAll("usp_UnitCode2_GetAll"); } public void Insert() { Insert("@Unit2", _Unit2, "usp_UnitCode2_Insert"); } public void Update() { Update("@Unit2", _Unit2, "usp_UnitCode2_Update"); } public void Delete() { Delete("@Unit2", _Unit2, "usp_UnitCode2_Delete"); } } }
UnitCode3.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; /// <summary> /// Summary description for UnitCode3 /// </summary> namespace Insus.NET { public class UnitCode3 : BaseUnitCode { private string _Unit3; public string Unit3 { get { return _Unit3; } set { _Unit3 = value; } } public UnitCode3() { // // TODO: Add constructor logic here // } public DataTable GetAll() { return GetAll("usp_UnitCode3_GetAll"); } public void Insert() { Insert("@Unit3", _Unit3, "usp_UnitCode3_Insert"); } public void Update() { Update("@Unit3", _Unit3, "usp_UnitCode3_Update"); } public void Delete() { Delete("@Unit3", _Unit3, "usp_UnitCode3_Delete"); } } }
UnitCode4.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; /// <summary> /// Summary description for UnitCode1 /// </summary> namespace Insus.NET { public class UnitCode4 : BaseUnitCode { private string _Unit4; public string Unit4 { get { return _Unit4; } set { _Unit4 = value; } } public UnitCode4() { // // TODO: Add constructor logic here // } public DataTable GetAll() { return GetAll("usp_UnitCode4_GetAll"); } public void Insert() { Insert("@Unit4", _Unit4, "usp_UnitCode4_Insert"); } public void Update() { Update("@Unit4", _Unit4, "usp_UnitCode4_Update"); } public void Delete() { Delete("@Unit4", _Unit4, "usp_UnitCode4_Delete"); } } }
BasePage.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for BasePage /// </summary> namespace Insus.NET { public class BasePage : System.Web.UI.Page { protected InsusJsUtility objInsusJsUtility = new InsusJsUtility(); } } ((ISetValable)this.Master).SetValue("单位码X");
对这部分的重构,只有创建用户控件(ascx),然后搬移过去,完成之后,再把这个用户控件拉至网页.aspx中:
InsertForm.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="InsertForm.ascx.cs" Inherits="Sys_InsertForm" %> <table class="table"> <tr class="tableRow"> <td class="tableCell" style="width: 35%;">单位码</td> <td class="tableCell">说明</td> <td style="width: 12%; text-align: center;" class="tableCell">操作</td> </tr> <tr> <td class="tableCell"> <asp:TextBox ID="TextBoxUnitCode" runat="server" CssClass="textbox" BackColor="#ffff6f"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBoxUnitCode" Display="none" ErrorMessage="单位码必须填写。" ValidationGroup="GeneralInsert"></asp:RequiredFieldValidator> </td> <td class="tableCell"> <asp:TextBox ID="TextBoxDescription" runat="server" CssClass="textbox"></asp:TextBox> </td> <td style="width: 12%;" class="tableCell"> <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true" ShowMessageBox="true" ShowSummary="false" ValidationGroup="GeneralInsert" /> <asp:Button ID="ButtonCreate" runat="server" OnClick="ButtonCreate_Click" Text="创建" ValidationGroup="GeneralInsert" /> </td> </tr> </table>
用户控件重构好之后,当然需要拉至网页中去,有一个地方是需要注意的,在用户控件,还要写上OnClick事件,OnClick="ButtonCreate_Click": 改程序,就得一步一个脚印,现在我们把目光放在每个.aspx的GridView控件上,它是显示记录,编辑记录以及删除等功能集成。有很高的相似度。只是Gridview的ID,DataKeyNames,以及OnRowEditing,OnRowCancelingEdit,OnRowUpdating,OnRowDeleting事件名称不一样,最后是绑定主键时,也不一样: <ItemTemplate> <%# Eval("UnitX") %> </ItemTemplate>
改完之后,如下代码一样,Insus.NET只列了个网页,如Unitcode4(部分): <asp:GridView ID="GridViewUnitCode" runat="server" DataKeyNames="Unit4" AutoGenerateColumns="false" ShowHeader="false" CellPadding="2" CellSpacing="0" Width="100%" BorderWidth="1px" BorderColor="#c0c0c0" BorderStyle="solid" HeaderStyle-Height="25" RowStyle-Height="25" HeaderStyle-BackColor="#efebde" OnRowEditing="GridViewUnitCode_RowEditing" OnRowCancelingEdit="GridViewUnitCode_RowCancelingEdit" OnRowUpdating="GridViewUnitCode_RowUpdating" OnRowDeleting="GridViewUnitCode_RowDeleting"> <Columns> <asp:TemplateField> <ItemStyle BorderStyle="solid" BorderWidth="1px" BorderColor="#c0c0c0" Width="35%" /> <ItemTemplate> <%# Eval("Unit4") %> </ItemTemplate> </asp:TemplateField>
OperationForm.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="OperationForm.ascx.cs" Inherits="Sys_OperationForm" %> <asp:GridView ID="GridViewUnitCode" runat="server" AutoGenerateColumns="false" ShowHeader="false" CellPadding="2" CellSpacing="0" Width="100%" BorderWidth="1px" BorderColor="#c0c0c0" BorderStyle="solid" HeaderStyle-Height="25" RowStyle-Height="25" HeaderStyle-BackColor="#efebde" OnRowEditing="GridViewUnitCode_RowEditing" OnRowCancelingEdit="GridViewUnitCode_RowCancelingEdit" OnRowUpdating="GridViewUnitCode_RowUpdating" OnRowDeleting="GridViewUnitCode_RowDeleting" OnRowDataBound="GridViewUnitCode_RowDataBound"> <Columns> <asp:TemplateField> <ItemStyle BorderStyle="solid" BorderWidth="1px" BorderColor="#c0c0c0" Width="35%" /> <ItemTemplate> <asp:Label ID="LabelUnitCode" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemStyle BorderStyle="solid" BorderWidth="1px" BorderColor="#c0c0c0" /> <ItemTemplate> <%# Eval("Description") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBoxDescription" runat="server" Text='<%# Eval("Description") %>' CssClass="textbox"></asp:TextBox> </EditItemTemplate> & |
请发表评论