vb.net 从 jQuery AJAX 调用动态填充 ASP.NET Gridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15788827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically populate ASP.NET Gridview from jQuery AJAX call
提问by tymeJV
Error:
错误:
cannot refer to an instance member of a class within a shared method
I'm working on a web application that pulls data from a MSSQL server on the backened. Calling my web method from ajax isn't the issue, it's displaying the data once it has been received.
我正在开发一个 Web 应用程序,该应用程序从支持的 MSSQL 服务器中提取数据。从 ajax 调用我的 web 方法不是问题,它会在收到数据后显示。
I have the following gridview on my report page:
我的报告页面上有以下 gridview:
<asp:GridView ID="gridReport" runat="server"></asp:GridView>
The web method that is pulling the data from the DB is called DBManager.asmx, it is declared as follows (this is called via ajax each time a drop-down is changed):
从数据库中提取数据的 web 方法称为 DBManager.asmx,它声明如下(每次下拉列表更改时都会通过 ajax 调用):
<WebMethod()> _
Public Function GetSpecificReport(ByVal strFilter As String) As String()
(it is NOT the code-behind file for the report page) The data being returned from the DB is currently in a DataSet (variable name: ds).
(它不是报告页面的代码隐藏文件)从数据库返回的数据当前位于数据集中(变量名称:ds)。
Is there any way to assign the DataSourcefor that GridView to the DataSet pulled from the .asmx file, or a nice workaround that I could try? I've tried creating shared methods in the code-behind report page with no luck, as well as putting the GetSpecificReport method inside the code-behind.
有什么方法可以将DataSourceGridView分配给从 .asmx 文件中提取的数据集,或者我可以尝试的一个很好的解决方法?我尝试在代码隐藏报告页面中创建共享方法但没有成功,并将 GetSpecificReport 方法放在代码隐藏中。
Thanks for the help, let me know if I should give more info or post more code.
感谢您的帮助,如果我应该提供更多信息或发布更多代码,请告诉我。
回答by Yablargo
I would offer 3 suggestions:
我会提供3个建议:
First and foremost, using something like telerik grid that allows you to return JSON from your webmethod and bind directly to that json: http://www.telerik.com/products/aspnet-ajax/grid.aspx
首先,使用类似 Telerik grid 的东西,它允许您从 webmethod 返回 JSON 并直接绑定到该 json:http: //www.telerik.com/products/aspnet-ajax/grid.aspx
That said, I whipped up a small example where you can render a gridview in your webmethod and re-emit the HTML.
也就是说,我创建了一个小例子,你可以在你的 webmethod 中渲染一个 gridview 并重新发送 HTML。
You can also just draw your table in HTML using RenderControl:
您也可以使用 RenderControl 在 HTML 中绘制表格:
Here is a simplified example that re-draws the control using jquery.ajax without any updatepanel or etc. Updatepanel probably works better though!
这是一个简化的示例,它使用 jquery.ajax 重新绘制控件而没有任何 updatepanel 等。不过 Updatepanel 可能效果更好!
ASPX:
ASPX:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="TymeJVTest._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function blag() {
var gridID = "<%= GridView1.ClientID %>";
$.ajax({
type: "POST",
data: {},
contentType: "application/json;charset=utf-8",
dataType: "json",
async: true,
url: "Default.aspx/GetSomeData",
success: function (data) {
//not sure why this is data.d , but what the hey
var theHtml = data.d;
$("#" + gridID).html(theHtml);
},
failure: function () {
alert("Sorry,there is a error!");
}
});
}
</script>
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<a href="#"onclick="blag();" >Test</a>
</asp:Content>
CS:
CS:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int[] theObject = {1, 2, 3, 4};
GridView1.DataSource = theObject;
GridView1.DataBind();
}
[WebMethod]
public static String GetSomeData()
{
GridView gv = new GridView();
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
int[] someNewData = {10, 11, 12, 13, 14};
gv.DataSource = someNewData;
gv.DataBind();
gv.RenderControl(htmlWriter);
return stringWriter.ToString();
}
}
This will create a replacement gridview on the server, render it to html, then transmit it back.
这将在服务器上创建一个替换 gridview,将其呈现为 html,然后将其传回。
You could also use something like AngularJS and just keep a piece of JSON that your server updates, and use this to bind to a grid in real-time with angular.
你也可以使用像 AngularJS 这样的东西,只保留一段你的服务器更新的 JSON,并使用它来实时绑定到带有 angular 的网格。
回答by Constanta
I think you can achieve what you want without too much changes using an updatepanel.
我认为您可以使用更新面板在不进行太多更改的情况下实现您想要的。
http://msdn.microsoft.com/en-gb/library/bb399001.aspx
http://msdn.microsoft.com/en-gb/library/bb399001.aspx
Basically populate your grid on page load or whatever suits you then do refreshes using updatepanel.
基本上在页面加载时填充您的网格或任何适合您的网格,然后使用 updatepanel 进行刷新。

