码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET 一般处理程序用户数据的修改

时间:2016-11-28 09:08:48      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:process   ldb   select   list   query   ssr   map   pat   input   

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="$EmpId" />
<table>
<tr>
<td>EmpName</td>
<td><input type="text" value="$EmpName" name="EmpName"/></td>
</tr>
<tr>
<td>EmpAge</td>
<td><input type="text" value="$EmpAge" name="EmpAge"/></td>
</tr>
<tr>
<td>DelFlag</td>
<td><input type="text" value="$DelFlag" name="DelFlag"/></td>
</tr>
<tr>
<td cospan="2"><input type="submit" value="修改" /></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 = 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 = context.Request.Form["EmpName"];
cmd.Parameters["@EmpAge"].Value = context.Request.Form["EmpAge"];
cmd.Parameters["@DelFlag"].Value = context.Request.Form["DelFlag"];
cmd.Parameters["@id"].Value = 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 一般处理程序用户数据的修改

标签:process   ldb   select   list   query   ssr   map   pat   input   

原文地址:http://www.cnblogs.com/zhaodachao/p/6108108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!