AJAX(prototype.js)+WEBSERVICES,实现二级联动的问题

weiqiaoxyz 2009-08-26 03:29:23
一直取不到二级列表的值 ALERT以后。弹出的是空值。不知道问题到底出在哪里了。
请求帮助。。

DEFAULT3.ASPX
<form id="form1" runat="server">
<div id="provinceDiv">
<select id="province" name="province" onchange="SelectCapital()">
<option>省份</option><% = a %>
</select>
</div>

<div id="capitalDiv"><select id="capital" name="capital">
<option>地市级</option></select>
</div>
</form>


DEFAULT3.ASPX.CS
public string a = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlConnection conn = new SqlConnection(DB.strConn);
conn.Open();
SqlDataAdapter sdr = new SqlDataAdapter();
sdr.SelectCommand = new SqlCommand("select * from sheng where city='0'", conn);
DataSet ds = new DataSet();
sdr.Fill(ds, "emp");
string b = "";
for (int i = 0; i < ds.Tables["emp"].Rows.Count; i++)
{
b += "<option>" + ds.Tables["emp"].Rows[i]["sheng"].ToString() +"</option>";
}
a = b;
sdr.Dispose();
ds.Dispose();
conn.Close();
conn.Dispose();
}
}


SELECT.JS
var path='services/system.asmx/';
function SelectCapital()
{
var b=
{
method:'get',
parameters:'name='+encodeURIComponent($F('province')),

onComplete:function(a)
{
$('capitalDiv').update(TransXml(a.responseXML));
alert(TransXml(a.responseXML));
}
};
new Ajax.Request(path+'SelectCapital',b)
}


system.CS
[WebMethod]
public string SelectCapital()
{
string name = HttpContext.Current.Request["name"].ToString();
SqlConnection conn = new SqlConnection(DB.strConn);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from sheng where (city<>'0' and sheng='" + name + "')", conn);
SqlDataReader sdr = cmd.ExecuteReader();
string b = "";
while (sdr.Read())
{
b += "<option>" + sdr["city"].ToString() + "</option>";
}
return b;
}


...全文
179 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiqiaoxyz 2009-08-26
  • 打赏
  • 举报
回复
具体哪些操作我也不是太清楚。框架是我一同学写的。我拿过来学习的。

先谢了。结帖先
阿非 2009-08-26
  • 打赏
  • 举报
回复
哦? TransXml 这个函数 你做了哪些操作
weiqiaoxyz 2009-08-26
  • 打赏
  • 举报
回复
非常感谢。问题弄明白了
就是没有加配置文件。。
mxc1225 2009-08-26
  • 打赏
  • 举报
回复
网上很多的!?
阿非 2009-08-26
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 weiqiaoxyz 的回复:]
让webservice 支持get 访问 记得在配置文件中 配置

那段配置文件放在哪个中间?
[/Quote]

在<system.web>中间

你可以在 aspx 所在的 目录 添加一个 局部的 配置文件 这样不会 影响 全局的 配置文件
阿非 2009-08-26
  • 打赏
  • 举报
回复
你首先 确保 webservice 能被调用

可以通过断点调试 来确认
weiqiaoxyz 2009-08-26
  • 打赏
  • 举报
回复
让webservice 支持get 访问 记得在配置文件中 配置

那段配置文件放在哪个中间?
阿非 2009-08-26
  • 打赏
  • 举报
回复

$('capitalDiv').update(TransXml(a.responseXML));
alert(TransXml(a.responseXML));



是这两句

只是数据库不同,其他 都差不多,你对照着 改下 就可以的
weiqiaoxyz 2009-08-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sandy945 的回复:]
数据库脚本可以看这里
http://blog.csdn.net/Sandy945/archive/2009/07/08/4332068.aspx
这不贴了。

HTML code<htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title><scriptsrc='prototype.js'></script><script>var path='SelectWebService.asmx/';function SelectCapital()
{var b=
{
method:'get',
parameters:'id='+encodeURIComponent($F('province')),

onComplete:function(a)
{var htmlStart='<select id="capital" name="capital"><option>城市</option>';var htmlEnd='</select>';var value=a.responseText;var optionStr=value.substring(value.indexOf("||start||")+9,value.lastIndexOf("||end||"));
optionStr=optionStr.replace(new RegExp("<","gm"),'<').replace(new RegExp(">","gm"),'>');
$('capitalDiv').update(htmlStart+optionStr+htmlEnd);
}
};new Ajax.Request(path+'SelectCapital',b)
}</script></head><body><formid="form1" runat="server"><divid="provinceDiv"><selectid="province" name="province" onchange="SelectCapital()"><option>省份</option><%= options%></select></div><divid="capitalDiv"><selectid="capital" name="capital"><option>城市</option></select></div></form></body></html>
[/Quote]


能不能看一下我发出来的那部分。问题到底出在哪里
因为我是刚学AJAX和WEBSERVICES,所以想知道错在哪里。
阿非 2009-08-26
  • 打赏
  • 举报
回复
WebService 名 叫 SelectWebService 与 aspx 在同一目录
阿非 2009-08-26
  • 打赏
  • 举报
回复

[WebMethod]
public string SelectCapital()
{
StringBuilder sb = new StringBuilder("||start||");
string id = HttpContext.Current.Request.QueryString["id"];
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=root;database=Test"))
{

SqlDataAdapter da = new SqlDataAdapter("select * from city where pId=@PID", conn);
da.SelectCommand.Parameters.Add(new SqlParameter("@PID", id));
DataSet ds = new DataSet();
da.Fill(ds, "city");

if (ds.Tables.Contains("city"))
{
for (int i = 0; i < ds.Tables["city"].Rows.Count; i++)
{
sb.AppendFormat("<option>{0}</option>", ds.Tables["city"].Rows[i]["cName"]);
}
}
}
sb.Append("||end||");
return sb.ToString();
}

让webservice 支持get 访问 记得在配置文件中 配置

<webServices>
<protocols>
<add name="HttpGet"/>

</protocols>
</webServices>
hrbwgs1111 2009-08-26
  • 打赏
  • 举报
回复
阿非 2009-08-26
  • 打赏
  • 举报
回复

protected string options;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection conn = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Test"))
{

SqlDataAdapter sdr = new SqlDataAdapter("select * from province ",conn);
DataSet ds = new DataSet();
sdr.Fill(ds, "province");
StringBuilder sb = new StringBuilder();
if (ds.Tables.Contains("province"))
{
for (int i = 0; i < ds.Tables["province"].Rows.Count; i++)
{
sb.AppendFormat("<option value='{0}'>{1}</option>", ds.Tables["province"].Rows[i]["pId"], ds.Tables["province"].Rows[i]["pName"]);
}
}
options = sb.ToString();
}
}

}

hrbwgs1111 2009-08-26
  • 打赏
  • 举报
回复
阿非 2009-08-26
  • 打赏
  • 举报
回复
数据库脚本可以看这里
http://blog.csdn.net/Sandy945/archive/2009/07/08/4332068.aspx
这不贴了。



<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script src='prototype.js'></script>
<script>
var path='SelectWebService.asmx/';
function SelectCapital()
{
var b=
{
method:'get',
parameters:'id='+encodeURIComponent($F('province')),

onComplete:function(a)
{
var htmlStart='<select id="capital" name="capital"><option>城市</option>';
var htmlEnd='</select>';
var value=a.responseText;
var optionStr=value.substring(value.indexOf("||start||")+9,value.lastIndexOf("||end||"));
optionStr=optionStr.replace(new RegExp("<","gm"),'<').replace(new RegExp(">","gm"),'>');
$('capitalDiv').update(htmlStart+optionStr+htmlEnd);
}
};
new Ajax.Request(path+'SelectCapital',b)
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div id="provinceDiv">
<select id="province" name="province" onchange="SelectCapital()">
<option>省份</option><% = options %>
</select>
</div>

<div id="capitalDiv"><select id="capital" name="capital">
<option>城市</option></select>
</div>

</form>
</body>
</html>

bl_song 2009-08-26
  • 打赏
  • 举报
回复
BAD GateWAY
weiqiaoxyz 2009-08-26
  • 打赏
  • 举报
回复
大家都很忙啊。。。
weiqiaoxyz 2009-08-26
  • 打赏
  • 举报
回复
MARK
yrj_star 2009-08-26
  • 打赏
  • 举报
回复
不错,留个名
weiqiaoxyz 2009-08-26
  • 打赏
  • 举报
回复
没人吗?
加载更多回复(1)

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧