【问题标题】:Populating Gridview Using Ajax使用 Ajax 填充 Gridview
【发布时间】:2013-12-09 07:11:56
【问题描述】:

我有两个网格视图。单击一个网格的一行时,我必须填充另一个网格视图。所以 onClientclick javascript 函数我调用了 ajax,它返回用于填充另一个网格的数据表。现在我被困在如何使用 javascript 绑定网格视图。

这里是代码

<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!');
            }
        });
}

【问题讨论】:

    标签: javascript jquery asp.net .net ajax


    【解决方案1】:

    您需要像这样在成功部分将数据附加到gridview

    如果您有 ID 为“gridview2”的gridview

    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

    【讨论】:

      【解决方案2】:

      在这里 Scott Guthrei 博客“提示/技巧:用于非 UpdatePanel 场景的 ASP.NET AJAX 的酷 UI 模板技术”,这是您正在寻找的确切示例。

      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 替换该部分。

      【讨论】:

      • 我在 javascript 中有从 ajax 返回的数据表,我想将它分配给网格视图。这对我没有帮助,而且我的两个网格都在更新面板中
      • GridView 是服务器端控件,您不能使用 DataSource 和 DataBind() 在客户端直接绑定它,您必须在服务器端执行此操作,生成的 html 您需要传递给客户端并放入容器
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多