04Add.ashx(新增班级)
04Add.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>新增班级</title> <style type="text/css"> #tbList { border:1px solid #0094ff; border-collapse:collapse; width:300px; margin:50px auto; } #tbList th,td{ border:1px solid #0094ff; padding:5px; } </style> </head> <body> <form method="post" action="04Add.ashx"> <table id="tbList"> <tr> <td>班级名称:</td> <td><input type="text" id="txtName" name="txtName"/></td> </tr> <tr> <td>班级人数:</td> <td><input type="text" id="txtCount" name="txtCount" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="确定" /> <input type="button" id="btnCancel" value="取消" onclick="window.location='01List.ashx'" /> </td> </tr> </table> </form> </body> </html>
04Add.ashx
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; namespace AspNetAshx { /// <summary> /// _04Add 的摘要说明 /// </summary> public class _04Add : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //1.获取用户表单post提交的数据 string strName = HttpContext.Current.Request.Form["txtName"].Trim(); string strCount = HttpContext.Current.Request.Form["txtCount"]; //2.验证数据 if (string.IsNullOrEmpty(strName) || !CommonHelper.IsNum(strCount)) { CommonHelper.WriteJs("对不起,您输入的数据格式错误~请仔细检查~~", "04Add.html"); } else { //3.更新到数据库中 SqlParameter[] paras = { new SqlParameter("Cname",strName), new SqlParameter("CCount",strCount) }; int res = SqlHelper.ExcuteNoneQuery("insert into Classes (CName,CCount) values(@Cname,@CCount)", paras); if (res > 0) { CommonHelper.WriteJs("新增成功~!", "01List.ashx"); } else { CommonHelper.WriteJs("新增失败~!如果您是美女,请联系管理员~~~qq:111111", "04Add.html"); } } } public bool IsReusable { get { return false; } } } }