一般处理程序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

<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>
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);  
            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();
            }           
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

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>
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;
            }
        }
    }
}

 

1 ban 2 ban 3 ban 4 ban 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>");
}

 

posted @ 2017-12-29 09:50  ProZkb  阅读(483)  评论(0编辑  收藏  举报