数据维护页面包括如下的内容
1) 界面的设计(分为四个并列层,分别放置:搜索条件控件,相关操作按钮,数据展现控件,分页控件)
基本效果如下:
2) 分页控件的使用
注意:(要在自己所要查找的表中运 行,不要在master中运行)
链接串名要相同。
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<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语句
例如:
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
}
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”后面不加空格的话 会和前面的单词连在一起。