在JavaScript中从DataTable中检索数据 [英] Retrieve data from a DataTable in JavaScript
问题描述
我有一个aspx页面,我从Oracle DB中获取代码中的数据并将其放入DataTable中。
在aspx中,我打算使用用于从.cs文件中检索DataTable的JavaScript。这样做有什么好的参考?在此先感谢。
以下是我的例子来解释你的情景: -
1)包括以下代码中提到的以下参考文献: -
使用系统;
使用 System.Collections.Generic;
使用 System.Data;
使用 System.Web.Script.Serialization;
使用 System.Web.Services;
2)创建了一个新的测试数据表,用于在页面加载时获取数据,如下所示: -
protected < span class =code-keyword> void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
_dt.Columns.Add( ID跨度>);
_dt.Columns.Add( 名称);
DataRow dr1 = _dt.NewRow();
dr1 [ ID] = 1 跨度>;
dr1 [ 名称] = Smruti;
_dt.Rows.Add(dr1);
DataRow dr2 = _dt.NewRow();
dr2 [ ID] = 2 跨度>;
dr2 [ 名称] = Ranjan;
_dt.Rows.Add(dr2);
}
}
3)创建一个页面方法,以JSON格式的字符串形式从此DataTable返回数据下面: -
[WebMethod]
public < span class =code-keyword> static string GetData()
{
List< Dictionary< string,object>> ; dicRows = new List< Dictionary< string,object>>();
Dictionary< string,object> dicRow = null ;
foreach (DataRow dr in _dt.Rows)
{
dicRow = new Dictionary< string,object>();
foreach (DataColumn col in _dt.Columns)
{
dicRow.Add(col.ColumnName,dr [col]);
}
dicRows.Add(dicRow);
}
JavaScriptSerializer objSerializer = new JavaScriptSerializer();
string sJSON = objSerializer.Serialize(dicRows);
return sJSON;
}
4)现在在aspx页面中添加对JS文件的引用&代码通过头部内的jQuery Ajax调用'GetData'方法如下: -
< head runat = 服务器 >
< title > < / title >
< script src = jquery-2.0.3.js > < / script >
< script type = text / javascript >
< blockquote>(function(){
(#btnTest)。click(function(){
GetTestData();
});
});
函数GetTestData(){
I have an aspx page and i am fetching data in code behind from Oracle DB and getting it in a DataTable.
In the aspx, I plan to use JavaScript to retrieve the DataTable from the .cs file. Any good reference for doing this? Thanks in advance.
Here is my example to explain your scenario :-
1) Include the following references as mentioned below in the code behind :-
using System; using System.Collections.Generic; using System.Data; using System.Web.Script.Serialization; using System.Web.Services;
2) Created a new test data table for the fetching of data on page load as below :-
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { _dt.Columns.Add("ID"); _dt.Columns.Add("Name"); DataRow dr1 = _dt.NewRow(); dr1["ID"] = 1; dr1["Name"] = "Smruti"; _dt.Rows.Add(dr1); DataRow dr2 = _dt.NewRow(); dr2["ID"] = 2; dr2["Name"] = "Ranjan"; _dt.Rows.Add(dr2); } }
3) Create a page method to return data from this DataTable in JSON formatted string as below :-
[WebMethod] public static string GetData() { List<Dictionary<string, object>> dicRows = new List<Dictionary<string, object>>(); Dictionary<string, object> dicRow = null; foreach (DataRow dr in _dt.Rows) { dicRow = new Dictionary<string, object>(); foreach (DataColumn col in _dt.Columns) { dicRow.Add(col.ColumnName, dr[col]); } dicRows.Add(dicRow); } JavaScriptSerializer objSerializer = new JavaScriptSerializer(); string sJSON = objSerializer.Serialize(dicRows); return sJSON; }
4) Now in aspx page add a reference to JS file & code to call the 'GetData' method through jQuery Ajax inside the head as below :-
<head runat="server"> <title></title> <script src="jquery-2.0.3.js"></script> <script type="text/javascript">
(function () {
("#btnTest").click(function () { GetTestData(); }); }); function GetTestData() {
这篇关于在JavaScript中从DataTable中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!