一般处理程序cookie和session+末尾的多选框,下拉框
登录页面
<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>"
+" <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+末尾的多选框,下拉框的更多相关文章
- vue中使用cookie记住用户上次选择(本次例子中为下拉框)
最近工作中碰到一个需求,添加一条数据时,自动记住上次选择的下拉框的数据,刚开始觉得没思路,后来请教了项目组长,组长直接一句,这不很简单吧,直接用cookie,我:....... 好吧,都王的差不多了, ...
- php之Cookie与Session详解
Cookie管理 Cookie是在HTTP协议下,通过服务器或脚本语言可以维护客户浏览器上信息的一种方式,Cookie的使用很普遍,许多提供个人化服务的网站都是利用Cookie来区别不同用户,以显示与 ...
- 为什么你学不会递归?告别递归,谈谈我的一些经验 关于集合中一些常考的知识点总结 .net辗转java系列(一)视野 彻底理解cookie,session,token
为什么你学不会递归?告别递归,谈谈我的一些经验 可能很多人在大一的时候,就已经接触了递归了,不过,我敢保证很多人初学者刚开始接触递归的时候,是一脸懵逼的,我当初也是,给我的感觉就是,递归太神奇了! ...
- Flask:cookie 和 session (0.1)
Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2 Cookie是什么?有什么用? 某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常 ...
- HTTP协议--cookie、session、缓存与代理
1 Cookie和 Session Cookie和 Session都为了用来保存状态信息,都是保存客户端状态的机制,它们都是为了解决 HTTP无状态的问题而所做的努力. Session可以用 Cook ...
- Cookie和Session 简单介绍
cookie : 1.cookie是存在客户端(浏览器)的进程内存中和客户端所在的机器硬盘上 2.cookie只能能够存储少量文本,大概4K大小 3.cookie是不能在不同浏 ...
- Cookie、Session、Token那点事儿和前后端分离之JWT用户认证
(两篇文章转自:https://www.jianshu.com/p/bd1be47a16c1:https://www.jianshu.com/p/180a870a308a) 什么是Cookie? Co ...
- HTTP之Cookie和Session
1. Cookie 1.1 为什么需要 Cookie? HTTP 协议是一种无状态的协议,也就是说,当前的 HTTP 请求与以前的 HTTP 请求没有任何联系.显然,这种无状态的情形在某些时候将让用户 ...
- Spring,SpringMVC,MyBatis,Hibernate,Servlet的生命周期,jsp有哪些内置对象,Tomcat,Cookie和Session的作用以及区别,oracle,MYSQL等面试题总结
1. 什么是Spring,谈谈你对Spring的理解 Spring是我们JAVA开发人员在搭建后台时选用的一个轻量级的开源框架,Spring框架目前也是功能丰富,十分优秀企业级采用最多的一个框架. S ...
随机推荐
- leveldb学习:sstable(2)
block写入:block_builder block.h和.cc里定义了block的entry存储格式和restart,提供了entry的查找接口以及迭代器.那么怎样往写block里写entry呢? ...
- Android ImageView 不显示JPEG图片 及 Android Studio中怎样引用图片资源
Android ImageView 不显示JPEG图片 今天在写一个小实例,ImageView在xml里面设置的是INVISIBLE,在代码里须要设置成setVisibility(View.VISIB ...
- ThinkPHP5.0框架开发--第2章 TP5.0架构
ThinkPHP5.0框架开发--第2章 TP5.0架构 第2章 TP5.0架构 ================================================== 上次复习 1.如 ...
- poj--3250--Bad Hair Day(模拟)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- xshell同时发送多条命令
1.如图,勾选撰写栏 勾选全部会话 下面的是一个很方便的小技巧.分屏 如下图垂直排列 水平排列等等
- Hadoop编译源码
Hadoop编译源码 克隆一个虚拟机 然后一步一步安装就行 安装所需:链接: https://pan.baidu.com/s/1jIZlQmi 密码: gggv 5.1 前期准备工作 1)CentOS ...
- jQuery学习(二)——使用JQ完成页面定时弹出广告
1.JQuery效果 2.步骤分析: 第一步:引入jQuery相关的文件 第二步:书写页面加载函数 第三步:在页面加载函数中,获取显示广告图片的元素. 第四步:设置定时操作(显示广告图片的函数) 第五 ...
- BAT 解密(四):配置中心、服务中心、异步技术细节
在系列文章的第二篇文章< BAT解密(二):聊聊业务如何驱动技术发展 >中我们深入分析了互联网业务发展的一个特点:复杂性越来越高.复杂性增加的典型现象就是系统越来越多,当系统的数量增加到一 ...
- POJ 2029 Get Many Persimmon Trees 【 二维树状数组 】
题意:给出一个h*w的矩形,再给出n个坐标,在这n个坐标种树,再给出一个s*t大小的矩形,问在这个s*t的矩形里面最多能够得到多少棵树 二维的树状数组,求最多能够得到的树的时候,因为h,w都不超过50 ...
- shell-4.bash的变量:用户自定义变量
目录 内容