SchoolManagementSystem /website/control/StudentControl.ashx.cs

Language C# Lines 266
MD5 Hash 58db2e562a941e5fe3ece28f58c271da
Repository https://github.com/panmingzhi815/SchoolManagementSystem.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Reflection;
using Domain.Entities;
using DataService.service.dao;
using DataService.util;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Web.SessionState;

namespace Domain.control
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class StudentControl : IHttpHandler, IRequiresSessionState
    {
        public HttpContext context;
        public void ProcessRequest(HttpContext context)
        {
            this.context = context;
            context.Response.ContentType = "text/plain";
            String method = context.Request.Form.Get("method");
            if (method == null)
            {
                method = context.Request.QueryString["method"];
            }
            switch (method)
            {
                case "addStudent":
                    addStudent();
                    break;
                case "delStudent":
                    delStudent();
                    break;
                case "updateStudent":
                    updateStudent();
                    break;
                case "getStudents":
                    getStudents();
                    break;
                case "getStudent":
                    getStudent();
                    break;
                case "updatePassword":
                    updatePassword();
                    break;
                default:
                    context.Response.Write("-1");
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void addStudent()
        {
            try
            {
                Student student = new Student();
                setValue(student, context);

                HttpPostedFile hpf = context.Request.Files["headImgFile"];
                if (hpf != null)
                {
                    string serverPath = "/uploadFile/headImg/" + System.DateTime.Now.Ticks + "." + hpf.FileName.Split('.')[1];
                    string savePath = context.Server.MapPath(serverPath);//路径,相对于服务器当前的路径
                    hpf.SaveAs(savePath);//保存
                    student.HeadImage = serverPath;
                }
                string FacultyID = context.Request.Form.Get("Faculty");
                DepartmentService ds = new DepartmentService();
                string professionID = context.Request.Form.Get("Profession");
                if (!string.IsNullOrEmpty(professionID))
                {
                    Profession profession = ds.getProfessionByID(professionID);
                    if (profession != null)
                        student.Profession = profession;
                }

                string ClassGradeID = context.Request.Form.Get("ClassGrade");
                if (!string.IsNullOrEmpty(ClassGradeID))
                {
                    ClassGrade classGrade = ds.getClassGradeByID(ClassGradeID);
                    if (classGrade != null)
                        student.ClassGrade = classGrade;
                }
                StudentService s = new StudentService();
                student.Password = student.Sn;
                s.save(student);
                
                context.Response.Write("1");
            }
            catch (Exception e)
            {
                context.Response.Write("0");
            }
        }

        public void delStudent()
        {
            try
            {
                string Id = context.Request.QueryString["Id"];
                StudentService service = new StudentService();
                service.del(service.get(typeof(Student), Id));
                context.Response.Write("1");
            }
            catch (Exception e)
            {
                context.Response.Write("0");
            }

        }

        public void updateStudent()
        {
            try
            {
                Student student = new Student();
                setValue(student, context);

                HttpPostedFile hpf = context.Request.Files["headImgFile"];
                if (hpf != null) {
                    string savepath = context.Server.MapPath("/uploadFile/headImg/" + student.Id + "." + hpf.GetType());//路径,相对于服务器当前的路径
                    hpf.SaveAs(savepath);//保存
                    student.HeadImage = savepath;
                }

                DepartmentService ds = new DepartmentService();
                string professionID = context.Request.Form.Get("Profession");
                if (!string.IsNullOrEmpty(professionID)) {
                    Profession profession = ds.getProfessionByID(professionID);
                    if (profession != null)
                        student.Profession = profession;
                }

                string ClassGradeID = context.Request.Form.Get("ClassGrade");
                if (!string.IsNullOrEmpty(ClassGradeID)) {
                    ClassGrade classGrade = ds.getClassGradeByID(ClassGradeID);
                    if (classGrade != null)
                        student.ClassGrade = classGrade;
                }
                
                StudentService s = new StudentService();
                s.save(student);
                context.Response.Write("1");
            }
            catch (Exception e)
            {
                context.Response.Write("0");
            }
        }

        public void getStudent() {
            string Id = context.Request.QueryString["Id"];
            StudentService service = new StudentService();
            Student student = (Student)service.get(typeof(Student), Id);
            String json = JsonConvert.SerializeObject(student);
            context.Response.Write(json);
        }

        public void getStudents()
        {
            try
            {
                StudentService service = new StudentService();
                Student student = new Student();
                setValue(student, context);

                string ProfessionID = context.Request.Form.Get("FacultyID");
                string FacultyID = context.Request.Form.Get("ProfessionID");

                IList<Profession> professionList = new List<Profession>();
                DepartmentService ds = new DepartmentService();
                if (!string.IsNullOrEmpty(ProfessionID)) {
                   Profession profession = ds.getProfessionByID(ProfessionID);
                   if (profession != null) professionList.Add(profession);
                }
                else if (!string.IsNullOrEmpty(FacultyID)) {
                    Faculty faculty = ds.getFacultyByID(FacultyID);
                    if (faculty != null && faculty.professionList != null)
                        foreach (Profession p in faculty.professionList)
                            professionList.Add(p);
                }

                student.ProfessionList = professionList;
                int rows = Convert.ToInt32(context.Request.Form["rows"]);
                int page = Convert.ToInt32(context.Request.Form["page"]);
                object[] data = service.getStudentList(student, rows, page);
                Hashtable ht = new Hashtable();
                ht.Add("total", data[0]);
                ht.Add("rows", data[1]);
                String json = JsonConvert.SerializeObject(ht);
                context.Response.Write(json);
            }
            catch (Exception e)
            {

            }

        }

        public void updatePassword() {
            try {
                string Sn = context.Request.Form.Get("Sn");
                string Password = context.Request.Form.Get("Password");
                StudentService ss = new StudentService();
                ss.updatePassword(Sn, Password);
                context.Response.Write("1");
            }
            catch (Exception e) {
                context.Response.Write("0");
            }
        }

        public Object setValue(Object o, HttpContext context)
        {
            string[] keys = context.Request.Form.AllKeys;
            foreach (string s in keys)
            {
                try
                {
                    PropertyInfo property = o.GetType().GetProperty(s);
                    if (property == null)
                    {
                        continue;
                    }
                    if (property.PropertyType == typeof(DateTime))
                    {
                        property.SetValue(o, Convert.ToDateTime(context.Request.Form.Get(s)), null);
                    }
                    else if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int))
                    {
                        property.SetValue(o, context.Request.Form.Get(s), null);
                    }

                }
                catch (Exception e)
                {
                    Console.Write(e.Message);
                }

            }
            return o;
        }

    }

}
Back to Top