登录页面
<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="添加" onclick="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 + "' onclick='return confrim(\"是否要删除\")'>删除</a>"
+"&nbsp;<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="添加" onclick="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="返回" onclick="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>");
}

一般处理程序cookie和session+末尾的多选框,下拉框的更多相关文章

  1. vue中使用cookie记住用户上次选择(本次例子中为下拉框)

    最近工作中碰到一个需求,添加一条数据时,自动记住上次选择的下拉框的数据,刚开始觉得没思路,后来请教了项目组长,组长直接一句,这不很简单吧,直接用cookie,我:....... 好吧,都王的差不多了, ...

  2. php之Cookie与Session详解

    Cookie管理 Cookie是在HTTP协议下,通过服务器或脚本语言可以维护客户浏览器上信息的一种方式,Cookie的使用很普遍,许多提供个人化服务的网站都是利用Cookie来区别不同用户,以显示与 ...

  3. 为什么你学不会递归?告别递归,谈谈我的一些经验 关于集合中一些常考的知识点总结 .net辗转java系列(一)视野 彻底理解cookie,session,token

    为什么你学不会递归?告别递归,谈谈我的一些经验   可能很多人在大一的时候,就已经接触了递归了,不过,我敢保证很多人初学者刚开始接触递归的时候,是一脸懵逼的,我当初也是,给我的感觉就是,递归太神奇了! ...

  4. Flask:cookie 和 session (0.1)

    Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2 Cookie是什么?有什么用? 某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常 ...

  5. HTTP协议--cookie、session、缓存与代理

    1 Cookie和 Session Cookie和 Session都为了用来保存状态信息,都是保存客户端状态的机制,它们都是为了解决 HTTP无状态的问题而所做的努力. Session可以用 Cook ...

  6. Cookie和Session 简单介绍

    cookie :     1.cookie是存在客户端(浏览器)的进程内存中和客户端所在的机器硬盘上     2.cookie只能能够存储少量文本,大概4K大小     3.cookie是不能在不同浏 ...

  7. Cookie、Session、Token那点事儿和前后端分离之JWT用户认证

    (两篇文章转自:https://www.jianshu.com/p/bd1be47a16c1:https://www.jianshu.com/p/180a870a308a) 什么是Cookie? Co ...

  8. HTTP之Cookie和Session

    1. Cookie 1.1 为什么需要 Cookie? HTTP 协议是一种无状态的协议,也就是说,当前的 HTTP 请求与以前的 HTTP 请求没有任何联系.显然,这种无状态的情形在某些时候将让用户 ...

  9. Spring,SpringMVC,MyBatis,Hibernate,Servlet的生命周期,jsp有哪些内置对象,Tomcat,Cookie和Session的作用以及区别,oracle,MYSQL等面试题总结

    1. 什么是Spring,谈谈你对Spring的理解 Spring是我们JAVA开发人员在搭建后台时选用的一个轻量级的开源框架,Spring框架目前也是功能丰富,十分优秀企业级采用最多的一个框架. S ...

随机推荐

  1. python微框架Bottle(http)

    环境: win7系统 Python2.7 一 背景和概述 眼下项目中须要加入一个激活码功能,打算单独弄一个httpserver来写. 由于之前的游戏中已经有了一套完整的激活码生成工具和验证httpse ...

  2. php中echo什么时候用到\"这个符号

    php中echo什么时候用到\"这个符号 当在引号中用到引号时,为避免混乱用\" \称为转义符,表示后面的字符和原来程序语言里的语法符号含义不同 常见的转义有 \" \' ...

  3. Scala语言

    一.Scala概述 Scala简介 Scala是一种针对JVM将函数和面向对象技术组合在一起的编程语言.所以Scala必须要有JVM才能运行,和Python一样,Scala也是可以面向对象和面向函数的 ...

  4. Linux开放1521端口允许网络连接Oracle Listener

    症状: 1. TCP/IP连接是通的.可以用ping 命令测试. 2. 服务器上Oracle Listener已经启动.   lsnrctl status  查看listener状态   lsnrct ...

  5. winform控件命名规范对照表

    WinForm Control 命名规范 数据类型 数据类型简写 标准命名举例 Label lbl lblMessage LinkLabel llbl llblToday Button btn btn ...

  6. SpringBoot(二) 主程序详解和配置文件如何配置

    SpringBoot主程序详解 /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootApplication ...

  7. STM8S103 STVD编译空间不足

    关于text空间(理解为代码空间)不足问题 # 关于.bsct和.ubsct问题(着重参考http://www.waveshare.net/article/STM8-3-1-10.htm) map文件 ...

  8. C语言-100加减求和

    ----------------------------度娘的思路------------------------------------------------------ Action() { / ...

  9. swift语言点评十一-Methods

    Assigning to self Within a Mutating Method Mutating methods can assign an entirely new instance to t ...

  10. 连类比事-category和extension

    extension看起来很像一个匿名的category,但是extension和有名字的category几乎完全是两个东西. extension在编译期决议,它就是类的一部分,在编译期和头文件里的@i ...