以XMLHttpRequest方式填入DataGrid

本文介绍了一种使用AJAX技术更新DataGrid的方法。通过JavaScript处理客户端请求,并利用C#在服务器端响应,实现DataGrid的数据加载。该方案适用于需要异步刷新表格数据的应用场景。

 古老的AJAX做法...

 DataGrid只能看,不能用...

 JavaScript Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
var client=null
if (window.XMLHttpRequest){ 
    client = 
new XMLHttpRequest(); 

else if (window.ActiveXObject){ 
    client = 
new ActiveXObject("Microsoft.XMLHTTP"); 
}

function getDataGrid(app_date) 

    
try 
    { 
        
var url="Grid.aspx";
        url=url+
'?app_data='+document.getElementById('app_data').value; 
        client.open(
"GET", url); 
        client.onreadystatechange = callBack; 
        client.send(); 
    } 
    
catch(ex) 
    { 
        alert(ex.message); 
    } 

 
function callBack(response) 

    
try 
    { 
        
if(client.readyState == 4 && client.status == 200
        { 
            document.getElementById(
"GridSection").innerHTML=client.responseText;
        } 
    } 
    
catch(ex) 
    { 
        alert(ex.message); 
    } 

 

 

 C# Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
        protected void Page_Load(object sender, System.EventArgs e)
        {
            
if(!this.IsPostBack)
            {
                
if (Request["app_data"] != null)
                {
                    
this.SetBind((string)Request["app_data"]);
                }
            }
        }
        
        
//取得主DataGrid畫面資料
        private void SetBind(string app_data)
        {
            
//資料
            DataTable Store = Operator.getDataGridDataSource(app_date).Tables[0];
            DataView StoreView = 
new DataView(Store);
            GridCS.DataSource = StoreView;
            GridCS.DataBind();

            
//將Grid寫到網頁
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter hw = 
new HtmlTextWriter(sw);
            Response.ClearHeaders();
            Response.Clear();
            Response.Charset = 
"UTF-8";
            GridCS.RenderControl(hw);
            Response.Write(sw.ToString());
            Response.End();
        }

转载于:https://www.cnblogs.com/jimmych/archive/2013/06/06/3121548.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值