24 个解决方案
#1
在CS里 读取数据库 Response.write JAVASCRIPT
#2
up……
#3
前台代码:
后台代码:
只写了一列值,根据你自己的情况改一下吧
var myArray = [<%=GetArray() %>];
后台代码:
protected string GetArray()
{
SqlConnection con = new SqlConnection("...");
SqlCommand cmd = new SqlCommand("select ThirdComponentID, ThirdComponentName from dic_ThirdComponent", con);
SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
System.Text.StringBuilder text = new System.Text.StringBuilder();
if (ds.Tables.Count != 0)
{
string format = "'{0}',";
foreach (DataRow dr in ds.Tables[0].Rows)
{
text.AppendFormat(format, dr["ThirdComponentName"]);
}
}
return text.ToString().TrimEnd(',');
}
只写了一列值,根据你自己的情况改一下吧
#4
读取数据库数据,写入一个数组的javascript 代码
要用到ajaxpro吧
ajaxpro使用(
http://blog.sina.com.cn/s/blog_50761d850100b3m3.html
AjaxPro 返回 DataSet/DataTable/DataView 如何呈现?
[code=JSscript]
// js
<script type="text/javascript">
function GetProductData()
{
var cb = function(res) {
if(res.error) return alert("发生错误\n" + res.error.Message);
//debugger;
//alert(res);
var ds = res.value;
var tbl = ds.Tables[0];
var tblHtml = "<table border=1>";
// 表头
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<th>" + tbl.Columns[j].Name + "</th>";
}
tblHtml += "</tr>";
// 数据
for(var i = 0; i < tbl.Rows.length; i++) {
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<td>" + tbl.Rows[i][tbl.Columns[j].Name] + "</td>";
}
tblHtml += "</tr>";
}
tblHtml += "</table>";
var divPro = document.getElementById("divPro");
divPro.innerHTML = tblHtml;
}
AjaxProSample.GetProductSet(cb);
}
</script>
[/CODE]
要用到ajaxpro吧
ajaxpro使用(
http://blog.sina.com.cn/s/blog_50761d850100b3m3.html
AjaxPro 返回 DataSet/DataTable/DataView 如何呈现?
[code=JSscript]
// js
<script type="text/javascript">
function GetProductData()
{
var cb = function(res) {
if(res.error) return alert("发生错误\n" + res.error.Message);
//debugger;
//alert(res);
var ds = res.value;
var tbl = ds.Tables[0];
var tblHtml = "<table border=1>";
// 表头
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<th>" + tbl.Columns[j].Name + "</th>";
}
tblHtml += "</tr>";
// 数据
for(var i = 0; i < tbl.Rows.length; i++) {
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<td>" + tbl.Rows[i][tbl.Columns[j].Name] + "</td>";
}
tblHtml += "</tr>";
}
tblHtml += "</table>";
var divPro = document.getElementById("divPro");
divPro.innerHTML = tblHtml;
}
AjaxProSample.GetProductSet(cb);
}
</script>
[/CODE]
// .aspx.cs
[AjaxPro.AjaxNamespace("AjaxProSample")]
public partial class AjaxPro_ReturnDataSet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxPro_ReturnDataSet));
}
[AjaxPro.AjaxMethod]
public static DataSet GetProductSet()
{
return CreateSampleProductSet();
}
sample data#region sample data
static DataSet CreateSampleProductSet()
{
DataSet ds = new DataSet();
ds.Tables.Add(CreateSampleProductData());
return ds;
}
static DataTable CreateSampleProductData()
{
DataTable tbl = new DataTable("Products");
tbl.Columns.Add("ProductID", typeof(int));
tbl.Columns.Add("ProductName", typeof(string));
tbl.Columns.Add("UnitPrice", typeof(decimal));
tbl.Columns.Add("CategoryID", typeof(int));
tbl.Rows.Add(1, "Chai", 18, 1);
tbl.Rows.Add(2, "Chang", 19, 1);
tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2);
tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2);
tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
tbl.Rows.Add(48, "Chocolade", 12.75, 3);
tbl.Rows.Add(49, "Maxilaku", 20, 3);
return tbl;
}
#endregion
}
AjaxPro 支持直接返回 DataTable 和 DataView ,客户端读取方式同 DataSet
var tbl = res.value; // 直接访问 DataTable
需要注意的是,返回 DataView,实际上是返回 DataView 关联的 DataTable 。
#5
我的问题是这样的,我找了一个联动无刷新的例子,它在javascript 中是这样给数组付的值,
var nation = new Array();
nation[ 0 ] = new Array(' 0 ',' 1 ','阿尔巴尼亚');
nation[ 1 ] = new Array(' 0 ',' 2 ','阿尔及利亚');
nation[ 2 ] = new Array(' 0 ',' 3 ','阿富汗');
nation[ 3 ] = new Array(' 0 ',' 4 ','阿根廷');
nation[ 4 ] = new Array(' 0 ',' 5 ','阿拉伯联合酋长国');
nation[ 5 ] = new Array(' 0 ',' 6 ','阿鲁巴');
nation[ 6 ] = new Array(' 0 ',' 7 ','阿曼');
nation[ 7 ] = new Array(' 0 ',' 8 ','阿塞拜疆');
nation[ 8 ] = new Array(' 0 ',' 9 ','埃及');
nation[ 9 ] = new Array(' 0 ',' 10 ','埃塞俄比亚');
nation[ 10 ] = new Array(' 0 ',' 11 ','爱尔兰');
nation[ 11 ] = new Array(' 0 ',' 12 ','爱沙尼亚');
nation[ 12 ] = new Array(' 0 ',' 13 ','安道尔');
nation[ 13 ] = new Array(' 0 ',' 14 ','安哥拉');
nation[ 14 ] = new Array(' 0 ',' 15 ','安圭拉岛');
nation[ 15 ] = new Array(' 0 ',' 16 ','安提瓜和巴布达');
nation[ 16 ] = new Array(' 0 ',' 17 ','奥地利');
nation[ 17 ] = new Array(' 0 ',' 18 ','澳大利亚');
nation[ 18 ] = new Array(' 0 ',' 19 ','澳门特别行政区');
nation[ 19 ] = new Array(' 0 ',' 20 ','巴巴多斯');
nation[ 20 ] = new Array(' 0 ',' 21 ','巴布亚新几内亚');
nation[ 21 ] = new Array(' 0 ',' 22 ','巴哈马');
nation[ 22 ] = new Array(' 0 ',' 23 ','巴基斯坦');
nation[ 23 ] = new Array(' 0 ',' 24 ','巴拉圭');
nation[ 24 ] = new Array(' 0 ',' 25 ','巴林');
nation[ 25 ] = new Array(' 0 ',' 26 ','*');
nation[ 26 ] = new Array(' 0 ',' 27 ','巴西');
nation[ 27 ] = new Array(' 0 ',' 28 ','白俄罗斯');
nation[ 28 ] = new Array(' 0 ',' 29 ','百慕大群岛');
nation[ 29 ] = new Array(' 0 ',' 30 ','保加利亚');
nation[ 30 ] = new Array(' 0 ',' 31 ','北马里亚纳群岛');
nation[ 31 ] = new Array(' 0 ',' 32 ','贝宁');
nation[ 32 ] = new Array(' 0 ',' 33 ','比利时');
nation[ 33 ] = new Array(' 0 ',' 34 ','冰岛');
nation[ 34 ] = new Array(' 0 ',' 35 ','波多黎各');
nation[ 35 ] = new Array(' 0 ',' 36 ','波兰');
nation[ 36 ] = new Array(' 0 ',' 37 ','波斯尼亚和黑塞哥维那');
nation[ 37 ] = new Array(' 0 ',' 38 ','玻利维亚');
nation[ 38 ] = new Array(' 0 ',' 39 ','伯利兹');
nation[ 39 ] = new Array(' 0 ',' 40 ','博茨瓦纳');
nation[ 40 ] = new Array(' 0 ',' 41 ','不丹');
他是这样给数组付的值,我是希望从数据库中读出来,然后这样付到数组中,应该怎么写?
var nation = new Array();
nation[ 0 ] = new Array(' 0 ',' 1 ','阿尔巴尼亚');
nation[ 1 ] = new Array(' 0 ',' 2 ','阿尔及利亚');
nation[ 2 ] = new Array(' 0 ',' 3 ','阿富汗');
nation[ 3 ] = new Array(' 0 ',' 4 ','阿根廷');
nation[ 4 ] = new Array(' 0 ',' 5 ','阿拉伯联合酋长国');
nation[ 5 ] = new Array(' 0 ',' 6 ','阿鲁巴');
nation[ 6 ] = new Array(' 0 ',' 7 ','阿曼');
nation[ 7 ] = new Array(' 0 ',' 8 ','阿塞拜疆');
nation[ 8 ] = new Array(' 0 ',' 9 ','埃及');
nation[ 9 ] = new Array(' 0 ',' 10 ','埃塞俄比亚');
nation[ 10 ] = new Array(' 0 ',' 11 ','爱尔兰');
nation[ 11 ] = new Array(' 0 ',' 12 ','爱沙尼亚');
nation[ 12 ] = new Array(' 0 ',' 13 ','安道尔');
nation[ 13 ] = new Array(' 0 ',' 14 ','安哥拉');
nation[ 14 ] = new Array(' 0 ',' 15 ','安圭拉岛');
nation[ 15 ] = new Array(' 0 ',' 16 ','安提瓜和巴布达');
nation[ 16 ] = new Array(' 0 ',' 17 ','奥地利');
nation[ 17 ] = new Array(' 0 ',' 18 ','澳大利亚');
nation[ 18 ] = new Array(' 0 ',' 19 ','澳门特别行政区');
nation[ 19 ] = new Array(' 0 ',' 20 ','巴巴多斯');
nation[ 20 ] = new Array(' 0 ',' 21 ','巴布亚新几内亚');
nation[ 21 ] = new Array(' 0 ',' 22 ','巴哈马');
nation[ 22 ] = new Array(' 0 ',' 23 ','巴基斯坦');
nation[ 23 ] = new Array(' 0 ',' 24 ','巴拉圭');
nation[ 24 ] = new Array(' 0 ',' 25 ','巴林');
nation[ 25 ] = new Array(' 0 ',' 26 ','*');
nation[ 26 ] = new Array(' 0 ',' 27 ','巴西');
nation[ 27 ] = new Array(' 0 ',' 28 ','白俄罗斯');
nation[ 28 ] = new Array(' 0 ',' 29 ','百慕大群岛');
nation[ 29 ] = new Array(' 0 ',' 30 ','保加利亚');
nation[ 30 ] = new Array(' 0 ',' 31 ','北马里亚纳群岛');
nation[ 31 ] = new Array(' 0 ',' 32 ','贝宁');
nation[ 32 ] = new Array(' 0 ',' 33 ','比利时');
nation[ 33 ] = new Array(' 0 ',' 34 ','冰岛');
nation[ 34 ] = new Array(' 0 ',' 35 ','波多黎各');
nation[ 35 ] = new Array(' 0 ',' 36 ','波兰');
nation[ 36 ] = new Array(' 0 ',' 37 ','波斯尼亚和黑塞哥维那');
nation[ 37 ] = new Array(' 0 ',' 38 ','玻利维亚');
nation[ 38 ] = new Array(' 0 ',' 39 ','伯利兹');
nation[ 39 ] = new Array(' 0 ',' 40 ','博茨瓦纳');
nation[ 40 ] = new Array(' 0 ',' 41 ','不丹');
他是这样给数组付的值,我是希望从数据库中读出来,然后这样付到数组中,应该怎么写?
#6
SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);说找不到类型或命名空间~~应该引用什么命名空间??
#7
var nation = new Array();
<%=GetArray() %>
protected string GetArray()
{
SqlConnection con = new SqlConnection("server=192.168.15.144;uid=sa;pwd=sa;database=dic_ThirdComponent");
SqlCommand cmd = new SqlCommand("select ThirdComponentID, ThirdComponentName from dic_ThirdComponent", con);
SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
System.Text.StringBuilder text = new System.Text.StringBuilder();
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
text.AppendFormat("nation[ {0} ] = new Array(' 0 ',' {1} ','{2}');", i.ToString(), ds.Tables[0].Rows[i]["ThirdComponentID"].ToString(), ds.Tables[0].Rows[i]["ThirdComponentName"].ToString());
}
}
return text.ToString();
}
#8
using System.Data.SqlClient;
#9
System.Data.SqlClient,因为我的数据库是 SQL Server,如果不是 SQL Server,就不是这个命名空间,程序也要做一些相应的调整。
还有按照你的格式填数组的话,只要把变量 format 改为:
string format = "['{0}','{1}','{2}'],";
然后在
text.AppendFormat(format, dr["ThirdComponentName"], dr["XXX"], dr["XXX"]);
填入相应的值就可以了。
还有按照你的格式填数组的话,只要把变量 format 改为:
string format = "['{0}','{1}','{2}'],";
然后在
text.AppendFormat(format, dr["ThirdComponentName"], dr["XXX"], dr["XXX"]);
填入相应的值就可以了。
#10
using System.Data.SqlClient;
我添加了这个命名空间,但是为何还是说 SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);找不到类型或命名空间呢
我添加了这个命名空间,但是为何还是说 SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);找不到类型或命名空间呢
#11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
这是我添加的,为何还是说 SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);找不到类型或命名空间呢,用的VS2008
#12
SqlDataAdapter adp = new SqlDataAdapter(cmd);
#13
SqlDataAdapter adp = new SqlDataAdapter(cmd);
把那个SqlClient去掉
#14
请帮我看下,到底哪里问题!谢谢了!!必结贴给分!!
Default.aspx
Default.aspx.cs
test.js
为何运行后,下拉菜单还是没有绑定?
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="test.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select name="RootID" id="RootID">
</select>
</div>
</form>
<script type="text/javascript">
tf = document.composeform;
CreateRootID(tf, 1);
</script>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected string GetArray()
{
SqlConnection con = new SqlConnection("server=192.168.15.144;uid=sa;pwd=sa;database=dic_ThirdComponent");
SqlCommand cmd = new SqlCommand("select ThirdComponentID, ThirdComponentName from dic_ThirdComponent", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
System.Text.StringBuilder text = new System.Text.StringBuilder();
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
text.AppendFormat("nation[ {0} ] = new Array('{1} ','{2}');", i.ToString(), ds.Tables[0].Rows[i]["ThirdComponentID"].ToString(), ds.Tables[0].Rows[i]["ThirdComponentName"].ToString());
}
}
return text.ToString();
}
}
test.js
var myArray = [<%=GetArray() %>];
function CreateRootID(targForm,sType) {
var targ = targForm.RootID;
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
为何运行后,下拉菜单还是没有绑定?
#15
要我做的话在.cs文件中读取,在js中读出<%=test%>
#16
改为<form id="composeform" method="post" runat="server">,还是不行~~
#17
设断点,查出return text.ToString();是正确的,为何就是邦定不了?
nation[ 0 ] = new Array('-1 ','请选择');nation[ 1 ] = new Array('1 ','避雷器');nation[ 2 ] = new Array('2 ','多功能仪表');nation[ 3 ] = new Array('3 ','数显表');nation[ 4 ] = new Array('4 ','指针表');nation[ 5 ] = new Array('5 ','双电源转换开关');nation[ 6 ] = new Array('6 ','电容电抗器');nation[ 7 ] = new Array('7 ','灯钮');nation[ 8 ] = new Array('8 ','电动机保护监控装置');nation[ 9 ] = new Array('9 ','电流互感器');
nation[ 0 ] = new Array('-1 ','请选择');nation[ 1 ] = new Array('1 ','避雷器');nation[ 2 ] = new Array('2 ','多功能仪表');nation[ 3 ] = new Array('3 ','数显表');nation[ 4 ] = new Array('4 ','指针表');nation[ 5 ] = new Array('5 ','双电源转换开关');nation[ 6 ] = new Array('6 ','电容电抗器');nation[ 7 ] = new Array('7 ','灯钮');nation[ 8 ] = new Array('8 ','电动机保护监控装置');nation[ 9 ] = new Array('9 ','电流互感器');
#18
JS文件改为这样也是不行~~
var nation = new Array();
<%=GetArray() %>
function CreateRootID(targForm,sType) {
var targ = targForm.RootID;
for (i = 0; i < nation.length; i++) {
targ.options[i] = new Option(nation[i][1], nation[i][0]);
}
}
#19
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var nation = new Array();
<%=GetArray() %>
function CreateRootID(id) {
var targ = document.getElementByID(id);
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select name="RootID" id="RootID">
</select>
</div>
</form>
<script type="text/javascript">
//tf = document.composeform;
CreateRootID('RootID');
</script>
</body>
</html>
#20
var targ = document.getElementByID(id);
说对象不支持此属性和方法,还有一个想问
<script type="text/javascript">
var nation = new Array();
<%=GetArray() %>
function CreateRootID(id) {
var targ = document.getElementByID(id);
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
</script>
必须写在Default.aspx 中吗?不可以单建一个js文件吗?因为绑定后还有下拉菜单联动的代码,如果都写在Default.aspx里会很大
说对象不支持此属性和方法,还有一个想问
<script type="text/javascript">
var nation = new Array();
<%=GetArray() %>
function CreateRootID(id) {
var targ = document.getElementByID(id);
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
</script>
必须写在Default.aspx 中吗?不可以单建一个js文件吗?因为绑定后还有下拉菜单联动的代码,如果都写在Default.aspx里会很大
#21
var targ = document.getElementById(id);
#22
<%=GetArray() %>
必须写在.aspx页中,其余可以写在单独的js文件中
#23
这次行了!非常感谢!再问最后一个问题
可不可以把单建一个js文件吗?因为绑定后还有下拉菜单联动的代码,如果都写在Default.aspx里会很大
#24
<%=GetArray() %>
必须写在.aspx页中,并且放在<script></script>之间。其余可以写在单独的js文件中
#1
在CS里 读取数据库 Response.write JAVASCRIPT
#2
up……
#3
前台代码:
后台代码:
只写了一列值,根据你自己的情况改一下吧
var myArray = [<%=GetArray() %>];
后台代码:
protected string GetArray()
{
SqlConnection con = new SqlConnection("...");
SqlCommand cmd = new SqlCommand("select ThirdComponentID, ThirdComponentName from dic_ThirdComponent", con);
SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
System.Text.StringBuilder text = new System.Text.StringBuilder();
if (ds.Tables.Count != 0)
{
string format = "'{0}',";
foreach (DataRow dr in ds.Tables[0].Rows)
{
text.AppendFormat(format, dr["ThirdComponentName"]);
}
}
return text.ToString().TrimEnd(',');
}
只写了一列值,根据你自己的情况改一下吧
#4
读取数据库数据,写入一个数组的javascript 代码
要用到ajaxpro吧
ajaxpro使用(
http://blog.sina.com.cn/s/blog_50761d850100b3m3.html
AjaxPro 返回 DataSet/DataTable/DataView 如何呈现?
[code=JSscript]
// js
<script type="text/javascript">
function GetProductData()
{
var cb = function(res) {
if(res.error) return alert("发生错误\n" + res.error.Message);
//debugger;
//alert(res);
var ds = res.value;
var tbl = ds.Tables[0];
var tblHtml = "<table border=1>";
// 表头
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<th>" + tbl.Columns[j].Name + "</th>";
}
tblHtml += "</tr>";
// 数据
for(var i = 0; i < tbl.Rows.length; i++) {
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<td>" + tbl.Rows[i][tbl.Columns[j].Name] + "</td>";
}
tblHtml += "</tr>";
}
tblHtml += "</table>";
var divPro = document.getElementById("divPro");
divPro.innerHTML = tblHtml;
}
AjaxProSample.GetProductSet(cb);
}
</script>
[/CODE]
要用到ajaxpro吧
ajaxpro使用(
http://blog.sina.com.cn/s/blog_50761d850100b3m3.html
AjaxPro 返回 DataSet/DataTable/DataView 如何呈现?
[code=JSscript]
// js
<script type="text/javascript">
function GetProductData()
{
var cb = function(res) {
if(res.error) return alert("发生错误\n" + res.error.Message);
//debugger;
//alert(res);
var ds = res.value;
var tbl = ds.Tables[0];
var tblHtml = "<table border=1>";
// 表头
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<th>" + tbl.Columns[j].Name + "</th>";
}
tblHtml += "</tr>";
// 数据
for(var i = 0; i < tbl.Rows.length; i++) {
tblHtml += "<tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += "<td>" + tbl.Rows[i][tbl.Columns[j].Name] + "</td>";
}
tblHtml += "</tr>";
}
tblHtml += "</table>";
var divPro = document.getElementById("divPro");
divPro.innerHTML = tblHtml;
}
AjaxProSample.GetProductSet(cb);
}
</script>
[/CODE]
// .aspx.cs
[AjaxPro.AjaxNamespace("AjaxProSample")]
public partial class AjaxPro_ReturnDataSet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxPro_ReturnDataSet));
}
[AjaxPro.AjaxMethod]
public static DataSet GetProductSet()
{
return CreateSampleProductSet();
}
sample data#region sample data
static DataSet CreateSampleProductSet()
{
DataSet ds = new DataSet();
ds.Tables.Add(CreateSampleProductData());
return ds;
}
static DataTable CreateSampleProductData()
{
DataTable tbl = new DataTable("Products");
tbl.Columns.Add("ProductID", typeof(int));
tbl.Columns.Add("ProductName", typeof(string));
tbl.Columns.Add("UnitPrice", typeof(decimal));
tbl.Columns.Add("CategoryID", typeof(int));
tbl.Rows.Add(1, "Chai", 18, 1);
tbl.Rows.Add(2, "Chang", 19, 1);
tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2);
tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2);
tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
tbl.Rows.Add(48, "Chocolade", 12.75, 3);
tbl.Rows.Add(49, "Maxilaku", 20, 3);
return tbl;
}
#endregion
}
AjaxPro 支持直接返回 DataTable 和 DataView ,客户端读取方式同 DataSet
var tbl = res.value; // 直接访问 DataTable
需要注意的是,返回 DataView,实际上是返回 DataView 关联的 DataTable 。
#5
我的问题是这样的,我找了一个联动无刷新的例子,它在javascript 中是这样给数组付的值,
var nation = new Array();
nation[ 0 ] = new Array(' 0 ',' 1 ','阿尔巴尼亚');
nation[ 1 ] = new Array(' 0 ',' 2 ','阿尔及利亚');
nation[ 2 ] = new Array(' 0 ',' 3 ','阿富汗');
nation[ 3 ] = new Array(' 0 ',' 4 ','阿根廷');
nation[ 4 ] = new Array(' 0 ',' 5 ','阿拉伯联合酋长国');
nation[ 5 ] = new Array(' 0 ',' 6 ','阿鲁巴');
nation[ 6 ] = new Array(' 0 ',' 7 ','阿曼');
nation[ 7 ] = new Array(' 0 ',' 8 ','阿塞拜疆');
nation[ 8 ] = new Array(' 0 ',' 9 ','埃及');
nation[ 9 ] = new Array(' 0 ',' 10 ','埃塞俄比亚');
nation[ 10 ] = new Array(' 0 ',' 11 ','爱尔兰');
nation[ 11 ] = new Array(' 0 ',' 12 ','爱沙尼亚');
nation[ 12 ] = new Array(' 0 ',' 13 ','安道尔');
nation[ 13 ] = new Array(' 0 ',' 14 ','安哥拉');
nation[ 14 ] = new Array(' 0 ',' 15 ','安圭拉岛');
nation[ 15 ] = new Array(' 0 ',' 16 ','安提瓜和巴布达');
nation[ 16 ] = new Array(' 0 ',' 17 ','奥地利');
nation[ 17 ] = new Array(' 0 ',' 18 ','澳大利亚');
nation[ 18 ] = new Array(' 0 ',' 19 ','澳门特别行政区');
nation[ 19 ] = new Array(' 0 ',' 20 ','巴巴多斯');
nation[ 20 ] = new Array(' 0 ',' 21 ','巴布亚新几内亚');
nation[ 21 ] = new Array(' 0 ',' 22 ','巴哈马');
nation[ 22 ] = new Array(' 0 ',' 23 ','巴基斯坦');
nation[ 23 ] = new Array(' 0 ',' 24 ','巴拉圭');
nation[ 24 ] = new Array(' 0 ',' 25 ','巴林');
nation[ 25 ] = new Array(' 0 ',' 26 ','*');
nation[ 26 ] = new Array(' 0 ',' 27 ','巴西');
nation[ 27 ] = new Array(' 0 ',' 28 ','白俄罗斯');
nation[ 28 ] = new Array(' 0 ',' 29 ','百慕大群岛');
nation[ 29 ] = new Array(' 0 ',' 30 ','保加利亚');
nation[ 30 ] = new Array(' 0 ',' 31 ','北马里亚纳群岛');
nation[ 31 ] = new Array(' 0 ',' 32 ','贝宁');
nation[ 32 ] = new Array(' 0 ',' 33 ','比利时');
nation[ 33 ] = new Array(' 0 ',' 34 ','冰岛');
nation[ 34 ] = new Array(' 0 ',' 35 ','波多黎各');
nation[ 35 ] = new Array(' 0 ',' 36 ','波兰');
nation[ 36 ] = new Array(' 0 ',' 37 ','波斯尼亚和黑塞哥维那');
nation[ 37 ] = new Array(' 0 ',' 38 ','玻利维亚');
nation[ 38 ] = new Array(' 0 ',' 39 ','伯利兹');
nation[ 39 ] = new Array(' 0 ',' 40 ','博茨瓦纳');
nation[ 40 ] = new Array(' 0 ',' 41 ','不丹');
他是这样给数组付的值,我是希望从数据库中读出来,然后这样付到数组中,应该怎么写?
var nation = new Array();
nation[ 0 ] = new Array(' 0 ',' 1 ','阿尔巴尼亚');
nation[ 1 ] = new Array(' 0 ',' 2 ','阿尔及利亚');
nation[ 2 ] = new Array(' 0 ',' 3 ','阿富汗');
nation[ 3 ] = new Array(' 0 ',' 4 ','阿根廷');
nation[ 4 ] = new Array(' 0 ',' 5 ','阿拉伯联合酋长国');
nation[ 5 ] = new Array(' 0 ',' 6 ','阿鲁巴');
nation[ 6 ] = new Array(' 0 ',' 7 ','阿曼');
nation[ 7 ] = new Array(' 0 ',' 8 ','阿塞拜疆');
nation[ 8 ] = new Array(' 0 ',' 9 ','埃及');
nation[ 9 ] = new Array(' 0 ',' 10 ','埃塞俄比亚');
nation[ 10 ] = new Array(' 0 ',' 11 ','爱尔兰');
nation[ 11 ] = new Array(' 0 ',' 12 ','爱沙尼亚');
nation[ 12 ] = new Array(' 0 ',' 13 ','安道尔');
nation[ 13 ] = new Array(' 0 ',' 14 ','安哥拉');
nation[ 14 ] = new Array(' 0 ',' 15 ','安圭拉岛');
nation[ 15 ] = new Array(' 0 ',' 16 ','安提瓜和巴布达');
nation[ 16 ] = new Array(' 0 ',' 17 ','奥地利');
nation[ 17 ] = new Array(' 0 ',' 18 ','澳大利亚');
nation[ 18 ] = new Array(' 0 ',' 19 ','澳门特别行政区');
nation[ 19 ] = new Array(' 0 ',' 20 ','巴巴多斯');
nation[ 20 ] = new Array(' 0 ',' 21 ','巴布亚新几内亚');
nation[ 21 ] = new Array(' 0 ',' 22 ','巴哈马');
nation[ 22 ] = new Array(' 0 ',' 23 ','巴基斯坦');
nation[ 23 ] = new Array(' 0 ',' 24 ','巴拉圭');
nation[ 24 ] = new Array(' 0 ',' 25 ','巴林');
nation[ 25 ] = new Array(' 0 ',' 26 ','*');
nation[ 26 ] = new Array(' 0 ',' 27 ','巴西');
nation[ 27 ] = new Array(' 0 ',' 28 ','白俄罗斯');
nation[ 28 ] = new Array(' 0 ',' 29 ','百慕大群岛');
nation[ 29 ] = new Array(' 0 ',' 30 ','保加利亚');
nation[ 30 ] = new Array(' 0 ',' 31 ','北马里亚纳群岛');
nation[ 31 ] = new Array(' 0 ',' 32 ','贝宁');
nation[ 32 ] = new Array(' 0 ',' 33 ','比利时');
nation[ 33 ] = new Array(' 0 ',' 34 ','冰岛');
nation[ 34 ] = new Array(' 0 ',' 35 ','波多黎各');
nation[ 35 ] = new Array(' 0 ',' 36 ','波兰');
nation[ 36 ] = new Array(' 0 ',' 37 ','波斯尼亚和黑塞哥维那');
nation[ 37 ] = new Array(' 0 ',' 38 ','玻利维亚');
nation[ 38 ] = new Array(' 0 ',' 39 ','伯利兹');
nation[ 39 ] = new Array(' 0 ',' 40 ','博茨瓦纳');
nation[ 40 ] = new Array(' 0 ',' 41 ','不丹');
他是这样给数组付的值,我是希望从数据库中读出来,然后这样付到数组中,应该怎么写?
#6
SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);说找不到类型或命名空间~~应该引用什么命名空间??
#7
var nation = new Array();
<%=GetArray() %>
protected string GetArray()
{
SqlConnection con = new SqlConnection("server=192.168.15.144;uid=sa;pwd=sa;database=dic_ThirdComponent");
SqlCommand cmd = new SqlCommand("select ThirdComponentID, ThirdComponentName from dic_ThirdComponent", con);
SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
System.Text.StringBuilder text = new System.Text.StringBuilder();
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
text.AppendFormat("nation[ {0} ] = new Array(' 0 ',' {1} ','{2}');", i.ToString(), ds.Tables[0].Rows[i]["ThirdComponentID"].ToString(), ds.Tables[0].Rows[i]["ThirdComponentName"].ToString());
}
}
return text.ToString();
}
#8
using System.Data.SqlClient;
#9
System.Data.SqlClient,因为我的数据库是 SQL Server,如果不是 SQL Server,就不是这个命名空间,程序也要做一些相应的调整。
还有按照你的格式填数组的话,只要把变量 format 改为:
string format = "['{0}','{1}','{2}'],";
然后在
text.AppendFormat(format, dr["ThirdComponentName"], dr["XXX"], dr["XXX"]);
填入相应的值就可以了。
还有按照你的格式填数组的话,只要把变量 format 改为:
string format = "['{0}','{1}','{2}'],";
然后在
text.AppendFormat(format, dr["ThirdComponentName"], dr["XXX"], dr["XXX"]);
填入相应的值就可以了。
#10
using System.Data.SqlClient;
我添加了这个命名空间,但是为何还是说 SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);找不到类型或命名空间呢
我添加了这个命名空间,但是为何还是说 SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);找不到类型或命名空间呢
#11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
这是我添加的,为何还是说 SqlDataAdapter adp = new SqlClient.SqlDataAdapter(cmd);找不到类型或命名空间呢,用的VS2008
#12
SqlDataAdapter adp = new SqlDataAdapter(cmd);
#13
SqlDataAdapter adp = new SqlDataAdapter(cmd);
把那个SqlClient去掉
#14
请帮我看下,到底哪里问题!谢谢了!!必结贴给分!!
Default.aspx
Default.aspx.cs
test.js
为何运行后,下拉菜单还是没有绑定?
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="test.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select name="RootID" id="RootID">
</select>
</div>
</form>
<script type="text/javascript">
tf = document.composeform;
CreateRootID(tf, 1);
</script>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected string GetArray()
{
SqlConnection con = new SqlConnection("server=192.168.15.144;uid=sa;pwd=sa;database=dic_ThirdComponent");
SqlCommand cmd = new SqlCommand("select ThirdComponentID, ThirdComponentName from dic_ThirdComponent", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
System.Text.StringBuilder text = new System.Text.StringBuilder();
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
text.AppendFormat("nation[ {0} ] = new Array('{1} ','{2}');", i.ToString(), ds.Tables[0].Rows[i]["ThirdComponentID"].ToString(), ds.Tables[0].Rows[i]["ThirdComponentName"].ToString());
}
}
return text.ToString();
}
}
test.js
var myArray = [<%=GetArray() %>];
function CreateRootID(targForm,sType) {
var targ = targForm.RootID;
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
为何运行后,下拉菜单还是没有绑定?
#15
要我做的话在.cs文件中读取,在js中读出<%=test%>
#16
改为<form id="composeform" method="post" runat="server">,还是不行~~
#17
设断点,查出return text.ToString();是正确的,为何就是邦定不了?
nation[ 0 ] = new Array('-1 ','请选择');nation[ 1 ] = new Array('1 ','避雷器');nation[ 2 ] = new Array('2 ','多功能仪表');nation[ 3 ] = new Array('3 ','数显表');nation[ 4 ] = new Array('4 ','指针表');nation[ 5 ] = new Array('5 ','双电源转换开关');nation[ 6 ] = new Array('6 ','电容电抗器');nation[ 7 ] = new Array('7 ','灯钮');nation[ 8 ] = new Array('8 ','电动机保护监控装置');nation[ 9 ] = new Array('9 ','电流互感器');
nation[ 0 ] = new Array('-1 ','请选择');nation[ 1 ] = new Array('1 ','避雷器');nation[ 2 ] = new Array('2 ','多功能仪表');nation[ 3 ] = new Array('3 ','数显表');nation[ 4 ] = new Array('4 ','指针表');nation[ 5 ] = new Array('5 ','双电源转换开关');nation[ 6 ] = new Array('6 ','电容电抗器');nation[ 7 ] = new Array('7 ','灯钮');nation[ 8 ] = new Array('8 ','电动机保护监控装置');nation[ 9 ] = new Array('9 ','电流互感器');
#18
JS文件改为这样也是不行~~
var nation = new Array();
<%=GetArray() %>
function CreateRootID(targForm,sType) {
var targ = targForm.RootID;
for (i = 0; i < nation.length; i++) {
targ.options[i] = new Option(nation[i][1], nation[i][0]);
}
}
#19
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var nation = new Array();
<%=GetArray() %>
function CreateRootID(id) {
var targ = document.getElementByID(id);
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select name="RootID" id="RootID">
</select>
</div>
</form>
<script type="text/javascript">
//tf = document.composeform;
CreateRootID('RootID');
</script>
</body>
</html>
#20
var targ = document.getElementByID(id);
说对象不支持此属性和方法,还有一个想问
<script type="text/javascript">
var nation = new Array();
<%=GetArray() %>
function CreateRootID(id) {
var targ = document.getElementByID(id);
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
</script>
必须写在Default.aspx 中吗?不可以单建一个js文件吗?因为绑定后还有下拉菜单联动的代码,如果都写在Default.aspx里会很大
说对象不支持此属性和方法,还有一个想问
<script type="text/javascript">
var nation = new Array();
<%=GetArray() %>
function CreateRootID(id) {
var targ = document.getElementByID(id);
for (i = 0; i < myArray.length; i++) {
targ.options[i] = new Option(myArray[i][1], myArray[i][0]);
}
}
</script>
必须写在Default.aspx 中吗?不可以单建一个js文件吗?因为绑定后还有下拉菜单联动的代码,如果都写在Default.aspx里会很大
#21
var targ = document.getElementById(id);
#22
<%=GetArray() %>
必须写在.aspx页中,其余可以写在单独的js文件中
#23
这次行了!非常感谢!再问最后一个问题
可不可以把单建一个js文件吗?因为绑定后还有下拉菜单联动的代码,如果都写在Default.aspx里会很大
#24
<%=GetArray() %>
必须写在.aspx页中,并且放在<script></script>之间。其余可以写在单独的js文件中