.Net下三级联动不刷新实现全代码(读取后台数据库 ok)

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>Three Join Union Form</title>
    <script language="javascript" type="text/javascript">
    
    // Function getcity
    function getcity(objcity,objprov,objplace,defcity,defprov,defplace)
    {
    //debugger;
        while(objcity.options != null && objcity.options.length>0)
        {
            objcity.options.remove(0);
        }
        var oOptionFirst= document.createElement("OPTION");
        objcity.options.add(oOptionFirst);
        oOptionFirst.value = '';
        oOptionFirst.innerText = 'Please Select';
        if(defcity == '')
        {
            oOptionFirst.selected=true;
        }
        for(i=0;i<city.length;i++)
        {
            var oOption= document.createElement("OPTION");
            objcity.options.add(oOption);
            oOption.value = city[i];
            oOption.innerText = city[i];
            if(city[i] == defcity)
            {
                oOption.selected = true;
            }
        }
        chgprov(objcity,objprov,objplace,defprov,defplace);
    }
    // Function chgprov
    function chgprov(objcity,objprov,objplace,defprov,defplace)
    {
    //debugger;
        idx=objcity.selectedIndex-1;
        while(objprov.options != null && objprov.options.length>0)
        {
            objprov.options.remove(0);
        }
        var oOptionFirst= document.createElement("OPTION");
        objprov.options.add(oOptionFirst);
        oOptionFirst.value = '';
        oOptionFirst.innerText = 'Please Select';
        if(defprov == '')
        {
            oOptionFirst.selected=true;
        }
        if(idx >= 0)
        {
            for(i=0;i<eval('prov'+idx).length;i++)
            {
                var oOption= document.createElement("OPTION");
                objprov.options.add(oOption);
                oOption.value = eval('prov'+idx+'[i]');
                oOption.innerText = eval('prov'+idx+'[i]');
                if(eval('prov'+idx+'[i]') == defprov)
                {
                    oOption.selected = true;
                }
            }
        }
        chgplace(objcity,objprov,objplace,defplace);
    }
    // Function chgplace
    function chgplace(objcity,objprov,objplace,defplace)
    {
    //debugger;
        pidx = objcity.selectedIndex-1;
        idx=objprov.selectedIndex-1;
        while(objplace.options != null && objplace.options.length>0)
        {
            objplace.options.remove(0);
        }
        var oOptionFirst= document.createElement("OPTION");
        objplace.options.add(oOptionFirst);
        oOptionFirst.value = '';
        oOptionFirst.innerText = 'Please Select';
        if(defplace == '')
        {
            oOptionFirst.selected=true;
        }
        if(idx >= 0)
        {
            for(i=0;i<eval('place'+pidx+'_'+idx).length;i++)
            {
                var oOption= document.createElement("OPTION");
                objplace.options.add(oOption);
                oOption.value = eval('place'+pidx+'_'+idx+'[i]');
                oOption.innerText = eval('place'+pidx+'_'+idx+'[i]');
                if(eval('place'+pidx+'_'+idx+'[i]') == defplace)
                {
                    oOption.selected = true;
                }
            }
        }
    }
    </script>
</head>
<body>
    <form id="form1" method="post" runat="server">
        <table>
            <tr>
                <td width="90" height="24" align="right">
                    <font color="666666">WeiZhi</font></td>
                <td width="90" align="left">
                    <select name="city" class="p3" size="1" onchange="chgprov(this,form1.province,form1.place,'','')">
                    </select>
                </td>
                <td width="90" align="left">
                    <select name="province" class="p3" size="1" onchange="chgplace(form1.city,this,form1.place,'')">
                    </select>
                </td>
                <td width="119">
                    <select name="place" class="p3">
                    </select>
                    <script language="javascript" type="text/javascript">
                            //getcity(form1.city,form1.province,form1.place,'','','')
                            getcity(document.getElementById("city"),document.getElementById("province"),document.getElementById("place"),'','','')
                    </script>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
********************************************************************************
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender , EventArgs e)
    {
        Response.Write(getJSContent());
    }
    public string getJSContent()
    {
        string strconn = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection conn = new SqlConnection(strconn);
        conn.Open();
        string strCity = " var city = new Array(";
        string strProv = "";
        string strPlace = "";
        string strsql = "select * from PolicyHolderFamily where city is not null";
        // Fill All PolicyHolderFamily Data to ds
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(strsql , conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dt = ds.Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            strCity += "'" + dt.Rows[i]["City"].ToString().Trim() + "',";
            string sqli = "select * from PolicyHolderFamily where RecordID = "
                        + dt.Rows[i][0].ToString().Trim();
            SqlDataAdapter dai = new SqlDataAdapter(sqli , conn);
            DataSet dsi = new DataSet();
            dai.Fill(dsi);
            DataTable dti = dsi.Tables[0];
            strProv += " var prov" + i + " = new Array(";
            for (int j = 0; j < dti.Rows.Count; j++)
            {
                strProv += "'" + dti.Rows[j]["State"].ToString().Trim() + "',";
                string sqlj = "select * from PolicyHolderFamily where RecordID = " +
                            dti.Rows[j][0].ToString().Trim();
                SqlDataAdapter daj = new SqlDataAdapter(sqlj , conn);
                DataSet dsj = new DataSet();
                daj.Fill(dsj);
                DataTable dtj = dsj.Tables[0];
                strPlace += " var place" + i + "_" + j + " = new Array(";
                for (int m = 0; m < dtj.Rows.Count; m++)
                {
                    strPlace += "'" + dtj.Rows[m][1].ToString().Trim() + "',";
                }
                if (strPlace.EndsWith(","))
                {
                    strPlace = strPlace.Substring(0 , strPlace.Length - 1);
                }
                strPlace += ");"n"r";
            }
            if (strProv.EndsWith(","))
            {
                strProv = strProv.Substring(0 , strProv.Length - 1);
            }
            strProv += ");"n"r";
        }
        if (strCity.EndsWith(","))
        {
            strCity = strCity.Substring(0 , strCity.Length - 1);
        }
        strCity += ");"n"r";
        conn.Close();
        return "<script language='javascript'>"n"r" + strCity + strProv +
                strPlace + "</script>";
    }
}
*******************************************************************************
Web.config(可更改数据库连接字符串)
<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    "Windows"Microsoft.Net"Framework"v2.x"Config 
-->
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Data Source=SQL2KSVR;Initial Catalog=APLNetTest.apl;Persist Security Info=True;User ID=sa;Password=arlington"/>
  </appSettings>
 <connectionStrings/>
 <system.web>
  <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
  <compilation debug="true"/>
  <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
  <authentication mode="Windows"/>
  <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
 </system.web>
</configuration>
posted @ 2009-02-16 17:04  litao6664  阅读(570)  评论(0编辑  收藏  举报