一个用webservice behavior实现的三级联动下拉列表框
2024-07-21 02:21:35
供稿:网友
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。我做了一个三级联动的下拉列表框,后台用webservice,前台用webservice behavior与后台通讯。请高手们多提改进意见。
server端:(service1.asmx.cs)
using system;
using system.text;
using system.configuration;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.diagnostics;
using system.web;
using system.web.services;
namespace webservice1
{
/// <summary>
/// summary description for service1.
/// </summary>
public class service1 : system.web.services.webservice
{
sqlconnection con;
public service1()
{
//codegen: this call is required by the asp.net web services designer
initializecomponent();
}
#region component designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
string dsn = configurationsettings.appsettings["yitong"];
con=new sqlconnection(dsn);
}
#endregion
/// <summary>
/// clean up any resources being used.
/// </summary>
protected override void dispose( bool disposing )
{
}
[webmethod]
public string fenlei()
{
string str="select distinct substring(code,1,3) as fenlei from jinan ";
sqlcommand cmd=new sqlcommand(str,con);
cmd.connection.open();
sqldatareader dr=cmd.executereader();
string s="请选择:";
while(dr.read())
{
s += ","+dr["fenlei"].tostring();
}
return s;
}
[webmethod]
public string leixing(string q)
{
string str="select distinct type from jinan where code like '"+q+"%'";
sqlcommand cmd=new sqlcommand(str,con);
cmd.connection.open();
sqldatareader dr=cmd.executereader();
string s="请选择:";
while(dr.read())
{
s += ","+dr["type"].tostring();
}
return s;
}
[webmethod]
public string haoduan(string q)
{
string str="select distinct substring(code,4,4) as haoduan from jinan where type='"+q+"'";
sqlcommand cmd=new sqlcommand(str,con);
cmd.connection.open();
sqldatareader dr=cmd.executereader();
string s="请选择:";
while(dr.read())
{
s += ","+dr["type"].tostring();
}
return s;
}
client端(htmlpage1.htm)
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<meta name="generator" content="microsoft visual studio 7.0">
<title></title>
<script id="clienteventhandlersjs" language="javascript">
<!--
var icallid;
var icallid1;
var icallid2;
function window_onload() {
service.useservice("/service1.asmx?wsdl","myselect");
icallid=service.myselect.callservice(fenlei,"fenlei");
}
function fenlei(result)
{
var m=result.value;
var a=m.split(",")
for(var i=0;i<a.length;i++)
{
window.s1.options[i]=new option(a[i],a[i]);
}
}
function s1_onchange() {
var x=s1.value;
icallid1=service.myselect.callservice(leixing,"leixing",x);
}
function s2_onchange() {
var x=s2.value;
icallid2=service.myselect.callservice(haoduan,"haoduan",x);
}
function leixing(result){
var m=result.value;
var a=m.split(",")
window.s2.length=0;
for(var i=0;i<a.length;i++)
{
window.s2.options[i]=new option(a[i],a[i]);
}
}
function haoduan(result){
var m=result.value;
var a=m.split(",")
window.s3.length=0;
for(var i=0;i<a.length;i++)
{
window.s3.options[i]=new option(a[i],a[i]);
}
}
//-->
</script>
</head>
<body onload="return window_onload()">
<div id="service" style="behavior:url(webservice.htc)">
</div>
<select id="s1" onchange="return s1_onchange()"> </select><select id="s2" onchange="return s2_onchange()"></select>
<select id="s3"></select>
</html>