AJAX刷新Gridview

InBlock.gif using System;
InBlock.gif using System.Configuration;
InBlock.gif using System.Data;
InBlock.gif using System.Linq;
InBlock.gif using System.Web;
InBlock.gif using System.Web.Security;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI.WebControls.WebParts;
InBlock.gif using System.Xml.Linq;
InBlock.gif
InBlock.gif public partial class _Default : System.Web.UI.Page    
InBlock.gif{
InBlock.gif        
InBlock.gif
InBlock.gif        System.Data.DataView CreateDataSourceByXianhuiMeng()
InBlock.gif        {
InBlock.gif                System.Data.DataTable dt = new System.Data.DataTable();
InBlock.gif                System.Data.DataRow dr;
InBlock.gif                dt.Columns.Add( new System.Data.DataColumn( "id", typeof(System.Int32)));
InBlock.gif                dt.Columns.Add( new System.Data.DataColumn( "学生姓名", typeof(System.String)));
InBlock.gif                dt.Columns.Add( new System.Data.DataColumn( "语文", typeof(System.Decimal)));
InBlock.gif                dt.Columns.Add( new System.Data.DataColumn( "数学", typeof(System.Decimal)));
InBlock.gif                dt.Columns.Add( new System.Data.DataColumn( "英语", typeof(System.Decimal)));
InBlock.gif                dt.Columns.Add( new System.Data.DataColumn( "计算机", typeof(System.Decimal)));
InBlock.gif
InBlock.gif                 for ( int i = 1; i < 30; i++)
InBlock.gif                {
InBlock.gif                        System.Random rd = new System.Random(Environment.TickCount * i); ;
InBlock.gif                        dr = dt.NewRow();
InBlock.gif                        dr[0] = i;
InBlock.gif                        dr[1] = "【孟子E章】" + i.ToString();
InBlock.gif                        dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
InBlock.gif                        dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
InBlock.gif                        dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
InBlock.gif                        dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
InBlock.gif                        dt.Rows.Add(dr);
InBlock.gif                }
InBlock.gif                System.Data.DataView dv = new System.Data.DataView(dt);
InBlock.gif                 return dv;
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void Page_Load( object sender, EventArgs e)
InBlock.gif        {
InBlock.gif                 if (Request.QueryString[ "id"] != null)
InBlock.gif                {
InBlock.gif                        Response.ClearContent();
InBlock.gif                        GridView1.DataSource = CreateDataSourceByXianhuiMeng();
InBlock.gif                        GridView1.DataBind();
InBlock.gif                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
InBlock.gif                        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
InBlock.gif                        HtmlTextWriter htw = new HtmlTextWriter(sw);
InBlock.gif                        Literal header = new Literal();
InBlock.gif                        header.Text = "<h2>项目 " + Request.QueryString[ "id"] + "</h2>";
InBlock.gif                        Header.Controls.Add(header);
InBlock.gif                        Header.RenderControl(htw);
InBlock.gif                        GridView1.RenderControl(htw);
InBlock.gif                        Response.Write( "这里查询数据,输出结果就可以了。结果:" + sb.ToString());
InBlock.gif                        Response.End();
InBlock.gif                }
InBlock.gif        }
InBlock.gif
InBlock.gif
InBlock.gif         public override void VerifyRenderingInServerForm(Control control)
InBlock.gif        { }
InBlock.gif
InBlock.gif
InBlock.gif}
< %@ Page Language ="C#" AutoEventWireup ="true"     CodeFile ="Default.aspx.cs" Inherits ="_Default" % >

< !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 >AJAX练习 </title>
         < script type ="text/javascript" >
        function GetData(p) {
            document.getElementById("d").innerHTML = "正在读取数据……";
            h = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP");
            h.open("GET", ' < %=Request.FilePath % >?id=' + p.value, true);
            h.onreadystatechange = function() {
                if (h.readyState == 4) {
                    if (h.status >= 200 && h.status < 300) {
                        document.getElementById("d").innerHTML = h.responseText;
                    }
                    else {
                        document.getElementById("d").innerHTML = " < h2 >数据操作错误: </h2>" + h.responseText;
                    }
                }
            }
            h.send(null);
        }
        alert("这个提示,只出现在第一次打开页面。");
     </script>

</head>
< body >
         < form id ="form1" runat ="server" >
     < asp:GridView ID ="GridView1" runat ="server" GridLines ="Horizontal" Width ="100%" >
             < HeaderStyle BackColor ="#00CCFF" />
             < AlternatingRowStyle BackColor ="#CCCCCC" />
         </asp:GridView>
     < asp:Panel ID ="Header" runat ="server" > </asp:Panel>
     < select onchange ="GetData(this)" >
         < option value ="1" >项目一 </option>
         < option value ="2" >项目二 </option>
     </select>
     </form>
     < div id ="d" > </div>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值