我写的第一个CodeSmith模板(添加修改数据页面)

这是我用CodeSmith的一个页面添加修改数据表的模板
.CS

  1 <% @ CodeTemplate Language = " C# "  TargetLanguage = " C# "
  2       Description = " Generates a class including a special informational header "   %>
  3  
  4 <% @ Property Name = " NameSpace "  Type = " String "
  5       Category = " Context "
  6       Description = " The namespace to use for this class "   %>
  7     
  8 <% @ Property Name = " SourceTable "  Type = " SchemaExplorer.TableSchema "
  9        Category = " Context "
 10        Description = " Table that the stored procedures should be based on. "   %>
 11 <% @ Assembly Name = " SchemaExplorer "   %>
 12   
 13   <% @ Import Namespace = " SchemaExplorer "   %>
 14  
 15 <% @ Property Name = " folder "  Type = " String "
 16       Category = " Context "
 17       Description = " The name of the class to generate "   %>
 18  
 19 <% @ Property Name = " DevelopersName "  Type = " String "
 20       Category = " Context "
 21       Description = " The name to include in the comment header "   %>
 22 using  System;
 23 using  System.Collections;
 24 using  System.ComponentModel;
 25 using  System.Data;
 26 using  System.Drawing;
 27 using  System.Web;
 28 using  System.Web.SessionState;
 29 using  System.Web.UI;
 30 using  System.Web.UI.WebControls;
 31 using  System.Web.UI.HtmlControls;
 32
 33 namespace  QuMeiXJ.Webs. <%= folder  %>
 34 {
 35    public class <%= SourceTable.Name %>Add : QuMeiXJ.Webs.PageBase
 36    {
 37        <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++%>
 38        protected System.Web.UI.WebControls.TextBox txt<%= SourceTable.NonPrimaryKeyColumns[i].Name %>;
 39        <% }
 %>
 40        protected System.Web.UI.WebControls.Button btnSave<%= SourceTable.Name %>;
 41        protected System.Web.UI.WebControls.Label lbl<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>;
 42        private void Page_Load(object sender, System.EventArgs e)
 43        {
 44            if(!Page.IsPostBack)
 45            {
 46                if (Request.QueryString["<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>"== null || Request.QueryString["<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>"].ToString() == string.Empty || Convert.ToInt32(Request.QueryString["<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>"]) < 1)
 47                {
 48                    btnSave<%= SourceTable.Name %>.CommandName ="insert";
 49                    //DaoHang1.location = new string[3] {"????","?????","?????"};
 50                }

 51                else
 52                {
 53                    //DaoHang1.location = new string[3] {"????","?????","?????"};
 54                    btnSave<%= SourceTable.Name %>.CommandName = "update";
 55                    ShowData();
 56                }

 57            }

 58        }

 59        
 60        Web ??????????
 74        
 75        private void ShowData()
 76        {
 77            QuMeiXJ.BLL.<%= SourceTable.Name %> bll = new QuMeiXJ.BLL.<%= SourceTable.Name %>();
 78            QuMeiXJ.Model.<%= SourceTable.Name %> model = bll.GetModel (Convert.ToInt32(Request.QueryString["<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>"]));
 79            <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++%>
 80            txt<%= SourceTable.NonPrimaryKeyColumns[i].Name %>.Text = model.<%= SourceTable.NonPrimaryKeyColumns[i].Name %>.ToString();
 81            <% }
 %>
 82            lbl<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>.Text = model.<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>.ToString();
 83        }

 84        
 85        private void btnSave<%= SourceTable.Name %>_Click(object sender, System.EventArgs e)
 86        {
 87            if(btnSave<%= SourceTable.Name %>.CommandName == "insert")
 88            {
 89                Add();    
 90            }

 91            else
 92            {
 93                Edit();
 94            }

 95        }

 96        
 97        private QuMeiXJ.Model.<%= SourceTable.Name %> GetModel()
 98        {
 99            QuMeiXJ.Model.<%= SourceTable.Name %> model;
100            if(lbl<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>.Text != string.Empty)
101            {
102                QuMeiXJ.BLL .<%= SourceTable.Name %> bll = new QuMeiXJ.BLL.<%= SourceTable.Name %>();
103                model = bll.GetModel(Convert.ToInt32(lbl<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>.Text));
104            }

105            else
106                model = new QuMeiXJ.Model.<%= SourceTable.Name %>();
107            <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++%>
108            <% if(SourceTable.NonPrimaryKeyColumns[i].SystemType.ToString()=="System.String"{%>
109            model.<%= SourceTable.NonPrimaryKeyColumns[i].Name %> = txt<%= SourceTable.NonPrimaryKeyColumns[i].Name %>.Text.Trim();
110            <%}
%>
111            <%else{%>
112            model.<%= SourceTable.NonPrimaryKeyColumns[i].Name %> = <%= SourceTable.NonPrimaryKeyColumns[i].SystemType %>.Parse(txt<%= SourceTable.NonPrimaryKeyColumns[i].Name %>.Text.Trim());
113            <% }
}
 %>
114            return model;
115        }

116        
117        private void Edit()
118        {
119            QuMeiXJ.Model.<%= SourceTable.Name %> model = GetModel();
120            QuMeiXJ.BLL .<%= SourceTable.Name %> bll = new QuMeiXJ.BLL.<%= SourceTable.Name %>();
121            bll.Update(model);
122            MessageBox.Show(this,"????");
123        }

124        
125        
126        private void Add()
127        {
128            QuMeiXJ.Model.<%= SourceTable.Name %> model = GetModel();
129            QuMeiXJ.BLL .<%= SourceTable.Name %> bll = new QuMeiXJ.BLL.<%= SourceTable.Name %>();
130            bll.Add (model);
131            MessageBox.Show(this,"????");
132        }

133    }

134}
aspx:
 1 <% @ CodeTemplate Language="C#" TargetLanguage="C#"
 2      Description="Generates a class including a special informational header" 
%>
 3  
 4 <% Property Name="NameSpace" Type="String"
 5      Category="Context"
 6      Description="The namespace to use for this class" 
%>
 7     
 8 <% Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
 9       Category="Context"
10       Description="Table that the stored procedures should be based on." 
%>
11 <% @ Assembly Name="SchemaExplorer"  %>
12   
13   <% @ Import Namespace="SchemaExplorer"  %>
14  
15 <% Property Name="folder" Type="String"
16      Category="Context"
17      Description="The name of the class to generate" 
%>
18  
19 <% Property Name="DevelopersName" Type="String"
20      Category="Context"
21      Description="The name to include in the comment header" 
%>
22 <% %@ Register TagPrefix="uc1" TagName="DaoHang" Src="../Controls/DaoHang.ascx" % %>
23 <% %@ Page language="c#" Codebehind="<%= SourceTable.Name %>Add.aspx.cs" AutoEventWireup="false" Inherits="QuMeiXJ.Webs.<%=folder %>.<%= SourceTable.Name %>Add" % %>
24 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
25 < HTML >
26      < HEAD >
27          < title > <% =  SourceTable.Name  %> Add </ title >
28 < meta  content ="Microsoft Visual Studio .NET 7.1"  name =GENERATOR >
29 < meta  content =C#  name =CODE_LANGUAGE >
30 < meta  content =JavaScript  name =vs_defaultClientScript >
31 < meta  content =http://schemas.microsoft.com/intellisense/ie5  name =vs_targetSchema >
32 </ HEAD >
33
34 < body  MS_POSITIONING ="GridLayout" >
35 < form  id =Form1  method =post  runat ="server" >
36 < TABLE  id =Table2  cellSpacing =0  cellPadding =0  width ="100%"  border =0 >
37      < TR >
38          < TD >
39              < uc1:daohang  id =DaoHang1  runat ="server" ></ uc1:daohang >
40          </ TD >
41      </ TR >
42      < TR >
43          < TD  align ="center" >
44              < table >
45                  <%  for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) {  %>
46                  < tr >
47                      < th >
48                          <% =  SourceTable.NonPrimaryKeyColumns[i].Name  %>
49                      </ th >
50                      < td >
51                          < asp:textbox  id =txt<%=  SourceTable.NonPrimaryKeyColumns[i].Name % >  runat="server" Width="104px"> </ asp:textbox >
52                      </ td >
53                  </ tr >
54                  <%  }  %>
55                  < tr >
56                      < td >
57                          < asp:label  id ="lbl<%= SourceTable.PrimaryKey.MemberColumns[0].Name %>"   Visible ="false"  runat ="server" > Label </ asp:label >
58                      </ td >
59                      < td >
60                          < asp:button  id =btnSave<%=  SourceTable.Name % >  runat="server" Text="??"> </ asp:button >
61                      </ td >
62                  </ tr >
63              </ table >
64          </ TD >
65      </ TR >
66 </ Table >
67 </ form >
68 </ body >

你可能感兴趣的:(code)