首页 > 代码库 > ASP.NET 一般处理程序用户数据的修改
ASP.NET 一般处理程序用户数据的修改
Modify.htm代码如下
<!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="update.ashx">
<input type="hidden" name="EmpId" value="http://www.mamicode.com/$EmpId" />
<table>
<tr>
<td>EmpName</td>
<td><input type="text" value="http://www.mamicode.com/$EmpName" name="EmpName"/></td>
</tr>
<tr>
<td>EmpAge</td>
<td><input type="text" value="http://www.mamicode.com/$EmpAge" name="EmpAge"/></td>
</tr>
<tr>
<td>DelFlag</td>
<td><input type="text" value="http://www.mamicode.com/$DelFlag" name="DelFlag"/></td>
</tr>
<tr>
<td cospan="2"><input type="submit" value="http://www.mamicode.com/修改" /></td>
</tr>
</table>
</form>
</body>
</html>
Modify.ashx的接收数据并展示单挑数据到表单,数据如下
<%@ WebHandler Language="C#" Class="Modify" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
public class Modify : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
int id;
if(int.TryParse(context.Request.QueryString["id"],out id))
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(ConnStr))
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT * from Employee where EmpId=@id", conn))
{
SqlParameter sp = new SqlParameter("@id", SqlDbType.Int);
sp.Value = http://www.mamicode.com/id;
da.SelectCommand.Parameters.Add(sp);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
string htmlpath = context.Server.MapPath("Modify.htm");
string content = File.ReadAllText(htmlpath);
content = content.Replace("$EmpId", dt.Rows[0]["EmpId"].ToString()).Replace("$EmpName", dt.Rows[0]["EmpName"].ToString()).Replace("$EmpAge", dt.Rows[0]["EmpAge"].ToString()).Replace("$DelFlag", dt.Rows[0]["DelFlag"].ToString());
context.Response.Write(content);
}
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
更新数据提交到update.ashx页面代码如下
<%@ WebHandler Language="C#" Class="update" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.IO;
public class update : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
int id;
if (int.TryParse(context.Request.Form["EmpId"], out id))
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText="update employee set EmpName=@EmpName,EmpAge=@EmpAge,DelFlag=@DelFlag where EmpId=@id";
cmd.Parameters.Add("@EmpName", SqlDbType.VarChar);
cmd.Parameters.Add("@EmpAge", SqlDbType.Int);
cmd.Parameters.Add("@DelFlag", SqlDbType.Int);
cmd.Parameters.Add("@id", 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"];
cmd.Parameters["@id"].Value = http://www.mamicode.com/id;
if (cmd.ExecuteNonQuery() > 0)
{
context.Response.Redirect("UserList.ashx");
}
else
{
context.Response.Redirect("error.htm");
}
}
}
}
else
{
context.Response.Write("参数错误");
}
}
public bool IsReusable {
get {
return false;
}
}
}
ASP.NET 一般处理程序用户数据的修改