首页 > 代码库 > ASP.NET 一般处理程序展示添加用户
ASP.NET 一般处理程序展示添加用户
添加用户模版,直接展示即可
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form method="post" action="add.ashx">
<table>
<tr>
<td>EmpName</td>
<td><input type="text" value="" name="EmpName"/></td>
</tr>
<tr>
<td>EmpAge</td>
<td><input type="text" value="" name="EmpAge"/></td>
</tr>
<tr>
<td>DelFlag</td>
<td><input type="text" value="" name="DelFlag"/></td>
</tr>
<tr>
<td cospan="2"><input type="submit" value="http://www.mamicode.com/添加数据" /></td>
</tr>
</table>
</form>
</body>
</html>
POST提交处理页面add.ashx页面负责处理数据添加
<%@ WebHandler Language="C#" Class="add" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.IO;
public class add : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "insert into employee(empname,empage,delflag)values(@empname,@empage,@delflag)";
cmd.Connection = conn;
cmd.Parameters.Add("@empname",SqlDbType.VarChar);
cmd.Parameters.Add("@empage", SqlDbType.Int);
cmd.Parameters.Add("@delflag", SqlDbType.Int);
cmd.Parameters["@empname"].Value = http://www.mamicode.com/context.Request.Form["EmpName"];
cmd.Parameters["@empage"].Value = http://www.mamicode.com/context.Request.Form["EmpAge"];
cmd.Parameters["@delflag"].Value = http://www.mamicode.com/context.Request.Form["DelFlag"];
conn.Open();
if (cmd.ExecuteNonQuery() > 0)
{
context.Response.Redirect("UserList.ashx");
}
else
{
context.Response.Redirect("error.htm");
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
ASP.NET 一般处理程序展示添加用户