ASP.NET实现列表页连接查询 拼接sql语句 绑定grivdView

ASP.NET实现列表页连接查询 拼接sql语句

如图效果:ASP.NET实现列表页连接查询 拼接sql语句 绑定grivdView

基本需求:1、当页面第一次加载的时候默认查询一个月时间(或者说是登陆者所属权限的所有数据)的数据绑定到gridView

2、添加查询条件时连接查询实现绑定

3、点击清空所有条件为空,查询所有数据

4、gridView实现分页

gridview分页

设定属性AllowPaging="True"就会默认分页,在做后台翻页的显示功能就行

前台按钮  <tr>
                <td style="border-top: #000000 1px solid; border-bottom: #000000 1px solid; font-size:16px;">
                    <asp:ImageButton ID="BtnFirst" runat="server" CommandName="First" ImageUrl="../images/Button/First.jpg"
                        OnClick="PagerButtonClick" />
                    <asp:ImageButton ID="BtnPre" runat="server" CommandName="Pre" ImageUrl="../images/Button/Pre.jpg"
                        OnClick="PagerButtonClick" />
                    <asp:ImageButton ID="BtnNext" runat="server" CommandName="Next" ImageUrl="../images/Button/Next.jpg"
                        OnClick="PagerButtonClick" />
                    <asp:ImageButton ID="BtnLast" runat="server" CommandName="Last" ImageUrl="../images/Button/Last.jpg"
                        OnClick="PagerButtonClick" />
                    &nbsp;第<asp:Label ID="LabCurrentPage" runat="server" Text="Label"></asp:Label>页&nbsp;
                    共<asp:Label ID="LabPageSum" runat="server" Text="Label"></asp:Label>页&nbsp;
                    <asp:TextBox ID="TxtPageSize" runat="server" CssClass="TextBoxCssUnder2" Height="20px"
                        Width="35px">15</asp:TextBox>
                    行每页 &nbsp; 转到第<asp:TextBox ID="GoPage" runat="server" CssClass="TextBoxCssUnder2"
                        Height="20px" Width="33px"></asp:TextBox>
                    页&nbsp;
                    <asp:ImageButton ID="ButtonGo" runat="server" OnClientClick="javascript:return CheckValuePiece();"
                        ImageUrl="../images/Button/Jump.jpg" OnClick="ButtonGo_Click" />
                    &nbsp;&nbsp;
                </td>
                <td align="right" valign="middle" style="border-top: #000000 1px solid; border-bottom: #000000 1px solid;">
                    <asp:ImageButton ID="ibtnAdd1" runat="server" ImageUrl="../images/Button/BtnAdd.jpg"
                        ImageAlign="AbsMiddle" OnClick="ibtnAdd1_Click" Height="17px" />&nbsp;
                    <asp:ImageButton ID="ibtnDel1" runat="server" OnClientClick="javascript:return CheckDel();"
                        ImageUrl="../images/Button/BtnDel.jpg" ImageAlign="AbsMiddle" OnClick="ibtnDel1_Click" />&nbsp;
                </td>
            </tr>

后台分页显示功能:

#region  分页方法
    protected void ButtonGo_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (GoPage.Text.Trim().ToString() == "")
            {
                Response.Write("<script language='javascript'>alert('页码不可以为空!');</script>");
            }
            else if (GoPage.Text.Trim().ToString() == "0" || Convert.ToInt32(GoPage.Text.Trim().ToString()) > GVData.PageCount)
            {
                Response.Write("<script language='javascript'>alert('页码不是一个有效值!');</script>");
            }
            else if (GoPage.Text.Trim() != "")
            {
                int PageI = Int32.Parse(GoPage.Text.Trim()) - 1;
                if (PageI >= 0 && PageI < (GVData.PageCount))
                {
                    GVData.PageIndex = PageI;
                }
            }

if (TxtPageSize.Text.Trim().ToString() == "")
            {
                Response.Write("<script language='javascript'>alert('每页显示行数不可以为空!');</script>");
            }
            else if (TxtPageSize.Text.Trim().ToString() == "0")
            {
                Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
            }
            else if (TxtPageSize.Text.Trim() != "")
            {
                try
                {
                    int MyPageSize = int.Parse(TxtPageSize.Text.ToString().Trim());
                    this.GVData.PageSize = MyPageSize;
                }
                catch
                {
                    Response.Write("<script language='javascript'>alert('每页显示行数不是一个有效值!');</script>");
                }
            }    }
        catch
        {    
        }
    }

数据查询绑定:

查询字符串拼接

string sql = "", sqlConditions = "", errorMsg = "";
        if (title !="")///不能用null如果用null找不到结果会提示错误
        {
            sqlConditions += "and title like'%"+title+"%'";
        }
        if (place != "")
        {
            sqlConditions += "and place like'%" + place + "%'";
        }
        if (applyer != "")
        {
            sqlConditions += "and applyer ='" + applyer + "'";
        }
        if (personList != "")
        {
            sqlConditions += "and PersonList like'%" + personList + "%'";
        }

sql = "select * from table where  " + sqlConditions + "order by TimeFr desc,TimeTo desc ,TimeTo desc,place desc";

字符串拼接需注意如果说是单表查询如果都以上面的格式拼接则会出现sql语句查询错误,找不到数据 错误"where and title……"

处理方法1

select * from table where 1=1 " + sqlConditions + "就会得到所查结果 查询结果绑定:但是这样不科学。

处理方法2

利用字符串处理函数去掉开始的“and”,查询条件都为空时默认查询所有数据。

if (sqlConditions.StartsWith("and"))
        {
            sqlConditions = sqlConditions.Substring(3);
            sql = "select * from T_Dispatch where " + sqlConditions + "order by TimeFr desc,TimeTo desc ,TimeTo desc,place desc";
        }
        else
        {
            if (sqlConditions == "")
            {
                sql = "select * from T_Dispatch order by TimeFr desc,TimeTo desc ,TimeTo desc,place desc";
            }
        }

DataSet ds = ZWL.DBUtility.DbHelperSQL.SelectDataSet(sql, "", ref errorMsg);
        GVData.DataSource = ds.Tables[0];
        GVData.DataBind();
        LabPageSum.Text = Convert.ToString(GVData.PageCount);
        LabCurrentPage.Text = Convert.ToString(((int)GVData.PageIndex + 1));
        this.GoPage.Text = LabCurrentPage.Text.ToString();
  处理方法3

当然是加上限制字段ok,若果谁有更好的方法希望交流学习

上一篇:Hibernate中的对象状态,及自动更新原因,Hibernate set对象后不调用update却自动更新


下一篇:Selenium自动化中DOM,XPATH,CSS定位Web页面对象的优劣性分析