ASP.NET – ASHX + jQuery POST + URL GET + Select Record + Update Record 使用方法

 
ASP.NET – ASHX + jQuery POST + URL GET + Select Record + Update Record 使用方法
 

   WebForm2.aspx
 
   <%@ Page Language="C#" AutoEventWireup="true"
   CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication13.WebForm2" %>
 
   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
 
      <title></title>
      <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
      <script language="javascript" type="text/javascript">
 
         $(document).ready(function () {
            $("#Insert_btn").click(function () {
 
               var str1 = $("#TextBox1").val();
               var str2 = $("#TextBox2").val();
               var str3 = $("#TextBox3").val();
               var str4 = $("#TextBox4").val();
               var str5 = $("#TextBox5").val();
               var str6 = $("#TextBox6").val();
               var id = urlgetparameter("ID");
 
               $.post("Insert.ashx", {
                  id: id,
                  data1: str1,
                  data2: str2,
                  data3: str3,
                  data4: str4,
                  data5: str5,
                  data6: str6
               }, function (data) {
                  window.location = "WebForm2.aspx?ID=" + id;
               });
 
            });
         });
 
         function urlgetparameter(name) {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(window.location.href);
            if (results == null) return "";
            else return results[1];
         }
 
      </script>
 
   </head>
   <body>
 
      <form id="form1" runat="server">
      <div>
 
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
 
         <tr>
         <td>Column 1 :</td>
         <td><asp:TextBox ID="TextBox1" Text="" runat="server" /></td>
         </tr>
 
         <tr>
         <td>Column 2 :</td>
         <td><asp:TextBox ID="TextBox2" Text="" runat="server" /></td>
         </tr>
 
         <tr>
         <td>Column 3 :</td>
         <td><asp:TextBox ID="TextBox3" Text="" runat="server" /></td>
         </tr>
 
         <tr>
         <td>Column 4 :</td>
         <td><asp:TextBox ID="TextBox4" Text="" runat="server" /></td>
         </tr>
 
         <tr>
         <td>Column 5 :</td>
         <td><asp:TextBox ID="TextBox5" Text="" runat="server" /></td>
         </tr>
 
         <tr>
         <td>Column 6 :</td>
         <td><asp:TextBox ID="TextBox6" Text="" runat="server" /></td>
         </tr>
 
         <tr>
         <td>
         <input id="Insert_btn" type="button" value="Update" />
         </td>
         </tr>
 
      </table>
      <br />
 
      </div>
      </form>
 
   </body>
   </html>
 

 

   WebForm2.aspx.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;
   using System.Data.SqlClient;
   using System.Text;
 
   namespace WebApplication13
   {
      public partial class WebForm2 : System.Web.UI.Page
      {
         protected void Page_Load(object sender, EventArgs e)
         {
            if (Request.QueryString["ID"] != null)
            {
               try
               {
                  String connectionString1 = "Data Source=VECTUS\\EXPRESS; Initial Catalog=Soccer; Integrated Security=SSPI;";
                  SqlConnection con1 = new SqlConnection(connectionString1);
                  con1.Open();
 
                  StringBuilder sb = new StringBuilder();
                  sb.Append("SELECT * FROM dbo.Test WHERE ID = ");
                  sb.Append(Request.QueryString["ID"].ToString());
 
                  SqlCommand cmd1 = new SqlCommand(sb.ToString(), con1);
                  SqlDataReader dr = cmd1.ExecuteReader();
 
                  if (dr.HasRows)
                  {
                     while (dr.Read())
                     {
                        TextBox1.Text = dr["Column_1"].ToString();
                        TextBox2.Text = dr["Column_2"].ToString();
                        TextBox3.Text = dr["Column_3"].ToString();
                        TextBox4.Text = dr["Column_4"].ToString();
                        TextBox5.Text = dr["Column_5"].ToString();
                        TextBox6.Text = dr["Column_6"].ToString();
                     }
                  }
               }catch(Exception ex){}
            }
         }
      }
   }
 

 

   Insert.ashx.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.UI.WebControls;
   using System.Data.SqlClient;
   using System.Text;
 
   namespace WebApplication13
   {
      /// <summary>
      ///Handler1 的摘要描述
      /// </summary>
      public class Handler1 : IHttpHandler
      {
         public void ProcessRequest(HttpContext context)
         {
            context.Response.ContentType = "text/plain";
 
            String ResponseString = "";
            ResponseString += context.Request.Form["id"] + "<br />";
            ResponseString += context.Request.Form["data1"] + "<br />";
            ResponseString += context.Request.Form["data2"] + "<br />";
            ResponseString += context.Request.Form["data3"] + "<br />";
            ResponseString += context.Request.Form["data4"] + "<br />";
            ResponseString += context.Request.Form["data5"] + "<br />";
            ResponseString += context.Request.Form["data6"] + "<br />";
 
            UpdateRecord(context);
 
            context.Response.Write(ResponseString);
 
         }
 
         private void UpdateRecord(HttpContext context)
         {
            try
            {
               String ConnectionString = "Data Source=VECTUS\\EXPRESS; Initial Catalog=Soccer; Integrated Security=SSPI;";
               SqlConnection conn = new SqlConnection(ConnectionString);
               conn.Open();
 
               StringBuilder SQLStatement = new StringBuilder();
               SQLStatement.Append("UPDATE Test SET ");
               SQLStatement.Append("Column_1=@Col1, ");
               SQLStatement.Append("Column_2=@Col2, ");
               SQLStatement.Append("Column_3=@Col3, ");
               SQLStatement.Append("Column_4=@Col4, ");
               SQLStatement.Append("Column_5=@Col5, ");
               SQLStatement.Append("Column_6=@Col6 ");
               SQLStatement.Append("WHERE ID = @ID");
 
               SqlCommand cmd = new SqlCommand(SQLStatement.ToString(), conn);
               cmd.Parameters.AddWithValue("@ID", context.Request.Form["id"]);
               cmd.Parameters.AddWithValue("@Col1", context.Request.Form["data1"]);
               cmd.Parameters.AddWithValue("@Col2", context.Request.Form["data2"]);
               cmd.Parameters.AddWithValue("@Col3", context.Request.Form["data3"]);
               cmd.Parameters.AddWithValue("@Col4", context.Request.Form["data4"]);
               cmd.Parameters.AddWithValue("@Col5", context.Request.Form["data5"]);
               cmd.Parameters.AddWithValue("@Col6", context.Request.Form["data6"]);
               cmd.ExecuteNonQuery();
 
               conn.Dispose();
               cmd.Dispose();
               conn.Close();
            }catch(Exception ex){}
         }
 
         public bool IsReusable
         {
            get
            {
               return false;
            }
         }
      }
   }