1 using Console_Core.BLL;
2 using Console_Core.Common;
3 using Console_Core.Model;
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Web;
8
9 namespace Web_Cassini.Day3
10 {
11 /// <summary>
12 /// studenteidt 的摘要说明
13 /// </summary>
14 public class studenteidt : IHttpHandler
15 {
16
17 public void ProcessRequest(HttpContext context)
18 {
19 context.Response.ContentType = "text/html";
20 string save = context.Request["save"];
21 string action = context.Request["action"];
22 string html = CommonHelper.GetHtmlFromVirtualPath(context, "~/Day3/studentedit.html");
23 MyORM_BLL myORM_BLL = new MyORM_BLL();
24
25 #region 展示
26 if (string.IsNullOrWhiteSpace(save)) //展示
27 {
28 if (action == "addnew")
29 {
30 html = html.Replace("@actionName", "新增").Replace("@action", "addnew").Replace("@ID", "").Replace("@USERNAME", "")
31 .Replace("@PASSWORD", "").Replace("@AGE", "").Replace("{ISRICH}", "").Replace("@PROFESSION", "")
32 .Replace("{male}", "checked").Replace("{female}", "").Replace("{both}", "");
33 context.Response.Write(html);
34 }
35 else if (action == "edit")
36 {
37 //获得id 根据id查询 验证格式
38 int id = Convert.ToInt32(context.Request["id"]);
39 TC_STUDENT tc = (TC_STUDENT)myORM_BLL.SelectModelById(typeof(TC_STUDENT), id);
40 //替换html
41 html = html.Replace("@actionName", "编辑" + tc.USERNAME).Replace("@action", "edit").Replace("@ID", tc.ID.ToString()).Replace("@USERNAME", tc.USERNAME)
42 .Replace("@PASSWORD", tc.PASSWORD).Replace("@AGE", tc.AGE.ToString()).Replace("{ISRICH}", "checked").Replace("@PROFESSION", tc.PROFESSION);
43 if (tc.GENDER == 1)
44 {
45 html = html.Replace("{male}", "checked").Replace("{female}", "").Replace("{both}", "");
46 }
47 else if (tc.GENDER == 2)
48 {
49 html = html.Replace("{male}", "").Replace("{female}", "checked").Replace("{both}", "");
50 }
51 else if (tc.GENDER == 3)
52 {
53 html = html.Replace("{male}", "").Replace("{female}", "").Replace("{both}", "checked");
54 }
55 else
56 {
57 CommonHelper.OutputError(context, "~/Day3/error.html", "500 INTERNET SERVER ERROR THIS GENDER:" + tc.GENDER);
58 return;
59 }
60 //输出
61 context.Response.Write(html);
62 }
63 else
64 {
65 CommonHelper.OutputError(context, "~/Day3/error.html", "404 NOT FOUND THIS ACTION:" + action);
66 }
67 }
68 #endregion
69
70 #region 保存
71 else if (save == "保存") //保存
72 {
73 string USERNAME = context.Request["USERNAME"];
74 string PROFESSION = context.Request["PROFESSION"];
75 string PASSWORD = context.Request["PASSWORD"];
76 string ISRICH = context.Request["ISRICH"];
77 string GENDER = context.Request["GENDER"];
78 string AGE = context.Request["AGE"];
79 #region 验证 非空、数字格式
80 //验证 非空、数字格式
81 if (string.IsNullOrWhiteSpace(USERNAME))
82 {
83 CommonHelper.OutputError(context, "~/Day3/error.html", "500 请填写用户名");
84 return;
85 }
86 if (string.IsNullOrWhiteSpace(PROFESSION))
87 {
88 CommonHelper.OutputError(context, "~/Day3/error.html", "500 请填写专业");
89 return;
90 }
91 if (string.IsNullOrWhiteSpace(PASSWORD))
92 {
93 CommonHelper.OutputError(context, "~/Day3/error.html", "500 请填写密码");
94 return;
95 }
96 if (string.IsNullOrWhiteSpace(AGE))
97 {
98 CommonHelper.OutputError(context, "~/Day3/error.html", "500 请填写年龄");
99 return;
100 }
101 int age;
102 if (!int.TryParse(AGE, out age)) //转换不成功
103 {
104 CommonHelper.OutputError(context, "~/Day3/error.html", "500 AGE必须是数字:" + AGE);
105 return;
106 }
107 #endregion
108 //实例共同项
109 TC_STUDENT tc = new TC_STUDENT();
110 tc.USERNAME = USERNAME;
111 tc.PROFESSION = PROFESSION;
112 tc.PASSWORD = PASSWORD;
113 tc.ISRICH = ISRICH == "on" ? 1 : 2;
114 tc.GENDER = Convert.ToInt32(context.Request["GENDER"]);
115 tc.AGE = age;
116 bool flag = false;
117 if (action == "addnew")
118 {
119 flag = myORM_BLL.InsertModel(tc, "SE_TC_STUDENT");
120 }
121 else if (action == "edit")
122 {
123 tc.ID = Convert.ToInt32(context.Request["ID"]);
124 flag = myORM_BLL.UpdateModel(tc);
125 }
126 else
127 {
128 CommonHelper.OutputError(context, "~/Day3/error.html", "404 NOT FOUND THIS ACTION:" + action);
129 }
130 if (!flag) //新增或更新失败
131 {
132 CommonHelper.OutputError(context, "~/Day3/error.html", "500 INSERT OR UPDATE 失败");
133 return;
134 }
135 context.Response.Redirect("studentlist.ashx");
136 }
137 #endregion
138
139 else
140 {
141 CommonHelper.OutputError(context, "~/Day3/error.html", "404 NOT FOUND THIS SAVE:" + save);
142 }
143 }
144
145 public bool IsReusable
146 {
147 get
148 {
149 return false;
150 }
151 }
152 }
153 }