首页 > 代码库 > webform(八)组合查询

webform(八)组合查询

组合查询就是根据条件取出某些数据并展示出来。

前台代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>组合查询</title>
    <style type="text/css">
        .UF_div {
            width: 1200px;
            margin: 0 auto;
            background-color: #eeeeee;
        }

        .User_div {
            display: inline-block;
            background-color: #ffccff;
            border-color: blue;
            border-width: 5px;
            margin: 5px 0;
            margin-left: 5px;
            border-style: solid;
            height: 210px;
            width: 220px;
            line-height:30px;
        }

            .User_div:hover {
                background-color: palevioletred;
            }
        .div_select{
            margin-left:76px;
            /*margin-right:180px;*/
            width:1000px;
            height:100px;
            line-height:50px;

        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="div_select"> 
            名称:<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
            &nbsp;&nbsp;&nbsp;&nbsp;
            地区:<asp:DropDownList ID="ddl_partys" runat="server"></asp:DropDownList>
            &nbsp;&nbsp;&nbsp;&nbsp;
            位置:<asp:DropDownList ID="ddl_lanes" runat="server"></asp:DropDownList>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="btn_select" runat="server" Text="查询" />
            <br />
            <asp:Label ID="Label1" runat="server" Text="查询语句"></asp:Label>
        </div>
     <div class="UF_div">
            <asp:Repeater ID="UserData_all" runat="server">
                <ItemTemplate>
                    <div class="User_div">&nbsp;&nbsp;&nbsp;号:<%# Eval("Ucode")%><br />
                        用户名:<%# Eval("UserName")%><br />&nbsp;&nbsp;&nbsp;码:<%# Eval("PassWord")%><br />&nbsp;&nbsp;&nbsp;称:<%# Eval("NickName")%><br />&nbsp;&nbsp;&nbsp;别:<%# Eval("SexStr")%><br />&nbsp;&nbsp;&nbsp;区:<%# Eval("PartyName")%><br />&nbsp;&nbsp;&nbsp;置:<%# Eval("LaneName")%><br />
                    </div>
                </ItemTemplate>
            </asp:Repeater>
            <div style="clear: both"></div>
         </div>
    </form>
</body>
</html>

后台代码

protected void Page_Load(object sender, EventArgs e)
    {
        //查询按钮
        btn_select.Click += Btn_select_Click;
        //如果首次登录 加载数据
        if (!IsPostBack)
        {
            //数据绑定
            UserData_all.DataSource = new UsersData().SelectAll();
            UserData_all.DataBind();

            //地区列表绑定
            ddl_partys.DataSource = new PartysData().SelectAll();
            ddl_partys.DataValueField = "PartyCode";
            ddl_partys.DataTextField = "PartyName";
            ddl_partys.DataBind();
            ddl_partys.Items.Add(new ListItem("全部", "all"));
            ddl_partys.Items.FindByValue("all").Selected = true;
            //位置列表绑定
            ddl_lanes.DataSource = new LanesData().SelectAll();
            ddl_lanes.DataValueField = "LaneCode";
            ddl_lanes.DataTextField = "LaneName";
            ddl_lanes.DataBind();
            ddl_lanes.Items.Add(new ListItem("全部", "all"));
            ddl_lanes.Items.FindByValue("all").Selected = true;
        }
    }
    //查询按钮点击事件
    private void Btn_select_Click(object sender, EventArgs e)
    {
        //拼接语句
        string sql = "select * from Users";
        Hashtable hs = new Hashtable();
        int count = 0;
        if (txt_name.Text.Trim().Length>0)
        {
            sql += " where NickName like @a";
            hs.Add("@a",  "%"+txt_name.Text.Trim()+"%");
            count++;
        }
        string x = ddl_partys.SelectedValue;
        if (x != "all")
        {
            if (count == 0)
            {
                sql += " where Party = ‘" + x + "";
            }
            else
            {
               sql+= " and Party = ‘" + x + "";
            }
            count++;
        }
        string y = ddl_lanes.SelectedValue;
        if (y != "all")
        {
            if (count == 0)
            {
                sql += " where Lane = ‘" + y+"";
            }
            else
            {
                sql += " and Lane = ‘" + y + "";
            }
            count++;
        }
        //调用方法 通过sql语句查询 接收返回值 并绑定数据
        Label1.Text = sql;
        List<Users> ul = new UsersData().SelectThe(sql, hs);
        UserData_all.DataSource = ul;
        UserData_all.DataBind();
    }
}

效果图

技术分享

技术分享

技术分享

技术分享

webform(八)组合查询