1 登录页面 2 <body> 3 <form action="Login.ashx" method="post"> 4 <input type="hidden" name="viewstate" value="123" id="viewstase" /> 5 <table style="margin:200px auto 0px;"> 6 <tr><td colspan="2" style="text-align:center">登陆系统</td></tr> 7 <tr><td>用户名:</td><td><input type="text" name="username" value="$name" id="username" /></td></tr> 8 <tr><td>密码:</td><td><input type="text" name="pwd" id="pwd" value="$pwd" /></td></tr> 9 <tr><td colspan="2" style="text-align:center"><input type="submit" value="登陆" /></td></tr> 10 </table> 11 </form> 12 </body> 13 登录页面.ashx 14 using System; 15 using System.Collections.Generic; 16 using System.Linq; 17 using System.Web; 18 using System.Data.SqlClient; 19 using FirstWeb; 20 using System.IO; 21 using System.Web.SessionState; 22 23 namespace Lesson3 24 { 25 /// <summary> 26 /// Login 的摘要说明 27 /// </summary> 28 public class Login : IHttpHandler,IRequiresSessionState 29 { 30 31 public void ProcessRequest(HttpContext context) 32 { 33 //context.Response.ContentType = "text/html"; 34 //context.Response.Write("Hello World"); 35 context.Response.ContentType = "text/html"; 36 string name = context.Request["username"]; 37 string Pwd = context.Request["pwd"]; 38 string path = context.Request.MapPath("Login.htm"); //将Login.htm文件的相对路径改为绝对路径 39 string loginhtml = File.ReadAllText(path);//读取文件内容 40 string viewstate = context.Request["viewstate"]; 41 bool IsPostBack = !string.IsNullOrEmpty(viewstate); 42 if (!IsPostBack)//第一次访问页面 43 { 44 HttpCookie cookie = context.Request.Cookies["Login"]; 45 if (cookie!=null) 46 { 47 //获取客户端保存的HttpCookie对象或值:context.Request.Cookies["Login"] 48 string username = cookie["name"]; 49 string userpwd = cookie.Values["pwd"]; 50 loginhtml = loginhtml.Replace("$name", username).Replace("$pwd", userpwd); 51 } 52 else 53 { 54 loginhtml = loginhtml.Replace("$name", "").Replace("$pwd", ""); 55 } 56 context.Response.Write(loginhtml); 57 return; 58 } 59 60 string sql = "select count(*) from Users where UserName=@username and Pwd=@pwd"; 61 SqlParameter[] sps ={ 62 new SqlParameter("@username",name), 63 new SqlParameter("@Pwd",Pwd) 64 }; 65 int result = Convert.ToInt32(sqlhelper.GetExecuteScalar(sql, sps)); 66 67 if (result > 0) 68 { 69 //HttpCookie cookie = new HttpCookie("Login"); 70 //cookie.Values["name"] = name; 71 //cookie["pwd"] = Pwd; 72 //cookie.Expires = DateTime.Now.AddMinutes(1); 73 //context.Response.Cookies.Add(cookie); 74 75 context.Response.Cookies["Login"]["name"] = name; 76 context.Response.Cookies["Login"]["pwd"] = Pwd; 77 context.Response.Cookies["Login"].Expires = DateTime.Now.AddMinutes(1); 78 context.Response.Write("登陆成功!"); 79 80 context.Session["user"] = name; 81 context.Application.Lock();//修改Application数据之前,需要先加锁处理,防止别人登录(一次只让一个人登陆,防止多人同时修改数据) 82 context.Application["online"] = Convert.ToInt32(context.Application["online"]) + 1; 83 context.Application.UnLock();//修改Application数据之后,需要先解锁处理,以供别人登录 84 context.Response.Redirect("ApplicationTest.aspx"); 85 //context.Response.Write(cookie.Value); 86 } 87 else 88 { 89 loginhtml = loginhtml.Replace("$name",name).Replace("$pwd",Pwd); 90 context.Response.Write(loginhtml); 91 context.Response.Write("<script>alert('登陆失败!')</script>"); 92 } 93 } 94 95 public bool IsReusable 96 { 97 get 98 { 99 return false; 100 } 101 } 102 } 103 } 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1.AddStudent.htm 120 121 1-1 122 <body> 123 <form action="AddStudent.ashx" method="post"> 124 <table style="margin:10px auto"> 125 <tr><td colspan="2" style="text-align:center">添加学生信息</td></tr> 126 <tr><td>学号:</td><td> 127 <input id="Text1" type="text" name="stuNo" /></td></tr> 128 <tr><td>姓名:</td><td> 129 <input id="Text2" type="text" name="stuName" /></td></tr> 130 <tr><td>性别</td><td> 131 <input id="Radio1" type="radio" name="sex" value="男" />男 132 <input id="Radio2" type="radio" name="sex" value="女" />女</td></tr> 133 <tr><td>出生日期:</td><td> 134 <input id="Text4" type="text" name="birthday" /></td></tr> 135 <tr><td>电话:</td><td> 136 <input id="Text5" type="text" name="phone" /></td></tr> 137 <tr><td>地址:</td><td> 138 <input id="Text6" type="text" name="address" /></td></tr> 139 <tr><td>Email:</td><td> 140 <input id="Text7" type="text" name="email" /></td></tr> 141 <tr><td colspan="2" style="text-align:center"> 142 <input id="Submit1" type="submit" value="添加" /></td></tr> 143 </table> 144 </form> 145 </body> 146 1-2 147 using System; 148 using System.Collections.Generic; 149 using System.Linq; 150 using System.Web; 151 using System.IO; 152 using System.Data.SqlClient; 153 using FirstWeb; 154 155 namespace Lesson3 156 { 157 /// <summary> 158 /// AddStudent 的摘要说明 159 /// </summary> 160 public class AddStudent : IHttpHandler 161 { 162 163 public void ProcessRequest(HttpContext context) 164 { 165 context.Response.ContentType = "text/html"; 166 //1.接收数据,这是从添加页面的name="stuName"获取的用户输入的信息 167 string stuName = context.Request["stuName"]; 168 string stuNo = context.Request["stuNo"]; 169 string sex = context.Request["sex"]; 170 string birthday = context.Request["birthday"]; 171 string phone = context.Request["phone"]; 172 string address = context.Request["address"]; 173 string email = context.Request["email"]; 174 //将htm文件的相对路径转为绝对路径 175 string path = context.Request.MapPath("AddStudent.htm"); 176 string html = File.ReadAllText(path); 177 //2.对接收数据进行处理 178 string msg = ""; 179 if (string.IsNullOrEmpty(stuNo)) 180 { 181 msg =msg + "学号不能为空"; 182 //context.Response.Write("<script>alert('学号不能为空!')</script>"); 183 } 184 if (string.IsNullOrEmpty(sex)) 185 { 186 msg += "\n请选择性别"; 187 } 188 if (!string.IsNullOrEmpty(birthday)) 189 { 190 DateTime birth; 191 bool tag = DateTime.TryParse(birthday, out birth); 192 if (!tag) 193 { 194 msg += "\n日期格式错误"; 195 } 196 } 197 198 if (msg!="") 199 { 200 context.Response.Write("<script>alert('"+msg+"')</script>"); 201 context.Response.Write(html); 202 context.Response.End(); 203 } 204 205 string sql = "declare @loginId int insert into users values(@username,@pwd);set @loginId=scope_identity();" + 206 "insert into tab_student values (@stuNo,@stuName,@sex,@birthday,@phone," + 207 "@address,@email,@IsDel,@loginId)"; 208 SqlParameter[] sps = { 209 new SqlParameter("@username",stuNo), 210 new SqlParameter("@pwd",stuNo), 211 new SqlParameter("@stuNo",stuNo), 212 new SqlParameter("@stuName",stuName), 213 new SqlParameter("@sex",sex), 214 new SqlParameter("@birthday",birthday), 215 new SqlParameter("@phone",phone), 216 new SqlParameter("@address",address), 217 new SqlParameter("@email",email), 218 new SqlParameter("@IsDel",false) 219 }; 220 int insert = sqlhelper.GetExecuteNotQuery(sql, sps); 221 //string sql = "insert into users values(@username,@pwd);select scope_identity()"; 222 //获取Users表里的主键id 223 //SqlParameter[] sps ={ 224 // new SqlParameter("@username",stuNo), 225 // new SqlParameter("@pwd",stuNo) 226 // }; 227 //int loginId = Convert.ToInt32(sqlhelper.GetExecuteScalar(sql,sps));//查询login的id 228 229 //string sql1 = "insert into tab_student values (@stuNo,@stuName,@sex,@birthday,@phone,@address,@email,@IsDel,@loginId) "; 230 //SqlParameter[] sps1 = { 231 // new SqlParameter("@stuNo",stuNo), 232 // new SqlParameter("@stuName",stuName), 233 // new SqlParameter("@sex",sex), 234 // new SqlParameter("@birthday",birthday), 235 // new SqlParameter("@phone",phone), 236 // new SqlParameter("@address",address), 237 // new SqlParameter("@email",email), 238 // new SqlParameter("@IsDel",false), 239 // new SqlParameter("@loginId",loginId), 240 241 // }; 242 //int insert = sqlhelper.GetExecuteNotQuery(sql1, sps1); 243 if (insert>0) 244 { 245 // context.Response.Write("<script>alert('登陆成功')</script>"); 246 context.Response.Redirect("StudentList.ashx"); 247 //context.Response.Write(html); 248 //context.Response.End(); 249 } 250 else 251 { 252 context.Response.Write("<script>alert('添加失败')</script>"); 253 context.Response.Write(html); 254 context.Response.End(); 255 } 256 //context.Response.Write("Hello World"); 257 } 258 259 public bool IsReusable 260 { 261 get 262 { 263 return false; 264 } 265 } 266 } 267 } 268 2 269 2-1StudentList.htm 270 <html xmlns="http://www.w3.org/1999/xhtml"> 271 <head> 272 <title></title> 273 <script type="text/javascript"> 274 function confrim() { 275 if (confirm("是否要删除!")) 276 { return true; } 277 return false; 278 } 279 </script> 280 281 </head> 282 <body> 283 <div style="800px"> 284 <input type="button" name="sub" value="添加" onclick="Add();" /> 285 <script type="text/javascript"> 286 function Add() { 287 window.location.href = "AddStudent.htm"; 288 } 289 </script> 290 <!--//<form action="StudentList.ashx" method="post">--> 291 <form method="post"> 292 $tbody 293 </form> 294 </div> 295 </body> 296 </html> 297 298 2-2StudentList.ashx 299 using System; 300 using System.Collections.Generic; 301 using System.Linq; 302 using System.Web; 303 using System.Data.SqlClient; 304 using System.Configuration; 305 using System.Text; 306 using System.Data; 307 using FirstWeb; 308 using System.IO; 309 310 namespace Lesson3 311 { 312 /// <summary> 313 /// StudentList 的摘要说明 314 /// </summary> 315 public class StudentList : IHttpHandler 316 { 317 318 public void ProcessRequest(HttpContext context) 319 { 320 string flag = context.Request.QueryString["flag"]; 321 if (!string.IsNullOrEmpty(flag)) 322 { 323 int result = Convert.ToInt32(flag); 324 if (result > 0) 325 { 326 context.Response.Write("<script>alert('删除成功')</script>"); 327 } 328 else 329 { 330 context.Response.Write("<script>alert('删除失败')</script>"); 331 } 332 } 333 334 context.Response.ContentType = "text/html"; 335 string path= context.Request.MapPath("StudentList.htm"); 336 string html = File.ReadAllText(path); 337 StringBuilder table = new StringBuilder(); 338 table.Append("<table style='800px;margin:10px auto;text-align:center;'>"); 339 table.Append("<tr><th>学号</th><th>姓名</th><th>性别</th><th>出生日期</th><th>年纪</th><th>电话</th><th>地址</th><th>Email</th><th>操作</th></tr>"); 340 341 string sql = "select * from tab_student"; 342 DataTable dt = sqlhelper.GetDataTable1(sql); 343 foreach (DataRow dr in dt.Rows) 344 { 345 string age = ""; 346 if (dr["birthday"] == DBNull.Value) 347 { 348 age = "未知"; 349 350 } 351 else 352 { 353 int nowyear = DateTime.Now.Year; 354 int oldyear = Convert.ToDateTime(dr["birthday"]).Year; 355 if (oldyear == 1900) 356 { age = "未知"; } 357 else 358 { 359 age = (nowyear - oldyear).ToString(); 360 } 361 } 362 363 string stuNo = dr["stuNo"].ToString(); 364 string stuName = dr["stuName"].ToString(); 365 string sex = dr["sex"].ToString(); 366 string birthday = dr["birthday"].ToString(); 367 string phone = dr["phone"].ToString(); 368 string address = dr["address"].ToString(); 369 string email = dr["email"].ToString(); 370 string loginid=dr["loginId"].ToString(); 371 table.Append("<tr><td>" + stuNo + "</td><td>" + stuName + "</td><td>" + sex + "</td><td>" + birthday + "</td><td>" + age + "</td><td>" 372 + phone + "</td><td>" + address + "</td><td>" + email + "</td>" 373 +"<td><a href='StudentDel.ashx?id=" + loginid + "' onclick='return confrim("是否要删除")'>删除</a>" 374 +" <a href='StudentExitShow.ashx?id=" + loginid + "'</td>修改</tr>"); 375 376 } 377 378 379 table.Append("</table>"); 380 // table.ToString(); 381 html = html.Replace("$tbody", table.ToString()); 382 context.Response.Write(html); 383 } 384 385 public bool IsReusable 386 { 387 get 388 { 389 return false; 390 } 391 } 392 } 393 } 394 <div style="800px"> 395 <input type="button" name="sub" value="添加" onclick="Add();" /> 396 <script type="text/javascript"> 397 function Add() { 398 window.location.href = "AddStudent.htm"; 399 } 400 </script> 401 <!--//<form action="StudentList.ashx" method="post">--> 402 <form method="post"> 403 $tbody 404 </form> 405 </div> 406 </body> 407 </html> 408 3 409 3-1StudentDel.ashx 410 using System; 411 using System.Collections.Generic; 412 using System.Linq; 413 using System.Web; 414 using System.Data.SqlClient; 415 416 417 namespace Lesson3 418 { 419 /// <summary> 420 /// StudentDel 的摘要说明 421 /// </summary> 422 public class StudentDel : IHttpHandler 423 { 424 425 public void ProcessRequest(HttpContext context) 426 { 427 context.Response.ContentType = "text/plain"; 428 // context.Response.Write("Hello World"); 429 string id = context.Request.QueryString["id"]; 430 if (string.IsNullOrEmpty(id)) 431 { 432 return; 433 } 434 string sql = "delete from tab_student where loginId=@loginId " 435 +"delete from Users where ID=@loginId"; 436 SqlParameter sps = new SqlParameter("@loginId", id); 437 int result = FirstWeb.sqlhelper.GetExecuteNotQuery(sql, sps); 438 context.Response.Redirect("StudentList.ashx?flag=" + result); 439 } 440 441 public bool IsReusable 442 { 443 get 444 { 445 return false; 446 } 447 } 448 } 449 } 450 4 451 4.1StudentExit.htm 452 <body> 453 <!--<form action="StudentExit.ashx" method="post">--> 454 <form method="post"> 455 <input type="hidden" name="hidID" value="@ID" /> 456 <table style="margin:50px auto"> 457 <tr><th colspan="2" style="text-align:left">修改学生信息:</th></tr> 458 <tr><td class="style1">学号:</td><td><input type="text" name="stuNo" value="@stuNo" /></td></tr> 459 <tr><td class="style1">姓名:</td><td><input type="text" name="stuName" value="@stuName" /></td></tr> 460 <tr><td class="style1">性别:</td><td><input type="radio" name="sex" value="男" boy="@boy" />男<input type="radio" name="sex" value="女" girl="@girl" />女</td></tr> 461 <tr><td class="style1">出生日期:</td><td><input type="text" name="birthday" value="@birthday" /></td></tr> 462 <tr><td class="style1">电话:</td><td><input type="text" name="phone" value="@phone"/></td></tr> 463 <tr><td class="style1">地址:</td><td><input type="text" name="address"value="@address" /></td></tr> 464 <tr><td class="style1">Email</td><td><input type="text" name="email" value="@email" /></td></tr> 465 <tr><td colspan="2" style="text-align:center"><input type="submit" value="修改" /> 466 <input type="button" value="返回" onclick="funhui()" /></td></tr> 467 </table> 468 <script type="text/javascript"> 469 function funhui() { 470 window.location.href = "StudentList.ashx"; 471 } 472 </script> 473 <!--<script type="text/javascript"> 474 function Exit() { 475 window.location.href = "StudentExit.ashx"; 476 } 477 </script>--> 478 </form> 479 </body> 480 4.2StudentExitShow.ashx 481 using System; 482 using System.Collections.Generic; 483 using System.Linq; 484 using System.Web; 485 using System.Data.SqlClient; 486 using System.Data; 487 using System.IO; 488 489 namespace Lesson3 490 { 491 /// <summary> 492 /// StudentExitShow 的摘要说明 493 /// </summary> 494 public class StudentExitShow : IHttpHandler 495 { 496 497 public void ProcessRequest(HttpContext context) 498 { 499 context.Response.ContentType = "text/html"; 500 //context.Response.Write("Hello World"); 501 string path = context.Request.MapPath("StudentExit.htm"); 502 string html = File.ReadAllText(path); 503 string loginid = context.Request.QueryString["id"]; 504 if (string.IsNullOrEmpty(loginid)) 505 { 506 context.Response.Redirect("StudentList.ashx"); 507 } 508 string id = context.Request["hidID"]; 509 bool IspostBack = !string.IsNullOrEmpty(id); 510 if (!IspostBack) 511 { 512 string sql = "select [ID],[stuNo],[stuName],[sex],convert(varchar,birthday,111) as birthday,[phone],[address],[email],[IsDel],[loginId] from tab_student where [loginId]=@loginid"; 513 SqlParameter[] sps = { new SqlParameter("@loginid", loginid) }; 514 DataTable dt = FirstWeb.sqlhelper.GetDataTable1(sql, sps); 515 html = html.Replace("@stuNo", dt.Rows[0]["stuNo"].ToString()); 516 html = html.Replace("@stuName", dt.Rows[0]["stuName"].ToString()); 517 html = html.Replace("@phone", dt.Rows[0]["phone"].ToString()); 518 html = html.Replace("@address", dt.Rows[0]["address"].ToString()); 519 html = html.Replace("@email", dt.Rows[0]["email"].ToString()); 520 html = html.Replace("@ID", dt.Rows[0]["loginid"].ToString()); 521 string sex = dt.Rows[0]["sex"].ToString(); 522 if (sex == "男") 523 { 524 html = html.Replace("boy="@boy"", "checked"); 525 } 526 else 527 { html = html.Replace("girl="@girl"", "checked"); } 528 if (dt.Rows[0]["birthday"].ToString().Substring(0, 4) == "1900") 529 { 530 html = html.Replace("@birthday", ""); 531 } 532 else 533 { html = html.Replace("@birthday", dt.Rows[0]["birthday"].ToString()); } 534 535 536 context.Response.Write(html); 537 538 } 539 else 540 { 541 string stuNo = context.Request["stuNo"]; 542 string stuName = context.Request["stuName"]; 543 string sex = context.Request["sex"]; 544 string birthday = context.Request["birthday"]; 545 string phone = context.Request["phone"]; 546 string address = context.Request["address"]; 547 string email = context.Request["email"]; 548 string sql = "update tab_student set stuNo=@stuNo,stuName=@stuName,sex=@sex,birthday=@birthday,phone=@phone,address=@address,email=@email where [loginId]=@loginId " 549 + "update Users set [UserName]=@stuNo,[Pwd]=@stuNo where [ID]=@loginId"; 550 SqlParameter[] sps ={ 551 new SqlParameter("@stuNo",stuNo), 552 new SqlParameter("@stuName",stuName), 553 new SqlParameter("@sex",sex), 554 new SqlParameter("@birthday",birthday), 555 new SqlParameter("@phone",phone), 556 new SqlParameter("@address",address), 557 new SqlParameter("@email",email), 558 new SqlParameter("@loginId",id) 559 }; 560 int result = FirstWeb.sqlhelper.GetExecuteNotQuery(sql, sps); 561 if (result > 0) 562 { 563 context.Response.Redirect("StudentList.ashx"); 564 } 565 else 566 { 567 context.Response.Write("<script>alert('修改失败!')</script>"); 568 } 569 } 570 } 571 572 573 public bool IsReusable 574 { 575 get 576 { 577 return false; 578 } 579 } 580 } 581 } 582 ------------------------------------------- 583 584 ----------------------------------- 585 1ban 2ban 3ban 4ban RadioButtonList 586 只要是列表xxList都有ListItem 列表选项 587 RadioButtonList1就是班级框列表 588 操作代码都是写在按钮里的,然后在按钮里绑定其控件 589 if(RadioButtonList1.selectedIndex<0) 590 { 591 response.write("<script>alert('选择班级')</script>"); 592 return; 593 } 594 Response.Write("<script>alert('你选择的班级的index是"+RadioButtonList1.SelectedIndex+"')</script>"); 595 Response.Write(""); 596 ---------------- 597 男 女 598 string s=""; 599 if(RadioButton1.cheched) 600 { 601 s=RadioButton1.Text; 602 } 603 if(RadioButton2.Checked) 604 { 605 s=RadioButton2.Text; 606 } 607 response.write("<script>alert('"+s+"')</script>"); 608 -------------------- 609 爱好:篮球 足球 多选框 CheckBoxList1 610 string s=""; 611 for循环出所有选中的 612 foreach(ListItem item in CheckBoxList1.item) 613 { 614 if(item.Selected) 615 { 616 s=s+item.Text+""; 617 } 618 response.Write("<script>alert('"+s+"')</script>"); 619 }