2013-12-09 101 views
0

我有兩個gridview。點擊一個網格的一行,我必須填充另一個gridview。所以onClientclick的JavaScript函數,我叫ajax返回數據表填充另一個網格。現在我堅持如何使用JavaScript綁定網格視圖。使用Ajax填充Gridview

這裏是代碼

<asp:gridview id="gridview1"> .....</asp:gridview> 
<asp:gridview id="gridview2"> .....</asp:gridview> 

代碼隱藏

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
      LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0]; 

     db.OnClientClick = "FunPopulateSecondGrid(" + CType(CType(e.Row.DataItem, System.Data.DataRowView).Row, Label).text + ");" 
      } 
} 

的JavaScript

function FunPopulateSecondGrid(productid) 
{ 
    $.ajax({ 
      url : '...', 
      data : { getExEmpList: true, SelectedShop : selectedShop, ajaxCall : true }, 
      method : 'GET', 
      dataType : 'json', 
      contentType: "application/json; charset=utf-8", 
      success : function(data) { 
// i am stuck here how to bind it 
//gridview2.datasource= data 
//gridview2.databind()    
      }, 
      error : function(xhr, status) { 
       alert('Sorry, there was a problem while placing your ajax request. Contact Admin!'); 
      } 
     }); 
} 

回答

4

您需要將數據追加到GridView控件在這樣

成功部分如果你有GridView控件ID爲「gridview2」

function FunPopulateSecondGrid(productid) 
    { 
     $.ajax({ 
       url : '...', 
       data : { getExEmpList: true, SelectedShop : selectedShop, ajaxCall : true }, 
       method : 'GET', 
       dataType : 'json', 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { //you need to append the data to gridview 
      for (var i = 0; i < data.d.length; i++) { 
        $("#gridview2").append("<tr><td>" + data.d[i].ProductName + 
               "</td><td>" + data.d[i].Price + "</td></tr>"); 
       }, 
       error : function(xhr, status) { 
        alert('Sorry, there was a problem while placing your ajax request. Contact Admin!'); 
       } 
      }); 
    } 

請在這裏找到完整的解決方案:Bind GridView with Jquery

+0

謝謝,我會嘗試,並會讓你今天知道EOD。 –

1

這裏斯科特Guthrei博客「技巧/訣竅:酷UI模板技術與ASP.NET AJAX使用對於非UpdatePanel場景「,這是您正在查看的確切示例。

http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx

注:在這裏,他用asp.net Ajax和腳本管理器,但可以更換使用jQuery AJAX的部分。

+0

我有數據表在從AJAX返回的JavaScript,我想分配給網格視圖。這沒有幫助我,除了我的兩個網格是在更新面板 –

+0

GridView是服務器端控制,你不能直接綁定它在客戶端使用DataSource和DataBind(),你必須在服務器端這樣做,併產生的HTML,你需要通過給客戶,並在容器 –