小弟关注cnblogs有一段时间了,每天上班第一件事是打开博客园关注里面的最新博客(当然开会或其它特殊情况就例外了,哈哈),这段时间工作不忙,刚刚申请开通了属于自己的blog,没想到cnblogs的管理员办事效率如此之高(真是令人倍感兴奋!),从提交申请到审核通过只等待了31分钟,在此先谢过啦。这是我的第一篇blog,希望各位帅哥美女不要扔太多砖头鸡蛋(小弟害怕太暴力哦!),写得不好的地方,也希望大家多多批评指正,好了,废话不多说了,今天介绍自己的一个小作品,通过建立数据库表,简单配置xml,生成添加信息的页面,希望各位XDJM能从中有所收获.
下面以一个简单的新闻管理应用介绍
第一步 建立数据库表(数据库为sqlserver 2005)
T_NEWSTYPE(新闻类别)
ID(int 自动编号),NAME (varchar(50))
T_INFO(新闻)
ID(char(36)),TITLE(varchar(100)),CONTENT(varchar(max)),ADDTIME(smalldatetime),CLICKNUM(int),TYPE(varchar(10))
ID(int 自动编号),NAME (varchar(50))
T_INFO(新闻)
ID(char(36)),TITLE(varchar(100)),CONTENT(varchar(max)),ADDTIME(smalldatetime),CLICKNUM(int),TYPE(varchar(10))
第二步 建立数据表对应的xml文件
代码
<table id="99" key="id" name="T_NEWSTYPE" cnname="新闻类别" order="id desc">
<column name="ID" cnname="编号" ftable="" fvalue="" ftext="" isfilter="n" visible="y" allownull="n" regex="" tip="" type=""></column>
<column name="NAME" cnname="类别名称" ftable="" fvalue="" ftext="" isfilter="y" visible="y" allownull="n" regex="" tip="" type="textbox"></column>
</table>
<table id="100" key="id" name="T_Info" cnname="新闻" order="addtime desc,id desc">
<column name="ID" cnname="编号" ftable="" fvalue="" ftext="" isfilter="n" visible="y" allownull="n" regex="" tip="" type="key"></column>
<column name="TITLE" cnname="标题" ftable="" fvalue="" ftext="" isfilter="y" visible="y" allownull="n" regex="" tip="" type="textbox"></column>
<column name="file" cnname="附件" ftable="" fvalue="" ftext="" isfilter="n" visible="n" allownull="n" regex="" tip="" type="file"></column>
<column name="TYPE" cnname="类别" ftable="T_NEWSTYPE" fvalue="ID" ftext="NAME" isfilter="n" visible="y" allownull="n" regex="" tip="" type="dropdownlist"></column>
<column name="CONTENT" cnname="内容" ftable="" fvalue="" ftext="" isfilter="n" visible="n" allownull="n" regex="" tip="" type="fck"></column>
<column name="ADDTIME" cnname="添加时间" ftable="" fvalue="" ftext="" isfilter="n" visible="y" allownull="n" regex="" tip="" type="autodatetime"></column>
</table>
<column name="ID" cnname="编号" ftable="" fvalue="" ftext="" isfilter="n" visible="y" allownull="n" regex="" tip="" type=""></column>
<column name="NAME" cnname="类别名称" ftable="" fvalue="" ftext="" isfilter="y" visible="y" allownull="n" regex="" tip="" type="textbox"></column>
</table>
<table id="100" key="id" name="T_Info" cnname="新闻" order="addtime desc,id desc">
<column name="ID" cnname="编号" ftable="" fvalue="" ftext="" isfilter="n" visible="y" allownull="n" regex="" tip="" type="key"></column>
<column name="TITLE" cnname="标题" ftable="" fvalue="" ftext="" isfilter="y" visible="y" allownull="n" regex="" tip="" type="textbox"></column>
<column name="file" cnname="附件" ftable="" fvalue="" ftext="" isfilter="n" visible="n" allownull="n" regex="" tip="" type="file"></column>
<column name="TYPE" cnname="类别" ftable="T_NEWSTYPE" fvalue="ID" ftext="NAME" isfilter="n" visible="y" allownull="n" regex="" tip="" type="dropdownlist"></column>
<column name="CONTENT" cnname="内容" ftable="" fvalue="" ftext="" isfilter="n" visible="n" allownull="n" regex="" tip="" type="fck"></column>
<column name="ADDTIME" cnname="添加时间" ftable="" fvalue="" ftext="" isfilter="n" visible="y" allownull="n" regex="" tip="" type="autodatetime"></column>
</table>
第三步 将xml中表对应的id传到这个添加信息的页面,生成添加类别和添加新闻信息页面的效果如下
添加成功后,返回列表页面,如下图:(如果有需要,列表和编辑页面在后面的系列再介绍,如果有人看懂了设计的思路自己实现了,我就不献丑了)
点击查看链接,查看刚才添加的新闻,如下图:
以上是实现的效果图,添加功能的aspx页面源码如下:
代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="commonAdd.aspx.cs" Inherits="webadmin_common_commonAdd" %>
2
3 <%@ Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>
4
5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7 <html xmlns="http://www.w3.org/1999/xhtml" >
8 <head runat="server">
9 <title>无标题页</title>
10 <link href="../css/style.css" rel="stylesheet" type="text/css" />
11 </head>
12 <body>
13 <form id="form1" runat="server">
14 <table width="100%">
15 <tr>
16 <td class="sitemap"><span><img src="../images/home.jpg" /></span>您当前的位置:<asp:Literal ID="tableCnName" runat="server"></asp:Literal>管理</td>
17 </tr>
18 </table>
19 <asp:PlaceHolder ID="contentPlaceHolder" runat="server"></asp:PlaceHolder>
20 </form>
21 </body>
22 </html>
2
3 <%@ Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>
4
5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7 <html xmlns="http://www.w3.org/1999/xhtml" >
8 <head runat="server">
9 <title>无标题页</title>
10 <link href="../css/style.css" rel="stylesheet" type="text/css" />
11 </head>
12 <body>
13 <form id="form1" runat="server">
14 <table width="100%">
15 <tr>
16 <td class="sitemap"><span><img src="../images/home.jpg" /></span>您当前的位置:<asp:Literal ID="tableCnName" runat="server"></asp:Literal>管理</td>
17 </tr>
18 </table>
19 <asp:PlaceHolder ID="contentPlaceHolder" runat="server"></asp:PlaceHolder>
20 </form>
21 </body>
22 </html>
页面放了两个控件,ID为tableCnName的literal显示表的中文名字,ID为contentPlaceHolder的控件放置根据xml配置动态添加的控件
aspx.cs后台代码如下:
代码
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 using System.Xml;
14 using Personal.Wgc.Diys.DBUtility;
15 using Personal.Wgc.Diys.WebTools;
16 using System.Collections.Generic;
17 using System.Text;
18 using System.Data.Common;
19 using Personal.Wgc.Diys.CommonDiyControls;
20 using Personal.Wgc.Diys.Dal;
21
22 public partial class webadmin_common_commonAdd : System.Web.UI.Page
23 {
24 private XmlDocument doc = new XmlDocument();
25 public webadmin_common_commonAdd()
26 {
27 doc.Load(Server.MapPath("http://www.cnblogs.com/app_data/config.xml"));
28 }
29 protected void Page_Load(object sender, EventArgs e)
30 {
31 //if (UserService.GetInstance().GetLoginedUserTicket() < 1)
32 //{
33 // this.ClientScript.RegisterClientScriptBlock(GetType(), "", "top.location='../default.aspx'", true);
34 // return;
35 //}
36 if (string.IsNullOrEmpty(Request.QueryString["id"]))
37 {
38 Response.Write("页面缺少生成参数,生成失败");
39 Response.End();
40 }
41 if (ViewState["columnList"] == null)
42 {
43 InitialXmlInfo();
44 }
45 BuildPage();
46 }
47 private void InitialXmlInfo()
48 {
49 List<T_COLUMN> columnList = new List<T_COLUMN>();
50 T_COLUMN column;
51 XmlNode node = doc.SelectSingleNode("/root/table[@id='"+Request.QueryString["id"]+"']");
52 if (node != null)
53 {
54 if (node.ChildNodes.Count == 0)
55 {
56 Response.Write("表配置不正确,页面生成失败!");
57 Response.End();
58 }
59 tableCnName.Text = node.Attributes["cnname"].Value;
60 foreach (XmlNode columnNode in node.ChildNodes)
61 {
62 column = new T_COLUMN();
63 column.AllowNull = columnNode.Attributes["allownull"].Value.ToString().ToUpper() == "Y" ? true : false;
64 column.CnName = columnNode.Attributes["cnname"].Value;
65 column.FTable = columnNode.Attributes["ftable"].Value;
66 column.FText = columnNode.Attributes["ftext"].Value;
67 column.FValue = columnNode.Attributes["fvalue"].Value;
68 column.IsFilter = columnNode.Attributes["isfilter"].Value.ToString().ToUpper() == "Y" ? true : false;
69 column.Key = node.Attributes["key"].Value;
70 column.Name = columnNode.Attributes["name"].Value;
71 column.Regex = columnNode.Attributes["regex"].Value;
72 column.TableName = node.Attributes["name"].Value;
73 column.Type = columnNode.Attributes["type"].Value;
74 column.Visible = columnNode.Attributes["visible"].Value.ToString().ToUpper() == "Y" ? true : false;
75 column.Tip = columnNode.Attributes["tip"].Value;
76 columnList.Add(column);
77 }
78 ViewState["columnList"] = columnList;
79 }
80 else
81 {
82 Response.Write("不存在此节点");
83 Response.End();
84 }
85 }
86 private void BuildPage()
87 {
88 Table myTable = new Table();
89 myTable.Attributes.Add("cellpadding", "4");
90 myTable.Attributes.Add("cellspacing", "1");
91 myTable.Style.Add(HtmlTextWriterStyle.Width, "100%");
92 if (ViewState["columnList"] == null)
93 {
94 InitialXmlInfo();
95 }
96 List<T_COLUMN> columnList = (List<T_COLUMN>)ViewState["columnList"];
97 foreach (T_COLUMN column in columnList)
98 {
99 TableRow tr = new TableRow();
100 tr.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
101 tr.Style.Add(HtmlTextWriterStyle.Height, "30px");
102 tr.Style.Add(HtmlTextWriterStyle.Padding, "5");
103 tr.Style.Add(HtmlTextWriterStyle.FontSize, "14px");
104 TableCell tc = new TableCell();
105 tc.Text = column.CnName;
106 tr.Cells.Add(tc);
107 tc = new TableCell();
108 if (column.Type.ToLower() == "textbox")
109 {
110 TextBox tb = new TextBox();
111 tb.ID = column.Name;
112 tb.ToolTip = column.Tip;
113 tb.Style.Add(HtmlTextWriterStyle.Width, "400px");
114 tc.Controls.Add(tb);
115 if (!column.AllowNull)
116 {
117 Literal msg = new Literal();
118 msg.Text = "<font color=\"red\">*</font>";
119 tc.Controls.Add(msg);
120 RequiredFieldValidator rfv = new RequiredFieldValidator();
121 rfv.ControlToValidate = column.Name;
122 rfv.ErrorMessage = "请填写";
123 tc.Controls.Add(rfv);
124 }
125 if (!string.IsNullOrEmpty(column.Regex.Trim()))
126 {
127 RegularExpressionValidator rev = new RegularExpressionValidator();
128 rev.ControlToValidate = column.Name;
129 rev.ErrorMessage = "格式错误!";
130 rev.ValidationExpression = column.Regex;
131 tc.Controls.Add(rev);
132 }
133 tr.Cells.Add(tc);
134 }
135 else if (column.Type.ToLower() == "mtextbox")
136 {
137 TextBox tb = new TextBox();
138 tb.ID = column.Name;
139 tb.TextMode = TextBoxMode.MultiLine;
140 tb.ToolTip = column.Tip;
141 tb.Style.Add(HtmlTextWriterStyle.Width, "90%");
142 tb.Style.Add(HtmlTextWriterStyle.Height, "30px");
143 tc.Controls.Add(tb);
144 if (!column.AllowNull)
145 {
146 Literal msg = new Literal();
147 msg.Text = "<font color=\"red\">*</font>";
148 tc.Controls.Add(msg);
149 RequiredFieldValidator rfv = new RequiredFieldValidator();
150 rfv.ControlToValidate = column.Name;
151 rfv.ErrorMessage = "请填写";
152 tc.Controls.Add(rfv);
153 }
154 tr.Cells.Add(tc);
155 }
156 else if (column.Type.ToLower() == "timetextbox")
157 {
158
159 Personal.Wgc.Diys.CommonDiyControls.DatePicker.DropDownDatePicker tb = new Personal.Wgc.Diys.CommonDiyControls.DatePicker.DropDownDatePicker();
160 tb.Style.Add(HtmlTextWriterStyle.Width, "200px");
161 tb.ID = column.Name;
162 tc.Controls.Add(tb);
163 tr.Controls.Add(tc);
164 }
165 else if (column.Type.ToLower() == "numbertextbox")
166 {
167 Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.NumberTextBox tb = new Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.NumberTextBox();
168 tb.Style.Add(HtmlTextWriterStyle.Width, "200px");
169 tb.ID = column.Name;
170 tb.ToolTip = column.Tip;
171 tc.Controls.Add(tb);
172 if (!column.AllowNull)
173 {
174 Literal msg = new Literal();
175 msg.Text = "<font color=\"red\">*</font>";
176 tc.Controls.Add(msg);
177 RequiredFieldValidator rfv = new RequiredFieldValidator();
178 rfv.ControlToValidate = column.Name;
179 rfv.ErrorMessage = "请填写";
180 tc.Controls.Add(rfv);
181 }
182 tr.Controls.Add(tc);
183 }
184 else if (column.Type.ToLower() == "maxlengthtextbox")
185 {
186 Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.MaxLengthTextBox tb = new Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.MaxLengthTextBox();
187 tb.ID = column.Name;
188 if (column.FText == "1")
189 {
190 tb.TextMode = TextBoxMode.MultiLine;
191 tb.Style.Add(HtmlTextWriterStyle.Width, "80%");
192 tb.Style.Add(HtmlTextWriterStyle.Height, "50px");
193 if (!string.IsNullOrEmpty(column.FValue))
194 {
195 tb.Length = int.Parse(column.FValue);
196 }
197 }
198 else
199 {
200 tb.Style.Add(HtmlTextWriterStyle.Width, "400px");
201 if (!string.IsNullOrEmpty(column.FValue))
202 {
203 tb.MaxLength = int.Parse(column.FValue);
204 }
205 }
206 tc.Controls.Add(tb);
207 if (!column.AllowNull)
208 {
209 Literal msg = new Literal();
210 msg.Text = "<font color=\"red\">*</font>";
211 tc.Controls.Add(msg);
212 RequiredFieldValidator rfv = new RequiredFieldValidator();
213 rfv.ControlToValidate = column.Name;
214 rfv.ErrorMessage = "请填写";
215 tc.Controls.Add(rfv);
216 }
217 tr.Cells.Add(tc);
218 }
219 else if (column.Type.ToLower() == "autodatetime")
220 {
221 TextBox tb = new TextBox();
222 tb.Style.Add(HtmlTextWriterStyle.Width, "200px");
223 tb.Text = DateTime.Now.ToString();
224 tb.Enabled = false;
225 tb.ID = column.Name;
226 tb.ToolTip = column.Tip;
227 tc.Controls.Add(tb);
228 tr.Cells.Add(tc);
229 }
230 else if (column.Type.ToLower() == "fck")
231 {
232 FredCK.FCKeditorV2.FCKeditor fckEditor = new FredCK.FCKeditorV2.FCKeditor();
233 fckEditor.ID = column.Name;
234 fckEditor.Width = new Unit(800);
235 fckEditor.Height = new Unit(400);
236 tc.Controls.Add(fckEditor);
237 tr.Cells.Add(tc);
238 }
239 else if (column.Type.ToLower() == "dropdownlist")
240 {
241 DropDownList ddl = new DropDownList();
242 ddl.ID = column.Name;
243 SQLData sqldata = new SQLData();
244 sqldata.SqlString = "select * from " + column.FTable + "";
245 SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnString, sqldata);
246 while (dr.Read())
247 {
248 ddl.Items.Add(new ListItem(dr[column.FText].ToString(), dr[column.FValue].ToString()));
249 }
250 dr.Close();
251 tc.Controls.Add(ddl);
252 tr.Cells.Add(tc);
253 }
254 else if (column.Type.ToLower() == "key")
255 {
256 tc.Text = string.Empty;
257 tr.Cells.Add(tc);
258 }
259 else if (string.IsNullOrEmpty(column.Type.Trim()))
260 {
261 tc.Text = string.Empty;
262 tr.Cells.Add(tc);
263 }
264 else if (column.Type.ToLower() == "file")
265 {
266 this.ClientScript.RegisterClientScriptInclude("jquery","http://www.cnblogs.com/js/jquery.js");
267 this.ClientScript.RegisterClientScriptInclude("moreFile","http://www.cnblogs.com/js/addmorefile.js");
268 FileUpload file = new FileUpload();
269 file.ID = column.Name;
270 HtmlInputButton inputButton = new HtmlInputButton();
271 inputButton.Attributes.Add("onclick", "addFile()");
272 inputButton.Attributes.Add("value", "更多附件");
273 tc.Controls.Add(file);
274 tc.Controls.Add(inputButton);
275 HtmlGenericControl span = new HtmlGenericControl("span");
276 span.ID = "moreFile";
277 span.Style.Add("margin-top", "0px");
278 tc.Controls.Add(span);
279 tr.Controls.Add(tc);
280 }
281 if (column.Type.ToLower() == "key" || string.IsNullOrEmpty(column.Type.Trim()))
282 {
283 //如果是主键,或自动增加列
284 // donothing
285 }
286 else
287 {
288 myTable.Rows.Add(tr);
289 }
290 }
291 TableRow trOperation = new TableRow();
292 TableCell tcOperation = new TableCell();
293 tcOperation.Text = string.Empty;
294 tcOperation.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
295 trOperation.Cells.Add(tcOperation);
296 tcOperation = new TableCell();
297 tcOperation.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
298 Button buttonSave = new Button();
299 buttonSave.Text = "保 存";
300 buttonSave.Click+=new EventHandler(buttonSave_Click);
301 Literal backButton = new Literal();
302 backButton.Text = " <input type=\"button\" value=\"返回\" onclick=\"location='commonquery.aspx?id=" + Request.QueryString["id"] + "'\"/>";
303 tcOperation.Controls.Add(buttonSave);
304 tcOperation.Controls.Add(backButton);
305 trOperation.Controls.Add(tcOperation);
306 myTable.Rows.Add(trOperation);
307 contentPlaceHolder.Controls.Add(myTable);
308 }
309 protected void buttonSave_Click(object sender, EventArgs e)
310 {
311 //添加附件用的关联编号
312 string id = Guid.NewGuid().ToString();
313 //处理附件用
314 List<Personal.Wgc.Diys.Model.T_DATA> fileList = new List<Personal.Wgc.Diys.Model.T_DATA>();
315
316 List<T_COLUMN> columnList = (List<T_COLUMN>)ViewState["columnList"];
317
318 if (columnList.Count > 0)
319 {
320 List<DbParameter> sqlParameterList = new List<DbParameter>();
321 StringBuilder sql = new StringBuilder();
322 StringBuilder paras = new StringBuilder();
323 paras.Append("values (");
324 sql.Append("insert into ");
325 sql.Append(columnList[0].TableName);
326 sql.Append("(");
327 foreach (T_COLUMN column in columnList)
328 {
329 //如果不是自增长列
330 if (!string.IsNullOrEmpty(column.Type.Trim()))
331 {
332 //如果不是附件
333 if (column.Type.ToLower() != "file")
334 {
335 sql.Append(column.Name);
336 sql.Append(",");
337 paras.Append("@");
338 paras.Append(column.Name);
339 paras.Append(",");
340 }
341 else
342 {
343 //含有附件
344 Personal.Wgc.Diys.Model.T_DATA file;
345 for (int i = 0; i < Request.Files.Count; i++)
346 {
347 //如果附件不为空
348 if (!string.IsNullOrEmpty(Request.Files[i].FileName))
349 {
350 file = new Personal.Wgc.Diys.Model.T_DATA();
351 file.ID = Guid.NewGuid().ToString();
352 file.LINKID = id;
353 file.NAME = Request.Files[i].FileName.Substring(Request.Files[i].FileName.LastIndexOf("\\") + 1);
354 file.SUFFIX = Request.Files[i].FileName.Substring(Request.Files[i].FileName.LastIndexOf('.') + 1).ToLower();
355 byte[] fileContent = new byte[Request.Files[i].ContentLength];
356 Request.Files[i].InputStream.Read(fileContent, 0, fileContent.Length);
357 file.CONTENT = fileContent;
358 fileList.Add(file);
359 }
360 }
361 }
362 }
363 //-------------------parameters deal
364 if (column.Type.ToLower() == "textbox" || column.Type.ToLower() == "mtextbox" || column.Type.ToLower() == "numbertextbox" || column.Type.ToLower() == "moneytextbox" || column.Type.ToLower() == "maxlengthtextbox" || column.Type.ToLower() == "autodatetime")
365 {
366 TextBox tb = this.FindControl(column.Name) as TextBox;
367 if (tb != null)
368 {
369 sqlParameterList.Add(new SqlParameter("@" + column.Name, WebUtility.CleanString(tb.Text.Trim())));
370 }
371 }
372 else if (column.Type.ToLower() == "dropdownlist")
373 {
374 DropDownList ddl = this.FindControl(column.Name) as DropDownList;
375 if (ddl != null)
376 {
377 sqlParameterList.Add(new SqlParameter("@" + column.Name, ddl.SelectedValue));
378 }
379 }
380 else if (column.Type.ToLower() == "fck")
381 {
382 FredCK.FCKeditorV2.FCKeditor fck = this.FindControl(column.Name) as FredCK.FCKeditorV2.FCKeditor;
383 if (fck != null)
384 {
385 sqlParameterList.Add(new SqlParameter("@" + column.Name,WebUtility.CleanString(fck.Value)));
386 }
387 }
388 else if (column.Type.ToLower() == "timetextbox")
389 {
390 TextBox tb = this.FindControl(column.Name) as TextBox;
391 if (tb != null)
392 {
393 sqlParameterList.Add(new SqlParameter("@" + column.Name, WebUtility.CleanString(tb.Text.Trim())));
394 }
395 }
396 else if (column.Type.ToLower() == "key")
397 {
398 sqlParameterList.Add(new SqlParameter("@" + column.Name, id));
399 }
400 else
401 {
402 //暂时默认为自动增长列
403 }
404 }
405 sql.Remove(sql.Length - 1, 1);
406 sql.Append(")");
407 paras.Remove(paras.Length - 1, 1);
408 paras.Append(")");
409 SQLData sqldata = new SQLData();
410 sqldata.SqlString = sql.ToString() + paras.ToString();
411 sqldata.SqlParameter = sqlParameterList;
412 try
413 {
414 if (SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, sqldata) > 0)
415 {
416 //如果有附件
417 if (fileList.Count > 0)
418 {
419 List<SQLData> sqldataList = new List<SQLData>();
420 foreach (Personal.Wgc.Diys.Model.T_DATA item in fileList)
421 {
422 if (item.SUFFIX != "exe" && item.SUFFIX != "jar" && item.SUFFIX != "dll")
423 {
424 sqldata = new SQLData();
425 sqldata.SqlString = "insert into t_data(id,linkid,suffix,name,content) values(@id,@linkid,@suffix,@name,@content)";
426 sqldata.SqlParameter.Add(new SqlParameter("@id", item.ID));
427 sqldata.SqlParameter.Add(new SqlParameter("@linkid",id));
428 sqldata.SqlParameter.Add(new SqlParameter("@suffix",item.SUFFIX));
429 sqldata.SqlParameter.Add(new SqlParameter("@name", item.NAME));
430 sqldata.SqlParameter.Add(new SqlParameter("@content", item.CONTENT));
431 sqldataList.Add(sqldata);
432 }
433 }
434 if (sqldataList.Count > 0)
435 {
436 try
437 {
438 SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, sqldataList);
439 }
440 catch
441 {
442 // do nothing
443 }
444 }
445 }
446 this.ClientScript.RegisterClientScriptBlock(GetType(), "add", "alert('添加成功');location='commonquery.aspx?id=" + Request.QueryString["id"] + "'", true);
447 }
448 else
449 {
450 this.ClientScript.RegisterClientScriptBlock(GetType(), "add", "alert('添加失败')", true);
451 }
452 }
453 catch
454 {
455 this.ClientScript.RegisterClientScriptBlock(GetType(), "add", "alert('出现异常操作失败')", true);
456 }
457 }
458 }
459 }
460
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 using System.Xml;
14 using Personal.Wgc.Diys.DBUtility;
15 using Personal.Wgc.Diys.WebTools;
16 using System.Collections.Generic;
17 using System.Text;
18 using System.Data.Common;
19 using Personal.Wgc.Diys.CommonDiyControls;
20 using Personal.Wgc.Diys.Dal;
21
22 public partial class webadmin_common_commonAdd : System.Web.UI.Page
23 {
24 private XmlDocument doc = new XmlDocument();
25 public webadmin_common_commonAdd()
26 {
27 doc.Load(Server.MapPath("http://www.cnblogs.com/app_data/config.xml"));
28 }
29 protected void Page_Load(object sender, EventArgs e)
30 {
31 //if (UserService.GetInstance().GetLoginedUserTicket() < 1)
32 //{
33 // this.ClientScript.RegisterClientScriptBlock(GetType(), "", "top.location='../default.aspx'", true);
34 // return;
35 //}
36 if (string.IsNullOrEmpty(Request.QueryString["id"]))
37 {
38 Response.Write("页面缺少生成参数,生成失败");
39 Response.End();
40 }
41 if (ViewState["columnList"] == null)
42 {
43 InitialXmlInfo();
44 }
45 BuildPage();
46 }
47 private void InitialXmlInfo()
48 {
49 List<T_COLUMN> columnList = new List<T_COLUMN>();
50 T_COLUMN column;
51 XmlNode node = doc.SelectSingleNode("/root/table[@id='"+Request.QueryString["id"]+"']");
52 if (node != null)
53 {
54 if (node.ChildNodes.Count == 0)
55 {
56 Response.Write("表配置不正确,页面生成失败!");
57 Response.End();
58 }
59 tableCnName.Text = node.Attributes["cnname"].Value;
60 foreach (XmlNode columnNode in node.ChildNodes)
61 {
62 column = new T_COLUMN();
63 column.AllowNull = columnNode.Attributes["allownull"].Value.ToString().ToUpper() == "Y" ? true : false;
64 column.CnName = columnNode.Attributes["cnname"].Value;
65 column.FTable = columnNode.Attributes["ftable"].Value;
66 column.FText = columnNode.Attributes["ftext"].Value;
67 column.FValue = columnNode.Attributes["fvalue"].Value;
68 column.IsFilter = columnNode.Attributes["isfilter"].Value.ToString().ToUpper() == "Y" ? true : false;
69 column.Key = node.Attributes["key"].Value;
70 column.Name = columnNode.Attributes["name"].Value;
71 column.Regex = columnNode.Attributes["regex"].Value;
72 column.TableName = node.Attributes["name"].Value;
73 column.Type = columnNode.Attributes["type"].Value;
74 column.Visible = columnNode.Attributes["visible"].Value.ToString().ToUpper() == "Y" ? true : false;
75 column.Tip = columnNode.Attributes["tip"].Value;
76 columnList.Add(column);
77 }
78 ViewState["columnList"] = columnList;
79 }
80 else
81 {
82 Response.Write("不存在此节点");
83 Response.End();
84 }
85 }
86 private void BuildPage()
87 {
88 Table myTable = new Table();
89 myTable.Attributes.Add("cellpadding", "4");
90 myTable.Attributes.Add("cellspacing", "1");
91 myTable.Style.Add(HtmlTextWriterStyle.Width, "100%");
92 if (ViewState["columnList"] == null)
93 {
94 InitialXmlInfo();
95 }
96 List<T_COLUMN> columnList = (List<T_COLUMN>)ViewState["columnList"];
97 foreach (T_COLUMN column in columnList)
98 {
99 TableRow tr = new TableRow();
100 tr.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
101 tr.Style.Add(HtmlTextWriterStyle.Height, "30px");
102 tr.Style.Add(HtmlTextWriterStyle.Padding, "5");
103 tr.Style.Add(HtmlTextWriterStyle.FontSize, "14px");
104 TableCell tc = new TableCell();
105 tc.Text = column.CnName;
106 tr.Cells.Add(tc);
107 tc = new TableCell();
108 if (column.Type.ToLower() == "textbox")
109 {
110 TextBox tb = new TextBox();
111 tb.ID = column.Name;
112 tb.ToolTip = column.Tip;
113 tb.Style.Add(HtmlTextWriterStyle.Width, "400px");
114 tc.Controls.Add(tb);
115 if (!column.AllowNull)
116 {
117 Literal msg = new Literal();
118 msg.Text = "<font color=\"red\">*</font>";
119 tc.Controls.Add(msg);
120 RequiredFieldValidator rfv = new RequiredFieldValidator();
121 rfv.ControlToValidate = column.Name;
122 rfv.ErrorMessage = "请填写";
123 tc.Controls.Add(rfv);
124 }
125 if (!string.IsNullOrEmpty(column.Regex.Trim()))
126 {
127 RegularExpressionValidator rev = new RegularExpressionValidator();
128 rev.ControlToValidate = column.Name;
129 rev.ErrorMessage = "格式错误!";
130 rev.ValidationExpression = column.Regex;
131 tc.Controls.Add(rev);
132 }
133 tr.Cells.Add(tc);
134 }
135 else if (column.Type.ToLower() == "mtextbox")
136 {
137 TextBox tb = new TextBox();
138 tb.ID = column.Name;
139 tb.TextMode = TextBoxMode.MultiLine;
140 tb.ToolTip = column.Tip;
141 tb.Style.Add(HtmlTextWriterStyle.Width, "90%");
142 tb.Style.Add(HtmlTextWriterStyle.Height, "30px");
143 tc.Controls.Add(tb);
144 if (!column.AllowNull)
145 {
146 Literal msg = new Literal();
147 msg.Text = "<font color=\"red\">*</font>";
148 tc.Controls.Add(msg);
149 RequiredFieldValidator rfv = new RequiredFieldValidator();
150 rfv.ControlToValidate = column.Name;
151 rfv.ErrorMessage = "请填写";
152 tc.Controls.Add(rfv);
153 }
154 tr.Cells.Add(tc);
155 }
156 else if (column.Type.ToLower() == "timetextbox")
157 {
158
159 Personal.Wgc.Diys.CommonDiyControls.DatePicker.DropDownDatePicker tb = new Personal.Wgc.Diys.CommonDiyControls.DatePicker.DropDownDatePicker();
160 tb.Style.Add(HtmlTextWriterStyle.Width, "200px");
161 tb.ID = column.Name;
162 tc.Controls.Add(tb);
163 tr.Controls.Add(tc);
164 }
165 else if (column.Type.ToLower() == "numbertextbox")
166 {
167 Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.NumberTextBox tb = new Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.NumberTextBox();
168 tb.Style.Add(HtmlTextWriterStyle.Width, "200px");
169 tb.ID = column.Name;
170 tb.ToolTip = column.Tip;
171 tc.Controls.Add(tb);
172 if (!column.AllowNull)
173 {
174 Literal msg = new Literal();
175 msg.Text = "<font color=\"red\">*</font>";
176 tc.Controls.Add(msg);
177 RequiredFieldValidator rfv = new RequiredFieldValidator();
178 rfv.ControlToValidate = column.Name;
179 rfv.ErrorMessage = "请填写";
180 tc.Controls.Add(rfv);
181 }
182 tr.Controls.Add(tc);
183 }
184 else if (column.Type.ToLower() == "maxlengthtextbox")
185 {
186 Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.MaxLengthTextBox tb = new Personal.Wgc.Diys.CommonDiyControls.DiyTextBox.MaxLengthTextBox();
187 tb.ID = column.Name;
188 if (column.FText == "1")
189 {
190 tb.TextMode = TextBoxMode.MultiLine;
191 tb.Style.Add(HtmlTextWriterStyle.Width, "80%");
192 tb.Style.Add(HtmlTextWriterStyle.Height, "50px");
193 if (!string.IsNullOrEmpty(column.FValue))
194 {
195 tb.Length = int.Parse(column.FValue);
196 }
197 }
198 else
199 {
200 tb.Style.Add(HtmlTextWriterStyle.Width, "400px");
201 if (!string.IsNullOrEmpty(column.FValue))
202 {
203 tb.MaxLength = int.Parse(column.FValue);
204 }
205 }
206 tc.Controls.Add(tb);
207 if (!column.AllowNull)
208 {
209 Literal msg = new Literal();
210 msg.Text = "<font color=\"red\">*</font>";
211 tc.Controls.Add(msg);
212 RequiredFieldValidator rfv = new RequiredFieldValidator();
213 rfv.ControlToValidate = column.Name;
214 rfv.ErrorMessage = "请填写";
215 tc.Controls.Add(rfv);
216 }
217 tr.Cells.Add(tc);
218 }
219 else if (column.Type.ToLower() == "autodatetime")
220 {
221 TextBox tb = new TextBox();
222 tb.Style.Add(HtmlTextWriterStyle.Width, "200px");
223 tb.Text = DateTime.Now.ToString();
224 tb.Enabled = false;
225 tb.ID = column.Name;
226 tb.ToolTip = column.Tip;
227 tc.Controls.Add(tb);
228 tr.Cells.Add(tc);
229 }
230 else if (column.Type.ToLower() == "fck")
231 {
232 FredCK.FCKeditorV2.FCKeditor fckEditor = new FredCK.FCKeditorV2.FCKeditor();
233 fckEditor.ID = column.Name;
234 fckEditor.Width = new Unit(800);
235 fckEditor.Height = new Unit(400);
236 tc.Controls.Add(fckEditor);
237 tr.Cells.Add(tc);
238 }
239 else if (column.Type.ToLower() == "dropdownlist")
240 {
241 DropDownList ddl = new DropDownList();
242 ddl.ID = column.Name;
243 SQLData sqldata = new SQLData();
244 sqldata.SqlString = "select * from " + column.FTable + "";
245 SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnString, sqldata);
246 while (dr.Read())
247 {
248 ddl.Items.Add(new ListItem(dr[column.FText].ToString(), dr[column.FValue].ToString()));
249 }
250 dr.Close();
251 tc.Controls.Add(ddl);
252 tr.Cells.Add(tc);
253 }
254 else if (column.Type.ToLower() == "key")
255 {
256 tc.Text = string.Empty;
257 tr.Cells.Add(tc);
258 }
259 else if (string.IsNullOrEmpty(column.Type.Trim()))
260 {
261 tc.Text = string.Empty;
262 tr.Cells.Add(tc);
263 }
264 else if (column.Type.ToLower() == "file")
265 {
266 this.ClientScript.RegisterClientScriptInclude("jquery","http://www.cnblogs.com/js/jquery.js");
267 this.ClientScript.RegisterClientScriptInclude("moreFile","http://www.cnblogs.com/js/addmorefile.js");
268 FileUpload file = new FileUpload();
269 file.ID = column.Name;
270 HtmlInputButton inputButton = new HtmlInputButton();
271 inputButton.Attributes.Add("onclick", "addFile()");
272 inputButton.Attributes.Add("value", "更多附件");
273 tc.Controls.Add(file);
274 tc.Controls.Add(inputButton);
275 HtmlGenericControl span = new HtmlGenericControl("span");
276 span.ID = "moreFile";
277 span.Style.Add("margin-top", "0px");
278 tc.Controls.Add(span);
279 tr.Controls.Add(tc);
280 }
281 if (column.Type.ToLower() == "key" || string.IsNullOrEmpty(column.Type.Trim()))
282 {
283 //如果是主键,或自动增加列
284 // donothing
285 }
286 else
287 {
288 myTable.Rows.Add(tr);
289 }
290 }
291 TableRow trOperation = new TableRow();
292 TableCell tcOperation = new TableCell();
293 tcOperation.Text = string.Empty;
294 tcOperation.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
295 trOperation.Cells.Add(tcOperation);
296 tcOperation = new TableCell();
297 tcOperation.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
298 Button buttonSave = new Button();
299 buttonSave.Text = "保 存";
300 buttonSave.Click+=new EventHandler(buttonSave_Click);
301 Literal backButton = new Literal();
302 backButton.Text = " <input type=\"button\" value=\"返回\" onclick=\"location='commonquery.aspx?id=" + Request.QueryString["id"] + "'\"/>";
303 tcOperation.Controls.Add(buttonSave);
304 tcOperation.Controls.Add(backButton);
305 trOperation.Controls.Add(tcOperation);
306 myTable.Rows.Add(trOperation);
307 contentPlaceHolder.Controls.Add(myTable);
308 }
309 protected void buttonSave_Click(object sender, EventArgs e)
310 {
311 //添加附件用的关联编号
312 string id = Guid.NewGuid().ToString();
313 //处理附件用
314 List<Personal.Wgc.Diys.Model.T_DATA> fileList = new List<Personal.Wgc.Diys.Model.T_DATA>();
315
316 List<T_COLUMN> columnList = (List<T_COLUMN>)ViewState["columnList"];
317
318 if (columnList.Count > 0)
319 {
320 List<DbParameter> sqlParameterList = new List<DbParameter>();
321 StringBuilder sql = new StringBuilder();
322 StringBuilder paras = new StringBuilder();
323 paras.Append("values (");
324 sql.Append("insert into ");
325 sql.Append(columnList[0].TableName);
326 sql.Append("(");
327 foreach (T_COLUMN column in columnList)
328 {
329 //如果不是自增长列
330 if (!string.IsNullOrEmpty(column.Type.Trim()))
331 {
332 //如果不是附件
333 if (column.Type.ToLower() != "file")
334 {
335 sql.Append(column.Name);
336 sql.Append(",");
337 paras.Append("@");
338 paras.Append(column.Name);
339 paras.Append(",");
340 }
341 else
342 {
343 //含有附件
344 Personal.Wgc.Diys.Model.T_DATA file;
345 for (int i = 0; i < Request.Files.Count; i++)
346 {
347 //如果附件不为空
348 if (!string.IsNullOrEmpty(Request.Files[i].FileName))
349 {
350 file = new Personal.Wgc.Diys.Model.T_DATA();
351 file.ID = Guid.NewGuid().ToString();
352 file.LINKID = id;
353 file.NAME = Request.Files[i].FileName.Substring(Request.Files[i].FileName.LastIndexOf("\\") + 1);
354 file.SUFFIX = Request.Files[i].FileName.Substring(Request.Files[i].FileName.LastIndexOf('.') + 1).ToLower();
355 byte[] fileContent = new byte[Request.Files[i].ContentLength];
356 Request.Files[i].InputStream.Read(fileContent, 0, fileContent.Length);
357 file.CONTENT = fileContent;
358 fileList.Add(file);
359 }
360 }
361 }
362 }
363 //-------------------parameters deal
364 if (column.Type.ToLower() == "textbox" || column.Type.ToLower() == "mtextbox" || column.Type.ToLower() == "numbertextbox" || column.Type.ToLower() == "moneytextbox" || column.Type.ToLower() == "maxlengthtextbox" || column.Type.ToLower() == "autodatetime")
365 {
366 TextBox tb = this.FindControl(column.Name) as TextBox;
367 if (tb != null)
368 {
369 sqlParameterList.Add(new SqlParameter("@" + column.Name, WebUtility.CleanString(tb.Text.Trim())));
370 }
371 }
372 else if (column.Type.ToLower() == "dropdownlist")
373 {
374 DropDownList ddl = this.FindControl(column.Name) as DropDownList;
375 if (ddl != null)
376 {
377 sqlParameterList.Add(new SqlParameter("@" + column.Name, ddl.SelectedValue));
378 }
379 }
380 else if (column.Type.ToLower() == "fck")
381 {
382 FredCK.FCKeditorV2.FCKeditor fck = this.FindControl(column.Name) as FredCK.FCKeditorV2.FCKeditor;
383 if (fck != null)
384 {
385 sqlParameterList.Add(new SqlParameter("@" + column.Name,WebUtility.CleanString(fck.Value)));
386 }
387 }
388 else if (column.Type.ToLower() == "timetextbox")
389 {
390 TextBox tb = this.FindControl(column.Name) as TextBox;
391 if (tb != null)
392 {
393 sqlParameterList.Add(new SqlParameter("@" + column.Name, WebUtility.CleanString(tb.Text.Trim())));
394 }
395 }
396 else if (column.Type.ToLower() == "key")
397 {
398 sqlParameterList.Add(new SqlParameter("@" + column.Name, id));
399 }
400 else
401 {
402 //暂时默认为自动增长列
403 }
404 }
405 sql.Remove(sql.Length - 1, 1);
406 sql.Append(")");
407 paras.Remove(paras.Length - 1, 1);
408 paras.Append(")");
409 SQLData sqldata = new SQLData();
410 sqldata.SqlString = sql.ToString() + paras.ToString();
411 sqldata.SqlParameter = sqlParameterList;
412 try
413 {
414 if (SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, sqldata) > 0)
415 {
416 //如果有附件
417 if (fileList.Count > 0)
418 {
419 List<SQLData> sqldataList = new List<SQLData>();
420 foreach (Personal.Wgc.Diys.Model.T_DATA item in fileList)
421 {
422 if (item.SUFFIX != "exe" && item.SUFFIX != "jar" && item.SUFFIX != "dll")
423 {
424 sqldata = new SQLData();
425 sqldata.SqlString = "insert into t_data(id,linkid,suffix,name,content) values(@id,@linkid,@suffix,@name,@content)";
426 sqldata.SqlParameter.Add(new SqlParameter("@id", item.ID));
427 sqldata.SqlParameter.Add(new SqlParameter("@linkid",id));
428 sqldata.SqlParameter.Add(new SqlParameter("@suffix",item.SUFFIX));
429 sqldata.SqlParameter.Add(new SqlParameter("@name", item.NAME));
430 sqldata.SqlParameter.Add(new SqlParameter("@content", item.CONTENT));
431 sqldataList.Add(sqldata);
432 }
433 }
434 if (sqldataList.Count > 0)
435 {
436 try
437 {
438 SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, sqldataList);
439 }
440 catch
441 {
442 // do nothing
443 }
444 }
445 }
446 this.ClientScript.RegisterClientScriptBlock(GetType(), "add", "alert('添加成功');location='commonquery.aspx?id=" + Request.QueryString["id"] + "'", true);
447 }
448 else
449 {
450 this.ClientScript.RegisterClientScriptBlock(GetType(), "add", "alert('添加失败')", true);
451 }
452 }
453 catch
454 {
455 this.ClientScript.RegisterClientScriptBlock(GetType(), "add", "alert('出现异常操作失败')", true);
456 }
457 }
458 }
459 }
460
本来想再介绍下代码和xml,只怪电脑性能太低,iexplorer.exe进程已经占了350M内存,再在编辑器中多输入一字都要卡半天,可惜啊可惜,看来干么事第一次都是很失败啊我。。希望各位XDJM有兴趣的直接看xml和代码自己领悟吧。。偶闪人了。。。