码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-03-03 09:45:32      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享

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

DropDownList则需要放在UpdatePanel中:

技术分享

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)
    {
    }

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

标签:

原文地址:http://www.cnblogs.com/Tirisfal/p/4310148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!