日期:2014-05-17 浏览次数:20424 次
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MengXianHui.mdb;"; System.Data.DataTable CreateDataSourceByXianhuiMeng() { System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(ConnectionString); System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("select * from Table1", cn); cn.Open(); System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader(); System.Data.DataTable dt = new System.Data.DataTable(); dt.Load(dr); cn.Close(); return dt; } protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["id"] != null) { //插入内容 String textbox = Request.Form["TextBox1"]; String select = Request.Form["DropDownList1"]; System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(ConnectionString); System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("insert into Table1 ([Title],[Content]) Values(@Title,@Content)", cn); cn.Open(); cmd.Parameters.AddWithValue("@Title", textbox); cmd.Parameters.AddWithValue("@Content", select); cmd.ExecuteNonQuery(); cn.Dispose(); Response.ClearContent(); GridView GridView1 = new GridView(); GridView1.DataSource = CreateDataSourceByXianhuiMeng(); GridView1.DataBind(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView1.RenderControl(htw); Response.Write("这里查询数据,输出结果就可以了。结果:" + sb.ToString()); Response.End(); } else { //测试数据 DropDownList1.DataSource = new String[] { "A", "B", "C", "D" }; DropDownList1.DataBind(); } } /// 添加这个override void VerifyRenderingInServerForm(Control control),是为了避免出现 /// 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。 /// 的异常 public override void VerifyRenderingInServerForm(Control control) { } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>使用 AJAX,局部刷新 GridView 进行数据绑定的简单实现</title> <script type="text/javascript"> function GetData() { data = "TextBox1=" + encodeURIComponent(document.getElementById('<%=TextBox1.ClientID %>').value) + "&DropDownList1=" + document.getElementById('<%=DropDownList1.Cli