以前一直从事C/S的项目比较多,所以XCodeFactory以前也只支持根据数据库表结构生成对应的WinForm,最近正在接手一个B/S项目,有很多表单需要录入,手动写这些录入、修改页面非常繁琐。于是修改了XCodeFactory来自动生成这些Page,然后在生成代码的基础上把布局修改一下加入美工就可以了。
下面举个简单的例子,一个表CQActivity中仅仅包含两个字段:ID和Name。数据实体代码如下:
CQActivity
public class CQActivity
{
public CQActivity()
{
}
#region FieldName ,用于强化静态检查
public const string _ID = "ID" ;
public const string _Name = "Name" ;
#endregion
#region ID
private string iD = "" ;
/// <summary>
/// ID 活动编号。
/// </summary>
public string ID
{
get
{
return this.iD ;
}
set
{
this.iD = value ;
}
}
#endregion
#region Name
private string name = "" ;
/// <summary>
/// Name 活动名称。
/// </summary>
public string Name
{
get
{
return this.name ;
}
set
{
this.name = value ;
}
}
#endregion
#region ToString
public override string ToString()
{
return this.ID.ToString() + " " + this.Name.ToString() ;
}
#endregion
}
public class CQActivity
{
public CQActivity()
{
}
#region FieldName ,用于强化静态检查
public const string _ID = "ID" ;
public const string _Name = "Name" ;
#endregion
#region ID
private string iD = "" ;
/// <summary>
/// ID 活动编号。
/// </summary>
public string ID
{
get
{
return this.iD ;
}
set
{
this.iD = value ;
}
}
#endregion
#region Name
private string name = "" ;
/// <summary>
/// Name 活动名称。
/// </summary>
public string Name
{
get
{
return this.name ;
}
set
{
this.name = value ;
}
}
#endregion
#region ToString
public override string ToString()
{
return this.ID.ToString() + " " + this.Name.ToString() ;
}
#endregion
}
对应生长的WebForm CS文件:
CQActivityForm
public class CQActivityForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label_ID;
protected System.Web.UI.WebControls.TextBox TextBox_ID;
protected System.Web.UI.WebControls.Label Label_Name;
protected System.Web.UI.WebControls.TextBox TextBox_Name;
protected System.Web.UI.WebControls.Button Button_addUpdate;
#region Auto Members
private bool isUpdate = false ;
#region Page_Load
private void Page_Load(object sender, System.EventArgs e)
{
if(this.Request.QueryString["ID"] != null)
{
this.Button_addUpdate.Text = "修 改" ;
this.isUpdate = true ;
this.TextBox_ID.ReadOnly = true ;
if(! this.IsPostBack)
{
string theID = this.Request.QueryString["ID"].ToString() ;
this.DisplayObjectInfo(theID) ;
}
}
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button_addUpdate.Click += new System.EventHandler(this.Button_addUpdate_Click);
this.Load += new System.EventHandler(this.Page_Load);
#region CreateObjectFromTextbox
private CQActivity CreateObjectFromTextbox()
{
CQActivity a_cQActivity = new CQActivity() ;
a_cQActivity.ID = this.TextBox_ID.Text.Trim() ;
a_cQActivity.Name = this.TextBox_Name.Text.Trim() ;
return a_cQActivity ;
}
#endregion
#region DisplayObjectInfo
private void DisplayObjectInfo(string theID)
{
CQActivity a_cQActivity = (CQActivity)Global.DataEntryToCQ.GetAObjectEspecial(typeof(CQActivity) ,theID) ;
if(a_cQActivity == null)
{
return ;
}
this.TextBox_ID.Text = a_cQActivity.ID.ToString() ;
this.TextBox_Name.Text = a_cQActivity.Name.ToString() ;
}
#endregion
#region buttonClick
private void Button_addUpdate_Click(object sender, System.EventArgs e)
{
try
{
CQActivity a_cQActivity = this.CreateObjectFromTextbox() ;
if(! this.isUpdate)
{
Global.DataEntryToCQ.Insert(a_cQActivity ,null) ;
}
else
{
Global.DataEntryToCQ.Update(a_cQActivity ,null) ;
}
}
catch(Exception ee)
{
ee = ee ;
}
}
#endregion
}
public class CQActivityForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label_ID;
protected System.Web.UI.WebControls.TextBox TextBox_ID;
protected System.Web.UI.WebControls.Label Label_Name;
protected System.Web.UI.WebControls.TextBox TextBox_Name;
protected System.Web.UI.WebControls.Button Button_addUpdate;
#region Auto Members
#endregion
private bool isUpdate = false ;
#region Page_Load
private void Page_Load(object sender, System.EventArgs e)
{
if(this.Request.QueryString["ID"] != null)
{
this.Button_addUpdate.Text = "修 改" ;
this.isUpdate = true ;
this.TextBox_ID.ReadOnly = true ;
if(! this.IsPostBack)
{
string theID = this.Request.QueryString["ID"].ToString() ;
this.DisplayObjectInfo(theID) ;
}
}
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button_addUpdate.Click += new System.EventHandler(this.Button_addUpdate_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#region CreateObjectFromTextbox
private CQActivity CreateObjectFromTextbox()
{
CQActivity a_cQActivity = new CQActivity() ;
a_cQActivity.ID = this.TextBox_ID.Text.Trim() ;
a_cQActivity.Name = this.TextBox_Name.Text.Trim() ;
return a_cQActivity ;
}
#endregion
#region DisplayObjectInfo
private void DisplayObjectInfo(string theID)
{
CQActivity a_cQActivity = (CQActivity)Global.DataEntryToCQ.GetAObjectEspecial(typeof(CQActivity) ,theID) ;
if(a_cQActivity == null)
{
return ;
}
this.TextBox_ID.Text = a_cQActivity.ID.ToString() ;
this.TextBox_Name.Text = a_cQActivity.Name.ToString() ;
}
#endregion
#region buttonClick
private void Button_addUpdate_Click(object sender, System.EventArgs e)
{
try
{
CQActivity a_cQActivity = this.CreateObjectFromTextbox() ;
if(! this.isUpdate)
{
Global.DataEntryToCQ.Insert(a_cQActivity ,null) ;
}
else
{
Global.DataEntryToCQ.Update(a_cQActivity ,null) ;
}
}
catch(Exception ee)
{
ee = ee ;
}
}
#endregion
}
对应的ASPX的Html:
Aspx
<%@ Page language="c#" Codebehind="CQActivityForm.aspx.cs" AutoEventWireup="false" Inherits="MGPMIS.Data.MGPMIS.CQActivityForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CQActivityForm</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="Label_ID" style="Z-INDEX:101; LEFT:24px; POSITION:absolute; TOP:16px" runat="server"
Width="120px" Height="24px">活动编号</asp:Label>
<asp:TextBox id="TextBox_ID" style="Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 16px"
runat="server" Width="240px"></asp:TextBox>
<asp:Label id="Label_Name" style="Z-INDEX:103; LEFT:24px; POSITION:absolute; TOP:48px" runat="server"
Width="120px" Height="24px">活动名称</asp:Label>
<asp:TextBox id="TextBox_Name" style="Z-INDEX: 104; LEFT: 152px; POSITION: absolute; TOP: 48px"
runat="server" Width="240px"></asp:TextBox>
<asp:Button id="Button_addUpdate" style="Z-INDEX: 105; LEFT: 24px; POSITION: absolute; TOP: 80px"
runat="server" Width="80px" Text="添加"></asp:Button>
</form>
</body>
</HTML>
<%@ Page language="c#" Codebehind="CQActivityForm.aspx.cs" AutoEventWireup="false" Inherits="MGPMIS.Data.MGPMIS.CQActivityForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CQActivityForm</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="Label_ID" style="Z-INDEX:101; LEFT:24px; POSITION:absolute; TOP:16px" runat="server"
Width="120px" Height="24px">活动编号</asp:Label>
<asp:TextBox id="TextBox_ID" style="Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 16px"
runat="server" Width="240px"></asp:TextBox>
<asp:Label id="Label_Name" style="Z-INDEX:103; LEFT:24px; POSITION:absolute; TOP:48px" runat="server"
Width="120px" Height="24px">活动名称</asp:Label>
<asp:TextBox id="TextBox_Name" style="Z-INDEX: 104; LEFT: 152px; POSITION: absolute; TOP: 48px"
runat="server" Width="240px"></asp:TextBox>
<asp:Button id="Button_addUpdate" style="Z-INDEX: 105; LEFT: 24px; POSITION: absolute; TOP: 80px"
runat="server" Width="80px" Text="添加"></asp:Button>
</form>
</body>
</HTML>
升级你当前的XCodeFactory即可获得此功能。内部版本号:2.998
如果还没有安装,下载地址为:http://xcodefactory.cnblogs.com/Files/XCodeFactory/XCodeFactory3.0Setup.rar