ASP.NET – ASHX + JQuery POST ( 做出 GridView + Select + Update + Delete 相同效果 ) 方法

 
ASP.NET – ASHX + JQuery POST ( 做出 GridView + Select + Update + Delete 相同效果 ) 方法
 

   WebForm3.aspx
 
   <head runat="server">
 
      … …
 
      <script src="http://code.jquery.com/jquery-latest.js"></script>
      <script language="javascript" type="text/javascript">
 
         $(document).ready(function () {
            <asp:Literal ID="Literal2" runat="server" />
         });
 
      </script>
 
   </head>
 
   <body>
 
      <form id="form1" runat="server">
         <div>
 
            <table border="1" cellpadding="0" cellspacing="0" width="850">
               <asp:Literal ID="Literal1" runat="server" />
            </table>
            <br />
            <span id="mess"></span>
 
         </div>
      </form>
 
   </body>
 

 

   WebForm3.aspx.cs
 
   using System;
   using System.Web;
   using System.Data.SqlClient;
   using System.Text;
 
   namespace WebApplication13
   {
      public partial class WebForm3 : System.Web.UI.Page
      {
         protected void Page_Load(object sender, EventArgs e)
         {
            String ConnectionString = "Data Source=VECTUS\\EXPRESS; Initial Catalog=Soccer; Integrated Security=SSPI;";
            SqlConnection con1 = new SqlConnection(ConnectionString);
            con1.Open();
 
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT [ID] ");
            sb.Append(",[Column_1] ");
            sb.Append(",[Column_2] ");
            sb.Append("FROM [Soccer].[dbo].[Test];");
 
            SqlCommand cmd1 = new SqlCommand(sb.ToString(), con1);
            SqlDataReader dr = cmd1.ExecuteReader();
 
            if (dr.HasRows)
            {
               while (dr.Read())
               {
                  Literal1.Text += "<tr>\n";
 
                  Literal1.Text += "<td width='150'>  \n";
                  Literal1.Text += "<span id='S" + dr["ID"].ToString() + "' style='display:;'>SELECT</span>\n";
                  Literal1.Text += "  <span id='E" + dr["ID"].ToString() + "' style='display:;'>EDIT</span>\n";
                  Literal1.Text += "<span id='U" + dr["ID"].ToString() + "' style='display:none;'>UPDATE</span>\n";
                  Literal1.Text += "  <span id='D" + dr["ID"].ToString() + "'>DELETE</span>\n";
                  Literal1.Text += "  </td>\n";
 
                  Literal1.Text += "<td width='10'>";
                  Literal1.Text += "<span id='ID" + dr["ID"].ToString() + "'>" + dr["ID"].ToString() + "</span>";
                  Literal1.Text += "</td>\n";
 
                  Literal1.Text += "<td width='200'>";
                  Literal1.Text += "<span id='SpanCol1_" + dr["ID"].ToString() + "' style='display:;'>" + dr["Column_1"].ToString() + "</span>";
                  Literal1.Text += "<input type='text' id='TextBoxCol1_" + dr["ID"].ToString() + "' value='" + dr["Column_1"].ToString() + "' style='display:none;' />";
                  Literal1.Text += "</td>\n";
 
                  Literal1.Text += "<td width='200'>";
                  Literal1.Text += "<span id='SpanCol2_" + dr["ID"].ToString() + "' style='display:;'>" + dr["Column_2"].ToString() + "</span>";
                  Literal1.Text += "<input type='text' id='TextBoxCol2_" + dr["ID"].ToString() + "' value='" + dr["Column_2"].ToString() + "' style='display:none;' />";
                  Literal1.Text += "</td>\n";
 
                  Literal1.Text += "</tr>";
 
                  Literal2.Text += "$(\"#S" + dr["ID"].ToString() + "\").click(function () {";
 
                  Literal2.Text += "var result = \"\";";
                  Literal2.Text += "result += \"ID : " + dr["ID"].ToString() + "<br>\";";
                  Literal2.Text += "result += \"Column 1 : " + dr["Column_1"].ToString() + "<br>\";";
                  Literal2.Text += "result += \"Column 2 : " + dr["Column_2"].ToString() + "<br>\";";
 
                  Literal2.Text += "$(\"#mess\").html(result);";
 
                  Literal2.Text += "});\n";
 
                  Literal2.Text += "$(\"#E" + dr["ID"].ToString() + "\").click(function () {";
 
                  Literal2.Text += "$(\"#E" + dr["ID"].ToString() + "\").hide();";
                  Literal2.Text += "$(\"#SpanCol1_" + dr["ID"].ToString() + "\").hide();";
                  Literal2.Text += "$(\"#SpanCol2_" + dr["ID"].ToString() + "\").hide();";
 
                  Literal2.Text += "$(\"#U" + dr["ID"].ToString() + "\").show();";
                  Literal2.Text += "$(\"#TextBoxCol1_" + dr["ID"].ToString() + "\").show();";
                  Literal2.Text += "$(\"#TextBoxCol2_" + dr["ID"].ToString() + "\").show();";
 
                  Literal2.Text += "});\n";
 
                  Literal2.Text += "$(\"#U" + dr["ID"].ToString() + "\").click(function () {";
 
                  Literal2.Text += "var str1 = $('#TextBoxCol1_" + dr["ID"].ToString() + "').val();";
                  Literal2.Text += "var str2 = $('#TextBoxCol2_" + dr["ID"].ToString() + "').val();";
                  Literal2.Text += "var id = $('#ID" + dr["ID"].ToString() + "').html();\n\n";
 
                  Literal2.Text += "$.post(\"Update.ashx\", {\n";
                  Literal2.Text += "id: id,\n";
                  Literal2.Text += "col1: str1,\n";
                  Literal2.Text += "col2: str2\n";
                  Literal2.Text += "}, function (data) {\n";
                  Literal2.Text += "window.location = 'WebForm3.aspx';\n";
                  Literal2.Text += "});\n";
 
                  Literal2.Text += "});\n";
 
                  Literal2.Text += "$(\"#D" + dr["ID"].ToString() + "\").click(function () {";
 
                  Literal2.Text += "var id = $('#ID" + dr["ID"].ToString() + "').html();\n\n";
 
                  Literal2.Text += "$.post(\"Delete.ashx\", {\n";
                  Literal2.Text += "id: id,\n";
                  Literal2.Text += "}, function (data) {\n";
                  Literal2.Text += "window.location = 'WebForm3.aspx';\n";
                  Literal2.Text += "});\n";
 
                  Literal2.Text += "});\n";
 
               }
            }
 
            con1.Close();
            con1.Dispose();
 
         }
      }
   }
 

 

   Update.ashx.cs
 
   using System;
   using System.Web;
   using System.Data.SqlClient;
   using System.Text;
 
   namespace WebApplication13
   {
      /// <summary>
      ///Update 的摘要描述
      /// </summary>
      public class Update : IHttpHandler
      {
 
         public void ProcessRequest(HttpContext context)
         {
            context.Response.ContentType = "text/plain";
            UpdateRecord(context);
            //context.Response.Write(context.Request.Form["id"] + "<br />" + context.Request.Form["col1"] + "<br />" + context.Request.Form["col2"]);
         }
 
         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("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["col1"]);
               cmd.Parameters.AddWithValue("@Col2", context.Request.Form["col2"]);
               cmd.ExecuteNonQuery();
 
               conn.Dispose();
               cmd.Dispose();
               conn.Close();
            }
            catch (Exception ex) {}
         }
 
         public bool IsReusable
         {
            get
            {
               return false;
            }
         }
      }
   }
 

 

   Delete.ashx.cs
 
   using System;
   using System.Web;
   using System.Data.SqlClient;
   using System.Text;
 
   namespace WebApplication13
   {
      ///

      ///Delete 的摘要描述
      ///

      public class Delete : IHttpHandler
      {
 
         public void ProcessRequest(HttpContext context)
         {
            context.Response.ContentType = "text/plain";
            DeleteRecord(context);
         }
 
         private void DeleteRecord(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("DELETE FROM Test ");
               SQLStatement.Append("WHERE ID = @ID");
 
               SqlCommand cmd = new SqlCommand(SQLStatement.ToString(), conn);
               cmd.Parameters.AddWithValue("@ID", context.Request.Form["id"]);
               cmd.ExecuteNonQuery();
 
               conn.Dispose();
               cmd.Dispose();
               conn.Close();
            }
            catch (Exception ex) { }
         }
 
         public bool IsReusable
         {
            get
            {
               return false;
            }
         }
      }
   }