数据维护页面(搜索分页)

数据维护页面包括如下的内容

1) 界面的设计(分为四个并列层,分别放置:搜索条件控件,相关操作按钮,数据展现控件,分页控件)

基本效果如下:

 

2) 分页控件的使用

  1. 将给的数据库附加到SqlServer中。
  2. 在给的数据库中执行“分页存储过程.sql”内的代码,创建存储过程。

      注意:(要在自己所要查找的表中运        行,不要在master中运行)

  1. 将“CommonPageQuery.cs”文件拷贝到网站内的App_Code目录中,并将Web.config内的连接串名拷贝到CommonPageQuery.cs文件内的对应位置。   例如:

 链接串名要相同。

 

  1. 将“AspNetPager.dll”文件拷贝到网站内的Bin目录内。
  2. 将如下的代码拷贝到网页的.aspx文件的第二行位置

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>  

  1. 将如下的代码拷贝到网页的.aspx文件放置分页控件的层内。

<webdiyer:AspNetPager ID="AspNetPager1" runat="server" PagingButtonSpacing="10px"

                    OnPageChanged="AspNetPager1_PageChanged" ShowCustomInfoSection="Right" CustomInfoHTML="总记录数:%RecordCount%,总页数:%PageCount%,当前为第%CurrentPageIndex%"

                    Width="100%" LayoutType="Table" ShowNavigationToolTip="true" SubmitButtonText="Go"

                    ShowPageIndexBox="Always" UrlPageIndexName="pageindex" PageSize="14">

</webdiyer:AspNetPager>

 

3) 搜索的事件代码编写方法

采用字符串拼接的方式编写Sql语句

  1. 首先写好基本的查询Sql语句,如果Sql语句本身没Where条件,给Sql加上” Where 1=1 ”.
  2. 根据搜索界面内控件的使用情况,自动将对应的条件语句拼接到查询Sql中。

例如:

if(this.TextBox1.Text !=""){

               QuerySql += " and ProductNumber like '%"

                   + TextBox1.Text + "%'";

}

如果是某些字段的取值只能从固定范围内取,需要利用SqlDataSource将其所有取值数据项查询出,并于DropDownList控件绑定,并给DropDownList手工添加一项多余的数据。并设定添加项的Text为”=请选择xxx=”,Value为”=”,和“男”,“女”,并设置AppendDataBoundItems为True.

拼接代码如下:

            if(Ddl2.SelectedValue != "Null"){

               QuerySql += " and SerialName = '"

                  + Ddl2.SelectedValue+"'";

      显示的部分用Gridview

 }

  1. 将所有的搜索条件都拼接上,即可。

4) 数据展现控件的外观设置。

设置控件套用专业风格,并宽为100%

 

 

将如下代码拷贝到网页的.aspx.cs文件中。

  protected void AspNetPager1_PageChanged(object sender, EventArgs e)   

    {

        BindData();

    }

 

    protected void BindData()               //自己设计的BindData

    {

        if (ViewState["QuerySQL"] != null && ViewState["CountSQL"] != null)

        {

          

            string QuerySQL = ViewState["QuerySQL"].ToString();

            string CountSQL = ViewState["CountSQL"].ToString();

            string GetFileds = "学号, 姓名, 性别, 身份证号";

            int index = this.AspNetPager1.CurrentPageIndex;

            int Size = this.AspNetPager1.PageSize;

            CommonPageQuery SQL = new CommonPageQuery(QuerySQL, CountSQL, GetFileds, index, Size);

            SQL.Execute();

            this.AspNetPager1.RecordCount = SQL.TotalNum;

            this.GridView1.DataSource = SQL.Result;

            this.GridView1.DataBind();

        }

    

    

    }

 

 

 

 

    protected void Button1_Click(object sender, EventArgs e)

    {

        

        string QuerySQL = "SELECT 学号, 姓名, 性别, 身份证号 FROM [JWInfo].[dbo].[学生信息] where 1=1 ";

        string CountSQL = "SELECT  count(*) from 学生信息 where 1=1";

        if(this.TextBox1.Text !="")

        {

            QuerySQL += " and  姓名 like '%" + this.TextBox1.Text + "%'";

            CountSQL += " and  姓名 like '%" + this.TextBox1.Text + "%'";

        }

 

        if(this.DropDownList1.SelectedValue !="=")

        { 

             QuerySQL += " and 性别='" + this.DropDownList1.SelectedValue + "'";

             CountSQL += " and 性别='" + this.DropDownList1.SelectedValue + "'";

        }

        QuerySQL += " order by 学号";

        ViewState["QuerySQL"] = QuerySQL;

        ViewState["CountSQL"] = CountSQL;

        this.AspNetPager1.CurrentPageIndex = 1;

        BindData();

 

 

    }

   注意:自己在写SQL的语句时  QuerySQL += " and  姓名 like '%" + this.TextBox1.Text + "%'"; 这种语句的开头和结尾的部分需要加上空格,否则会导致SubString()的参数不对的错误,因为“and”后面不加空格的话 会和前面的单词连在一起。

 

 

 

posted @ 2014-03-20 15:23  三江学院计算机系学生兴趣小组  阅读(267)  评论(0编辑  收藏  举报