ASP.NET 一般處理常式使用者資料的修改

來源:互聯網
上載者:User

標籤: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 [email protected]", 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 [email protected],[email protected],[email protected] where [email protected]";
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 一般處理常式使用者資料的修改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.