.NET 二級聯動

.cs

using System;
using System.Data;
using System.Configuration;
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 Ajax;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(_Default));
       
        //btnData.Attributes.Add("onclick","getData()");
        string ConnStr = @"Data Source=CHMZH;Initial Catalog=DataBase;user id=sa;password=123456";
        SqlConnection sqlConn = new SqlConnection(ConnStr);
        try
        {
            if (!Page.IsPostBack)
            {
                cmbProvince.Attributes.Add("onchange", "SelectCity()");  

                string strSQL = "select * from Province";

                SqlDataAdapter adpGetDst = new SqlDataAdapter(strSQL, sqlConn);
                DataSet dstProvince = new DataSet();
                SqlCommandBuilder cbdGetDst = new SqlCommandBuilder(adpGetDst);
                adpGetDst.Fill(dstProvince);
                cmbProvince.Items.Clear();
                for (int intRowCount = 0; intRowCount < dstProvince.Tables[0].Rows.Count; intRowCount++)
                {
                    string strValue = dstProvince.Tables[0].Rows[intRowCount]["ProvinceID"].ToString();
                    string strText = dstProvince.Tables[0].Rows[intRowCount]["ProvinceName"].ToString();
                    cmbProvince.Items.Add(new ListItem(strText,strValue));
                }
                //cmbProvince.DataSource = dstProvince;
                //cmbProvince.DataValueField = "ProvinceID";
                //cmbProvince.DataTextField = "ProvinceName";
                //cmbProvince.DataBind();
                string strProvinceID = cmbProvince.SelectedValue;
                strSQL = "select * from City where ProvinceID=" + "/'" + strProvinceID + "/'";

                adpGetDst = new SqlDataAdapter(strSQL, sqlConn);

                cbdGetDst = new SqlCommandBuilder(adpGetDst);
                DataSet dstCity = new DataSet();
                adpGetDst.Fill(dstCity);
                cmbCity.Items.Clear();
                for (int intRowCount = 0; intRowCount < dstCity.Tables[0].Rows.Count; intRowCount++)
                {
                    string strValue = dstCity.Tables[0].Rows[intRowCount]["CityID"].ToString();
                    string strText = dstCity.Tables[0].Rows[intRowCount]["CityName"].ToString();
                    cmbCity.Items.Add(new ListItem(strText, strValue));
                }

            }
            else
            {
                int intCitySelected = cmbCity.SelectedIndex;
                string strProvinceID = cmbProvince.SelectedValue;
                string strSQL = "select * from City where ProvinceID=" + "/'" + strProvinceID + "/'";

                SqlDataAdapter adpGetDst = new SqlDataAdapter(strSQL, sqlConn);

                SqlCommandBuilder cbdGetDst = new SqlCommandBuilder(adpGetDst);
                DataSet dstCity = new DataSet();
                adpGetDst.Fill(dstCity);
                cmbCity.Items.Clear();
                for (int intRowCount = 0; intRowCount < dstCity.Tables[0].Rows.Count; intRowCount++)
                {
                    string strValue = dstCity.Tables[0].Rows[intRowCount]["CityID"].ToString();
                    string strText = dstCity.Tables[0].Rows[intRowCount]["CityName"].ToString();
                    cmbCity.Items.Add(new ListItem(strText, strValue));

                }
                cmbCity.SelectedIndex= intCitySelected;
            }
        }
       
        catch (Exception exc)
        {
           
        }

    }

    [AjaxMethod()]
    public DataSet getCity(string strProvinceID)
    {
        string ConnStr = @"Data Source=CHMZH;Initial Catalog=DataBase;user id=sa;password=123456";
        string strReturn = "";
        DataSet dstCity = new DataSet();
        try
        {
            SqlConnection sqlConn = new SqlConnection(ConnStr);
            sqlConn.Open();
            string strSQL = "select * from City where ProvinceID=" + "/'" + strProvinceID +"/'";

            SqlDataAdapter adpGetDst = new SqlDataAdapter(strSQL, sqlConn);
           
            SqlCommandBuilder cbdGetDst = new SqlCommandBuilder(adpGetDst);
            adpGetDst.Fill(dstCity);
        }
        catch (Exception exc)
        {

        }
        return dstCity;
    }

 /*   [AjaxMethod()]
    public string getCity(string strProvince)
    {
        string ConnStr = @"Data Source=CHMZH;Initial Catalog=DataBase;user id=sa;password=123456";
        string strReturn = "";

        try
        {
            SqlConnection sqlConn = new SqlConnection(ConnStr);
            sqlConn.Open();
            string strSQL = "select * from City where ProvinceID=" + "/'" + strProvince + "/'";

            SqlDataAdapter adpGetDst = new SqlDataAdapter(strSQL, sqlConn);

            SqlCommandBuilder cbdGetDst = new SqlCommandBuilder(adpGetDst);
            adpGetDst.Fill(dstCity);

            for (int intRowCount = 0; intRowCount < dstCity.Tables[0].Rows.Count; intRowCount++)
            {
                //cmbCity.Items.Add(dstCity.Tables[0].Rows[intRowCount]["CityName"].ToString());
                if (intRowCount == dstCity.Tables[0].Rows.Count - 1)
                    strReturn += dstCity.Tables[0].Rows[intRowCount]["CityName"].ToString();
                else
                    strReturn += dstCity.Tables[0].Rows[intRowCount]["CityName"].ToString() + "&";

            }
            Session["strCity"] = strReturn;
        }
        catch (Exception exc)
        {

        }
        return strReturn;
    }
    */
    protected void btnData_Click(object sender, EventArgs e)
    {
        //string strCity = txtData.Text;
        //string[] strValueAndText = strCity.Split('|');
        //string[] strCitys={""};
        //string strValue="";
        //string strText="";
        //cmbCity.Items.Clear();
        //for(int i=0;i<strValueAndText.Length;i++)
        //{
        //    if (i == strValueAndText.Length - 1)
        //    {
        //        strCitys = strValueAndText[i].Split('&');
        //        cmbCity.SelectedIndex = int.Parse(strCitys[1].ToString());
        //    }
        //    else
        //    {
        //        strCitys = strValueAndText[i].Split('&');
        //        cmbCity.Items.Add(new ListItem(strCitys[1].ToString(), strCitys[0].ToString()));
        //    }

        //}

        Response.Write("省份:" + cmbProvince.SelectedIndex + "市" + cmbCity.SelectedIndex);
    }

}

 

.html

<%@ Page Language="C#" CodeFile="Default.aspx.cs"  EnableEventValidation="true" 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>Untitled Page</title>
    <script language=javascript>
    function SelectCity()
    {   
        var objProvince = document.getElementById("cmbProvince");
         _Default.getCity(objProvince.value,getResultCallBack);
    }   
   
    function getResultCallBack(response)
 {
     var objCity = document.getElementById("cmbCity");
  if (response.value != null)
  {     
   //debugger;
   objCity.options.length=0;
   
   var ds = response.value;

   if(ds != null && typeof(ds) == "object" && ds.Tables != null)
   {     
    for(var i=0; i<ds.Tables[0].Rows.length; i++)
    {
     var name=ds.Tables[0].Rows[i].CityName;
        var id=ds.Tables[0].Rows[i].CityID;
        objCity.options.add(new Option(name,id));

       }
      }
     }
    }
   
    function getData()
    {     
        var objCity = document.getElementById("cmbCity");
        var cindex = objCity.selectedIndex;
  var cValue = objCity.options[cindex].value;
  var cText  = objCity.options[cindex].text;
  var objData = document.getElementById("<%=txtData.ClientID%>");
  objData.value="";
  for(var i=0;i<objCity.length;i++)
  {
      var stroption =  objCity.options[i].value +"&"+ objCity.options[i].text;
      if(i==objCity.length-1)
      {
          objData.value += "|"+stroption + "|Index&"+cindex;
      }
      else
      {
          objData.value += stroption;
      }
     
  }        
        return true;
    }
   
    </script>
</head>
<body runat="server">
    <form id="form1" runat="server" action="Default.aspx">
    <div>
        <asp:DropDownList ID="cmbProvince" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="cmbCity" runat="server">
        </asp:DropDownList>
        <div style="visibility:hidden">
        <asp:TextBox ID="txtData" runat="server"></asp:TextBox></div>
        <asp:Button ID="btnData" runat="server" Text="Button" OnClick="btnData_Click" /></div>
    </form>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值