三層三級聯動

aspx頁

<body>
    <form id="form1" runat="server">
    <div>
    <table><tr><td>用戶名</td><td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
        <tr><td>密碼</td><td>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td></tr>
        <tr><td>確認密碼</td><td>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td></tr>
        <tr><td>郵箱</td><td>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td></tr>
        <tr><td>地址</td><td>
            <asp:DropDownList ID="ddlprovince" runat="server" AutoPostBack="True"
                onselectedindexchanged="ddlprovince_SelectedIndexChanged">
            </asp:DropDownList>省
            <asp:DropDownList ID="ddlcity" runat="server" AutoPostBack="True"
                onselectedindexchanged="ddlcity_SelectedIndexChanged">
            </asp:DropDownList>市
            <asp:DropDownList ID="ddlarear" runat="server">
            </asp:DropDownList>縣
        </td></tr></table>
    </div>
    </form>
</body>

cs頁

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadProvince();
            }
        }

        private void LoadProvince()
        {
            BLL.province bpro = new BLL.province();
            DataTable dt= bpro.GetList("").Tables[0];

            ddlprovince.DataSource = dt;
            ddlprovince.DataTextField = "provincename";
            ddlprovince.DataValueField = "provinceID";
            ddlprovince.DataBind();

        }

        protected void ddlprovince_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlarear.Items.Count>0)
            {
                this.ddlarear.Items.Clear();
            }
            BLL.city bcity = new BLL.city();
            string proid = this.ddlprovince.SelectedItem.Value;
           
            DataTable dt = bcity.GetList("father='"+proid+"'").Tables[0];

            ddlcity.DataSource = dt;
            ddlcity.DataTextField = "cityname";
            ddlcity.DataValueField = "cityID";
            ddlcity.DataBind();

            //綁定默認顯示的市級區劃下面所有的縣
            string cityid= dt.Rows[0]["cityID"].ToString();
            BLL.area baraer = new BLL.area();
           
            DataTable dtarea = baraer.GetList("father='" + cityid + "'").Tables[0];

            ddlarear.DataSource = dtarea;
            ddlarear.DataTextField = "areaname";
            ddlarear.DataValueField = "areaID";
            ddlarear.DataBind();

        }

        protected void ddlcity_SelectedIndexChanged(object sender, EventArgs e)
        {
            BLL.area baraer = new BLL.area();
            string cityid = this.ddlcity.SelectedItem.Value;

            DataTable dt = baraer.GetList("father='" + cityid + "'").Tables[0];

            ddlarear.DataSource = dt;
            ddlarear.DataTextField = "areaname";
            ddlarear.DataValueField = "areaID";
            ddlarear.DataBind();
        }
    }
}

 

Getlist方法

public DataSet GetList(string strWhere)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select id,provinceID,provincename ");
   strSql.Append(" FROM province ");
   if(strWhere.Trim()!="")
   {
    strSql.Append(" where "+strWhere);
   }
   return DbHelperSQL.Query(strSql.ToString());
  }

Query方法:

 public static DataSet Query(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章