省市县三级联动(webFrom...DropdownList)

编辑页面

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:DropDownList ID="DropDownList1" runat="server" Width="100px" AutoPostBack="True" Height="50px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" style="margin-bottom: 0px" >
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server" Width="100px" AutoPostBack="True" Height="50px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList3" runat="server" Width="100px" AutoPostBack="True" Height="50px">
        </asp:DropDownList>
    </form>
</body>
</html>

编辑cs

public partial class Test2 : System.Web.UI.Page
{
    public DataClassesDataContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = new DataClassesDataContext();
        if(!IsPostBack)
        {
            databind("0001");
            datashibind("11");
            dataxianbind("1101");
        }

    }
    private void databind(string parentareacode)
    {

        var querydata = from q in context.ChinaStates
                    where q.ParentAreaCode == parentareacode
                    select q;
        DropDownList1.DataSource = querydata;//指定数据源
   
        //DropDownList1.Items.Insert(0,new ListItem("请选择单位"));
     
        DropDownList1.DataTextField = "AreaName";//界面上显示的是地名
        DropDownList1.DataValueField = "AreaCode";//存的是地区编号
   
        DropDownList1.DataBind();


    }
    private void datashibind(string parentareacode)
    {
        var querydata = from q in context.ChinaStates
                        where q.ParentAreaCode ==parentareacode
                        select q;
        DropDownList2.DataSource = querydata;

        DropDownList2.DataTextField = "AreaName";//界面上显示的是地名
        DropDownList2.DataValueField = "AreaCode";//存的是地区编号
        DropDownList2.DataBind();

    }
    private void dataxianbind(string parentareacode)
    {

        var querydata = context.ChinaStates.Where(r=>r.ParentAreaCode==parentareacode);
        DropDownList3.DataSource = querydata;
        DropDownList3.DataTextField = "AreaName";//界面上显示的是地名
        DropDownList3.DataValueField = "AreaCode";//存的是地区编号
        DropDownList3.DataBind();

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string code = DropDownList1.SelectedValue.ToString();
        datashibind(code);
        if (DropDownList2.Items.Count > 0)
        {
            string s = DropDownList2.SelectedItem.Value.ToString();
            dataxianbind(s);
        }
        else 
        {
            DropDownList3.Items.Clear();
        }
       
   
      

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string code = DropDownList2.SelectedValue.ToString();
        dataxianbind(code);

    }
}
原文地址:https://www.cnblogs.com/liuyudong0825/p/4953076.html