ajaxpro实现无刷新更新数据库【简单方法】

时间:2022-11-06 20:05:29

原文发布时间为:2008-10-24 —— 来源于本人的百度文章 [由搬家工具导入]

我用的是AjaxPro.2.dll,然后我想点击那个 “无刷新更新” 那个按钮,实现 无刷新 修改表中的内容

HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!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 runat="server"> <title>无标题页</title> <script type="text/javascript"> function gx() { var u=document.getElementById("TextBox1").value; var p=document.getElementById("TextBox2").value; var s=document.getElementById("TextBox3").value; Default2.gengxin(u,p,s,callback); } function callback(res) { if(res.value==true)
      {
          alert('提交成功');         
          document.getElementById("Button2").disabled=false;
          document.getElementById("Button2").value="无刷新更新";
   }
      } </script></head><body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> <br /> <asp:TextBox ID="TextBox3" runat="server" OnTextChanged="TextBox3_TextChanged"></asp:TextBox><br /> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="普通更新" /><br /> <br />         &nbsp;<input id="Button2" type="button" value="无刷新更新" onclick="this.disabled=true;this.value='正在提交当中';return gx()"/></div>
</form></body></html>
C# code
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;using AjaxPro;public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { AjaxPro.Utility.RegisterTypeForAjax(typeof(Default2)); } [AjaxPro.AjaxMethod] public bool gengxin(string u,string p,string s) { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConn"].ConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand("Proc_UpdateTest", conn); cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] param = new SqlParameter[3]; param[0] = new SqlParameter("@user", SqlDbType.NChar, 10); param[0].Value = u; param[1] = new SqlParameter("@paper", SqlDbType.NChar, 10); param[1].Value = p; param[2] = new SqlParameter("@score", SqlDbType.NChar, 10); param[2].Value = s; foreach (SqlParameter Parameter in param) cmd.Parameters.Add(Parameter); cmd.ExecuteNonQuery(); //SqlCommand cmd = new SqlCommand("update Paper set paper='" +p + "',score='" + s+"' where user='" +u + "'",conn); //cmd.ExecuteNonQuery(); conn.Close(); return true; } protected void Button1_Click(object sender, EventArgs e) { gengxin(TextBox1.Text,TextBox2.Text,TextBox3.Text); } protected void TextBox3_TextChanged(object sender, EventArgs e) { }}