20150303--从SQL中获取数据的三级联动

省市地区的三级联动,每变更一次所选地都需要提交,但是又不需要把整个页面提交,所以我们需要使用控件:UdataPanel。工具--AJAX扩展

image

还有ScriptManager,并要将其放在页面的最顶端。

DropDownList则需要放在UpdatePanel中:

image

HTML源:

<body>

    <form id="form1" runat="server">

    <div>

    

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

    

    </div>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>

                <asp:DropDownList ID="ddl_sheng" runat="server" AutoPostBack="true" DataTextField="Name" DataValueField="ParentId" OnSelectedIndexChanged="ddl_sheng_SelectedIndexChanged">

                </asp:DropDownList>

                <asp:DropDownList ID="ddl_shi" runat="server" AutoPostBack="true" DataTextField="Name" DataValueField="ParentId" OnSelectedIndexChanged="ddl_shi_SelectedIndexChanged">

                </asp:DropDownList>

                <asp:DropDownList ID="ddl_diqu" runat="server" DataTextField="Name" DataValueField="ParentId" OnSelectedIndexChanged="ddl_diqu_SelectedIndexChanged">

                </asp:DropDownList>

            </ContentTemplate>

        </asp:UpdatePanel>

    </form>

</body>

需要注意的是,在DropDownList的标签中,必须把AutoPostBack的属性设置为 true,自动回发数据。

DataTextField="Name" DataValueField="ParentId"

则是用来绑定显示的Text以及值Value。

cs中的代码:

DiquDataContext diqu=new DiquDataContext();//外部定义LINQ方便使用



    protected void Page_Load(object sender, EventArgs e)

    {

        //第一次加载时才会走这里,必须要有,否则每次改动选择值时加载都会显示第一次加载的数据

        if (!IsPostBack)

        {

            getsheng();

            getshi();

            getdiqu();

        }

    }

    /// <summary>

    /// 绑定省

    /// </summary>

    public void getsheng()//

    {

        var va = diqu.Members.Where(m => (m.ParentId.ToString().Length == 2));

        ddl_sheng.DataSource = va;

        ddl_sheng.DataTextField = "Name";

        ddl_sheng.DataValueField = "ParentId";

        ddl_sheng.DataBind();

    }

   /// <summary>

   /// 绑定市

   /// </summary>

    public void getshi()//

    {

        string ss = ddl_sheng.SelectedValue;//获取显示的省的值。

        var sh = diqu.Members.Where(m => (m.ParentId.ToString().Length == 4) && (m.ParentId.ToString().Substring(0, 2) == ss));

        ddl_shi.DataSource = sh;

        ddl_shi.DataTextField = "Name";

        ddl_shi.DataValueField = "ParentId";

        ddl_shi.DataBind();

    }

    /// <summary>

    /// 绑定地区

    /// </summary>

    public void getdiqu()

    {

        string ss = ddl_shi.SelectedValue;//获取显示的省的值。

        var sh = diqu.Members.Where(m => (m.ParentId.ToString().Length == 6) && (m.ParentId.ToString().Substring(0, 4) == ss));

        ddl_diqu.DataSource = sh;

        ddl_diqu.DataTextField = "Name";

        ddl_diqu.DataValueField = "ParentId";

        ddl_diqu.DataBind();



    }

    protected void ddl_shi_SelectedIndexChanged(object sender, EventArgs e)

    {

        getdiqu();

    }

    protected void ddl_sheng_SelectedIndexChanged(object sender, EventArgs e)

    {

        getshi();

        getdiqu();

    }

    protected void ddl_diqu_SelectedIndexChanged(object sender, EventArgs e)

    {

    }

你可能感兴趣的:(三级联动)