2016-12-15 70 views
0

好的我正在嘗試使用javascript填充一個asp GridView。 webService調用已成功,我可以正常檢索數據。但是,當我想在我的網格中顯示它時,什麼都不會發生。在JavaScript中填充GridView不起作用,沒有錯誤捕獲

JavaScript函數:

function SearchUsers() { 
    AutoComplete.ShowLoginUserOnSearch("PL", "pi", "", "", "", 0, 20, onGetLoginUserSuccess); 
    } 

function onGetLoginUserSuccess(result) { 
    alert(result[1].ProviderName); 
    $("#grdvUserInfo").append("<tr><td>" + result[1].UserName + 
      "</td><td>" + result[1].ProviderName + "</td></tr>"); 
    alert(result[1].UserName); 
} 

Asp.net:

<asp:GridView ID="grdvUserInfo" AllowPaging="True" PagerSettings- 
Mode="NextPreviousFirstLast" PageSize="20" runat="server" 
AutoGenerateColumns="False" Width="700px" SkinID="grdMySite" 
DataSourceID="ObjectDataSource1"> 

</asp:GridView> 

兩個警報都充滿了正確的數據,可能是什麼問題呢?

回答

0

HTML

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">script> 
<script type="text/javascript"> 
    $(function() { 
     $.ajax({ 
      type: "POST", 
      url: "Default.aspx/GetCustomers", 
      data: '{}', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: OnSuccess, 
      failure: function (response) { 
       alert(response.d); 
      }, 
      error: function (response) { 
       alert(response.d); 
      } 
     }); 
    }); 

    function OnSuccess(response) { 
     var xmlDoc = $.parseXML(response.d); 
     var xml = $(xmlDoc); 
     var customers = xml.find("Table"); 
     var row = $("[id*=gvCustomers] tr:last-child").clone(true); 
     $("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove(); 
     $.each(customers, function() { 
      var customer = $(this); 
      $("td", row).eq(0).html($(this).find("CustomerID").text()); 
      $("td", row).eq(1).html($(this).find("ContactName").text()); 
      $("td", row).eq(2).html($(this).find("City").text()); 
      $("[id*=gvCustomers]").append(row); 
      row = $("[id*=gvCustomers] tr:last-child").clone(true); 
     }); 
    } 
<script> 

Web方法

[WebMethod] 
public static string GetCustomers() 
{ 
    string query = "SELECT top 10 CustomerID, ContactName, City FROM Customers"; 
    SqlCommand cmd = new SqlCommand(query); 
    return GetData(cmd).GetXml(); 
} 
private static DataSet GetData(SqlCommand cmd) 
{ 
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(strConnString)) 
    { 
     using (SqlDataAdapter sda = new SqlDataAdapter()) 
     { 
      cmd.Connection = con; 
      sda.SelectCommand = cmd; 
      using (DataSet ds = new DataSet()) 
      { 
       sda.Fill(ds); 
       return ds; 

      } 
     } 
    } 
} 

enter image description here http://www.aspsnippets.com/Articles/Populate-ASPNet-GridView-by-binding-DataSet-Client-Side-using-jQuery-AJAX.aspx

0

Asp.net

<asp:GridView ID="grdvUserInfo" AllowPaging="True" PagerSettings- 
    Mode="NextPreviousFirstLast" PageSize="20" runat="server" 
    AutoGenerateColumns="False" Width="700px" SkinID="grdMySite" 
    DataSourceID="ObjectDataSource1"> 

    </asp:GridView> 

功能

function onGetLoginUserSuccess(result) { 
     alert(result[1].ProviderName); 
     $("#grdvUserInfo").append("<asp:TemplateField><ItemTemplate>" + result[1].UserName + 
       "</ItemTemplate></asp:TemplateField><asp:TemplateField><ItemTemplate>" + result[1].ProviderName + 
"</ItemTemplate></asp:TemplateField>"); 
     alert(result[1].UserName); 
    } 
+0

表永遠不能被嵌套在GridView中! –