前台代码:
View Code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<style type="text/css" >
body div{ text-align:center}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
输入要查找的关键字:<asp:TextBox runat="server" ID="txtKey"/> <asp:Button runat="server"
ID="btnGo" Text="查找" onclick="btnGo_Click" />
</div>
<div>
<asp:UpdateProgress runat="server" ID="up">
<ProgressTemplate>
<div>
<img src="images/whx0000.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</div><asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel runat="server" ID="upanel">
<ContentTemplate>
<asp:GridView runat="server" ID="gvData" >
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnGo" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
<div>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
用户名:<asp:TextBox ID="txtName" runat="server" />
<br />密码:<asp:TextBox ID="txtPwd" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
<asp:Button ID="btnAdd" runat="server" Text="添加" onclick="btnAdd_Click" />
</form>
</body>
</html>
后台代码:
View Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace TestPage
{
public partial class ajax1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind();
}
}
public void Bind()
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123456;database=taocihome;");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Users", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvData.DataSource = ds.Tables[0];
gvData.DataBind();
}
protected void btnGo_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123456;database=taocihome;");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Users where id>"+txtKey.Text , con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvData.DataSource = ds.Tables[0];
gvData.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123456;database=taocihome;");
SqlCommand cmd = new SqlCommand("insert into Users(uName,uPwd) values('" + txtName.Text + "','" + txtPwd.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Bind();
}
}
}
这里使用的是两个updatepanel,都可以对gvData进行无刷新更新。