三级联动代码优化

三级连动前台代码:
<div>
      
        省:
        <asp:DropDownList ID="ddlPromary" runat="server" AutoPostBack="True" Height="16px" OnSelectedIndexChanged="ddlPromary_SelectedIndexChanged" Width="118px">
        </asp:DropDownList>
& nbsp;&nbsp; 市:<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="True" Height="16px" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged" Width="118px">
        </asp:DropDownList>
& nbsp;&nbsp; 县:&nbsp;
        <asp:DropDownList ID="ddlCount" runat="server" Height="16px" Width="118px">
        </asp:DropDownList>
      
    </div>


三级后台代码:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //第一次加载的时候绑定ddlPromary控件的值
                 string sql = "select * from province";
                DataTable dt=ExecuteDataTable(sql);
                ddlPromary.DataSource = dt;
                ddlPromary.DataTextField = "province";
                ddlPromary.DataValueField = "provinceID";
                ddlPromary.DataBind();
            }
        }




        protected void ddlPromary_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            ddlCity.Items.Clear();
            string sql = "select * from city where father=@id";
            DataTable dt = ExecuteDataTable(sql, new SqlParameter("@id",ddlPromary.SelectedValue.ToString()));
            ddlCity.DataSource = dt;
            ddlCity.DataTextField = "city";
            ddlCity.DataValueField = "cityID";
            ddlCity.DataBind();


        }


        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCount.Items.Clear();
            string sql = "select * from area where father=@areaID";
            DataTable dt = ExecuteDataTable(sql, new SqlParameter("@areaID", ddlCity.SelectedValue.ToString()));
            ddlCount.DataSource = dt;
            ddlCount.DataTextField = "area";
            ddlCount.DataValueField = "areaID";
            ddlCount.DataBind();
        }


        //定义一个方法,传入两个参数,一个为SQL语句,另一个是SQL语句的参数
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
        {
            string constr = @"data source=.;initial catalog=PromaryCityCount;user id=sa;password=111111;";
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();


            cmd.CommandText = sql;
            if (pms != null)
            {
                cmd.Parameters.AddRange(pms);
            }
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
            conn.Close();
            conn.Dispose();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值