登录页面
<body>
<form action="Login.ashx" method="post">
<input type="hidden" name="viewstate" value="123" id="viewstase" />
<table style="margin:200px auto 0px;">
<tr><td colspan="2" style="text-align:center">登陆系统</td></tr>
<tr><td>用户名:</td><td><input type="text" name="username" value="$name" id="username" /></td></tr>
<tr><td>密码:</td><td><input type="text" name="pwd" id="pwd" value="$pwd" /></td></tr>
<tr><td colspan="2" style="text-align:center"><input type="submit" value="登陆" /></td></tr>
</table>
</form>
</body>
登录页面.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using FirstWeb;
using System.IO;
using System.Web.SessionState;
namespace Lesson3
{
/// <summary>
/// Login 的摘要说明
/// </summary>
public class Login : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/html";
//context.Response.Write("Hello World");
context.Response.ContentType = "text/html";
string name = context.Request["username"];
string Pwd = context.Request["pwd"];
string path = context.Request.MapPath("Login.htm"); //将Login.htm文件的相对路径改为绝对路径
string loginhtml = File.ReadAllText(path);//读取文件内容
string viewstate = context.Request["viewstate"];
bool IsPostBack = !string.IsNullOrEmpty(viewstate);
if (!IsPostBack)//第一次访问页面
{
HttpCookie cookie = context.Request.Cookies["Login"];
if (cookie!=null)
{
//获取客户端保存的HttpCookie对象或值:context.Request.Cookies["Login"]
string username = cookie["name"];
string userpwd = cookie.Values["pwd"];
loginhtml = loginhtml.Replace("$name", username).Replace("$pwd", userpwd);
}
else
{
loginhtml = loginhtml.Replace("$name", "").Replace("$pwd", "");
}
context.Response.Write(loginhtml);
return;
}
string sql = "select count(*) from Users where UserName=@username and Pwd=@pwd";
SqlParameter[] sps ={
new SqlParameter("@username",name),
new SqlParameter("@Pwd",Pwd)
};
int result = Convert.ToInt32(sqlhelper.GetExecuteScalar(sql, sps));
if (result > 0)
{
//HttpCookie cookie = new HttpCookie("Login");
//cookie.Values["name"] = name;
//cookie["pwd"] = Pwd;
//cookie.Expires = DateTime.Now.AddMinutes(1);
//context.Response.Cookies.Add(cookie);
context.Response.Cookies["Login"]["name"] = name;
context.Response.Cookies["Login"]["pwd"] = Pwd;
context.Response.Cookies["Login"].Expires = DateTime.Now.AddMinutes(1);
context.Response.Write("登陆成功!");
context.Session["user"] = name;
context.Application.Lock();//修改Application数据之前,需要先加锁处理,防止别人登录(一次只让一个人登陆,防止多人同时修改数据)
context.Application["online"] = Convert.ToInt32(context.Application["online"]) + 1;
context.Application.UnLock();//修改Application数据之后,需要先解锁处理,以供别人登录
context.Response.Redirect("ApplicationTest.aspx");
//context.Response.Write(cookie.Value);
}
else
{
loginhtml = loginhtml.Replace("$name",name).Replace("$pwd",Pwd);
context.Response.Write(loginhtml);
context.Response.Write("<script>alert('登陆失败!')</script>");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
1.AddStudent.htm
1-1
<body>
<form action="AddStudent.ashx" method="post">
<table style="margin:10px auto">
<tr><td colspan="2" style="text-align:center">添加学生信息</td></tr>
<tr><td>学号:</td><td>
<input id="Text1" type="text" name="stuNo" /></td></tr>
<tr><td>姓名:</td><td>
<input id="Text2" type="text" name="stuName" /></td></tr>
<tr><td>性别</td><td>
<input id="Radio1" type="radio" name="sex" value="男" />男
<input id="Radio2" type="radio" name="sex" value="女" />女</td></tr>
<tr><td>出生日期:</td><td>
<input id="Text4" type="text" name="birthday" /></td></tr>
<tr><td>电话:</td><td>
<input id="Text5" type="text" name="phone" /></td></tr>
<tr><td>地址:</td><td>
<input id="Text6" type="text" name="address" /></td></tr>
<tr><td>Email:</td><td>
<input id="Text7" type="text" name="email" /></td></tr>
<tr><td colspan="2" style="text-align:center">
<input id="Submit1" type="submit" value="添加" /></td></tr>
</table>
</form>
</body>
1-2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data.SqlClient;
using FirstWeb;
namespace Lesson3
{
/// <summary>
/// AddStudent 的摘要说明
/// </summary>
public class AddStudent : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//1.接收数据,这是从添加页面的name="stuName"获取的用户输入的信息
string stuName = context.Request["stuName"];
string stuNo = context.Request["stuNo"];
string sex = context.Request["sex"];
string birthday = context.Request["birthday"];
string phone = context.Request["phone"];
string address = context.Request["address"];
string email = context.Request["email"];
//将htm文件的相对路径转为绝对路径
string path = context.Request.MapPath("AddStudent.htm");
string html = File.ReadAllText(path);
//2.对接收数据进行处理
string msg = "";
if (string.IsNullOrEmpty(stuNo))
{
msg =msg + "学号不能为空";
//context.Response.Write("<script>alert('学号不能为空!')</script>");
}
if (string.IsNullOrEmpty(sex))
{
msg += "\\n请选择性别";
}
if (!string.IsNullOrEmpty(birthday))
{
DateTime birth;
bool tag = DateTime.TryParse(birthday, out birth);
if (!tag)
{
msg += "\\n日期格式错误";
}
}
if (msg!="")
{
context.Response.Write("<script>alert('"+msg+"')</script>");
context.Response.Write(html);
context.Response.End();
}
string sql = "declare @loginId int insert into users values(@username,@pwd);set @loginId=scope_identity();" +
"insert into tab_student values (@stuNo,@stuName,@sex,@birthday,@phone," +
"@address,@email,@IsDel,@loginId)";
SqlParameter[] sps = {
new SqlParameter("@username",stuNo),
new SqlParameter("@pwd",stuNo),
new SqlParameter("@stuNo",stuNo),
new SqlParameter("@stuName",stuName),
new SqlParameter("@sex",sex),
new SqlParameter("@birthday",birthday),
new SqlParameter("@phone",phone),
new SqlParameter("@address",address),
new SqlParameter("@email",email),
new SqlParameter("@IsDel",false)
};
int insert = sqlhelper.GetExecuteNotQuery(sql, sps);
//string sql = "insert into users values(@username,@pwd);select scope_identity()";
//获取Users表里的主键id
//SqlParameter[] sps ={
// new SqlParameter("@username",stuNo),
// new SqlParameter("@pwd",stuNo)
// };
//int loginId = Convert.ToInt32(sqlhelper.GetExecuteScalar(sql,sps));//查询login的id
//string sql1 = "insert into tab_student values (@stuNo,@stuName,@sex,@birthday,@phone,@address,@email,@IsDel,@loginId) ";
//SqlParameter[] sps1 = {
// new SqlParameter("@stuNo",stuNo),
// new SqlParameter("@stuName",stuName),
// new SqlParameter("@sex",sex),
// new SqlParameter("@birthday",birthday),
// new SqlParameter("@phone",phone),
// new SqlParameter("@address",address),
// new SqlParameter("@email",email),
// new SqlParameter("@IsDel",false),
// new SqlParameter("@loginId",loginId),
// };
//int insert = sqlhelper.GetExecuteNotQuery(sql1, sps1);
if (insert>0)
{
// context.Response.Write("<script>alert('登陆成功')</script>");
context.Response.Redirect("StudentList.ashx");
//context.Response.Write(html);
//context.Response.End();
}
else
{
context.Response.Write("<script>alert('添加失败')</script>");
context.Response.Write(html);
context.Response.End();
}
//context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
2
2-1StudentList.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function confrim() {
if (confirm("是否要删除!"))
{ return true; }
return false;
}
</script>
</head>
<body>
<div style="width:800px">
<input type="button" name="sub" value="添加" οnclick="Add();" />
<script type="text/javascript">
function Add() {
window.location.href = "AddStudent.htm";
}
</script>
<!--//<form action="StudentList.ashx" method="post">-->
<form method="post">
$tbody
</form>
</div>
</body>
</html>
2-2StudentList.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Data;
using FirstWeb;
using System.IO;
namespace Lesson3
{
/// <summary>
/// StudentList 的摘要说明
/// </summary>
public class StudentList : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string flag = context.Request.QueryString["flag"];
if (!string.IsNullOrEmpty(flag))
{
int result = Convert.ToInt32(flag);
if (result > 0)
{
context.Response.Write("<script>alert('删除成功')</script>");
}
else
{
context.Response.Write("<script>alert('删除失败')</script>");
}
}
context.Response.ContentType = "text/html";
string path= context.Request.MapPath("StudentList.htm");
string html = File.ReadAllText(path);
StringBuilder table = new StringBuilder();
table.Append("<table style='width:800px;margin:10px auto;text-align:center;'>");
table.Append("<tr><th>学号</th><th>姓名</th><th>性别</th><th>出生日期</th><th>年纪</th><th>电话</th><th>地址</th><th>Email</th><th>操作</th></tr>");
string sql = "select * from tab_student";
DataTable dt = sqlhelper.GetDataTable1(sql);
foreach (DataRow dr in dt.Rows)
{
string age = "";
if (dr["birthday"] == DBNull.Value)
{
age = "未知";
}
else
{
int nowyear = DateTime.Now.Year;
int oldyear = Convert.ToDateTime(dr["birthday"]).Year;
if (oldyear == 1900)
{ age = "未知"; }
else
{
age = (nowyear - oldyear).ToString();
}
}
string stuNo = dr["stuNo"].ToString();
string stuName = dr["stuName"].ToString();
string sex = dr["sex"].ToString();
string birthday = dr["birthday"].ToString();
string phone = dr["phone"].ToString();
string address = dr["address"].ToString();
string email = dr["email"].ToString();
string loginid=dr["loginId"].ToString();
table.Append("<tr><td>" + stuNo + "</td><td>" + stuName + "</td><td>" + sex + "</td><td>" + birthday + "</td><td>" + age + "</td><td>"
+ phone + "</td><td>" + address + "</td><td>" + email + "</td>"
+"<td><a href='StudentDel.ashx?id=" + loginid + "' οnclick='return confrim(\"是否要删除\")'>删除</a>"
+" <a href='StudentExitShow.ashx?id=" + loginid + "'</td>修改</tr>");
}
table.Append("</table>");
// table.ToString();
html = html.Replace("$tbody", table.ToString());
context.Response.Write(html);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
<div style="width:800px">
<input type="button" name="sub" value="添加" οnclick="Add();" />
<script type="text/javascript">
function Add() {
window.location.href = "AddStudent.htm";
}
</script>
<!--//<form action="StudentList.ashx" method="post">-->
<form method="post">
$tbody
</form>
</div>
</body>
</html>
3
3-1StudentDel.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace Lesson3
{
/// <summary>
/// StudentDel 的摘要说明
/// </summary>
public class StudentDel : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
// context.Response.Write("Hello World");
string id = context.Request.QueryString["id"];
if (string.IsNullOrEmpty(id))
{
return;
}
string sql = "delete from tab_student where loginId=@loginId "
+"delete from Users where ID=@loginId";
SqlParameter sps = new SqlParameter("@loginId", id);
int result = FirstWeb.sqlhelper.GetExecuteNotQuery(sql, sps);
context.Response.Redirect("StudentList.ashx?flag=" + result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
4
4.1StudentExit.htm
<body>
<!--<form action="StudentExit.ashx" method="post">-->
<form method="post">
<input type="hidden" name="hidID" value="@ID" />
<table style="margin:50px auto">
<tr><th colspan="2" style="text-align:left">修改学生信息:</th></tr>
<tr><td class="style1">学号:</td><td><input type="text" name="stuNo" value="@stuNo" /></td></tr>
<tr><td class="style1">姓名:</td><td><input type="text" name="stuName" value="@stuName" /></td></tr>
<tr><td class="style1">性别:</td><td><input type="radio" name="sex" value="男" boy="@boy" />男<input type="radio" name="sex" value="女" girl="@girl" />女</td></tr>
<tr><td class="style1">出生日期:</td><td><input type="text" name="birthday" value="@birthday" /></td></tr>
<tr><td class="style1">电话:</td><td><input type="text" name="phone" value="@phone"/></td></tr>
<tr><td class="style1">地址:</td><td><input type="text" name="address"value="@address" /></td></tr>
<tr><td class="style1">Email</td><td><input type="text" name="email" value="@email" /></td></tr>
<tr><td colspan="2" style="text-align:center"><input type="submit" value="修改" />
<input type="button" value="返回" οnclick="funhui()" /></td></tr>
</table>
<script type="text/javascript">
function funhui() {
window.location.href = "StudentList.ashx";
}
</script>
<!--<script type="text/javascript">
function Exit() {
window.location.href = "StudentExit.ashx";
}
</script>-->
</form>
</body>
4.2StudentExitShow.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace Lesson3
{
/// <summary>
/// StudentExitShow 的摘要说明
/// </summary>
public class StudentExitShow : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//context.Response.Write("Hello World");
string path = context.Request.MapPath("StudentExit.htm");
string html = File.ReadAllText(path);
string loginid = context.Request.QueryString["id"];
if (string.IsNullOrEmpty(loginid))
{
context.Response.Redirect("StudentList.ashx");
}
string id = context.Request["hidID"];
bool IspostBack = !string.IsNullOrEmpty(id);
if (!IspostBack)
{
string sql = "select [ID],[stuNo],[stuName],[sex],convert(varchar,birthday,111) as birthday,[phone],[address],[email],[IsDel],[loginId] from tab_student where [loginId]=@loginid";
SqlParameter[] sps = { new SqlParameter("@loginid", loginid) };
DataTable dt = FirstWeb.sqlhelper.GetDataTable1(sql, sps);
html = html.Replace("@stuNo", dt.Rows[0]["stuNo"].ToString());
html = html.Replace("@stuName", dt.Rows[0]["stuName"].ToString());
html = html.Replace("@phone", dt.Rows[0]["phone"].ToString());
html = html.Replace("@address", dt.Rows[0]["address"].ToString());
html = html.Replace("@email", dt.Rows[0]["email"].ToString());
html = html.Replace("@ID", dt.Rows[0]["loginid"].ToString());
string sex = dt.Rows[0]["sex"].ToString();
if (sex == "男")
{
html = html.Replace("boy=\"@boy\"", "checked");
}
else
{ html = html.Replace("girl=\"@girl\"", "checked"); }
if (dt.Rows[0]["birthday"].ToString().Substring(0, 4) == "1900")
{
html = html.Replace("@birthday", "");
}
else
{ html = html.Replace("@birthday", dt.Rows[0]["birthday"].ToString()); }
context.Response.Write(html);
}
else
{
string stuNo = context.Request["stuNo"];
string stuName = context.Request["stuName"];
string sex = context.Request["sex"];
string birthday = context.Request["birthday"];
string phone = context.Request["phone"];
string address = context.Request["address"];
string email = context.Request["email"];
string sql = "update tab_student set stuNo=@stuNo,stuName=@stuName,sex=@sex,birthday=@birthday,phone=@phone,address=@address,email=@email where [loginId]=@loginId "
+ "update Users set [UserName]=@stuNo,[Pwd]=@stuNo where [ID]=@loginId";
SqlParameter[] sps ={
new SqlParameter("@stuNo",stuNo),
new SqlParameter("@stuName",stuName),
new SqlParameter("@sex",sex),
new SqlParameter("@birthday",birthday),
new SqlParameter("@phone",phone),
new SqlParameter("@address",address),
new SqlParameter("@email",email),
new SqlParameter("@loginId",id)
};
int result = FirstWeb.sqlhelper.GetExecuteNotQuery(sql, sps);
if (result > 0)
{
context.Response.Redirect("StudentList.ashx");
}
else
{
context.Response.Write("<script>alert('修改失败!')</script>");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
-------------------------------------------
-----------------------------------
1ban 2ban 3ban 4ban RadioButtonList
只要是列表xxList都有ListItem 列表选项
RadioButtonList1就是班级框列表
操作代码都是写在按钮里的,然后在按钮里绑定其控件
if(RadioButtonList1.selectedIndex<0)
{
response.write("<script>alert('选择班级')</script>");
return;
}
Response.Write("<script>alert('你选择的班级的index是"+RadioButtonList1.SelectedIndex+"')</script>");
Response.Write("");
----------------
男 女
string s="";
if(RadioButton1.cheched)
{
s=RadioButton1.Text;
}
if(RadioButton2.Checked)
{
s=RadioButton2.Text;
}
response.write("<script>alert('"+s+"')</script>");
--------------------
爱好:篮球 足球 多选框 CheckBoxList1
string s="";
for循环出所有选中的
foreach(ListItem item in CheckBoxList1.item)
{
if(item.Selected)
{
s=s+item.Text+"";
}
response.Write("<script>alert('"+s+"')</script>");
}