一個用webservice behavior實現的三級聯動下拉式清單方塊

來源:互聯網
上載者:User
web|下拉|下拉式清單 我做了一個三級聯動的下拉式清單方塊,後台用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>




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。