Webform 三级联动例子

本文介绍如何使用ASP.NET实现省市区三级联动下拉菜单的功能。通过创建实体类、数据访问类,并结合后台代码完成数据绑定及联动更新。适用于需要实现地区选择功能的Web应用程序。

首先分别做三个下拉列表

<body>
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>
</form>
</body>

建类:China实体类

public China()
    {
        
    }
    private string code;

    public string Code
    {
        get { return code; }
        set { code = value; }
    }
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private string prentcode;

    public string Prentcode
    {
        get { return prentcode; }
        set { prentcode = value; }
    }

数据访问类:

public class ChinaData
{
    SqlConnection conn = null;
    SqlCommand cmd = null;
    public ChinaData()
    {
        conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
        cmd = conn.CreateCommand();
    }
    public List<China> Select(string pcode)
    {
        cmd.CommandText = "select * from ChinaStates where ParentAreaCode=@pcode";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@pcode", pcode);

        conn.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        List<China> list = new List<China>();

        if (dr.HasRows)
        {
            while (dr.Read())
            {
                China data = new China();
                data.Code = dr[0].ToString();
                data.Name = dr[1].ToString();
                data.Prentcode = dr[2].ToString();

                list.Add(data);

            }


        }


        conn.Close();


        return list;

}


}

 

.cs后台代码

首先数据绑定:

if (!IsPostBack)
        {
  //调用方法绑定数据         
Bind(DropDownList1,new ChinaStatesData().Select("0001"));

Bind(DropDownList2, new ChinaStatesData().Select(DropDownList1.SelectedValue));

Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));
        }
//委托
        DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;
        DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;
//造方法:
private void Bind(DropDownList ddl, List<ChinaStates> list)
    {
        ddl.DataSource = list;
        ddl.DataTextField = "AreaName";
        ddl.DataValueField = "AreaCode";
        ddl.DataBind();
    }
//SelectedIndexChanged事件
 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind(DropDownList2, new ChinaStatesData().Select(DropDownList1.SelectedValue));//填充市
        Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));//填充区
    }
void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));//填充区
    }

代码部分完成后,将下拉列表的AutoPostBack属性改为true

转载于:https://www.cnblogs.com/dreamer666/p/5897515.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值