我用的吴旗娃的分页控件AspNetPager,我能够实现在初始化页面的时候能显示显示总的记录数、共多少页、当前是第几页,而且还能分页。但是在按照查询功能的时候AspNetPager不能够按照查询出的结果显示总的记录数、共多少页、当前是第几页,而还是第一次初始化的页面。
举个例子:我的表中一共有30条记录。在页面初始化时DataGrid显示30条记录 当显示第1/3页(其中DataGrid每页显示10条记录).按照名称查询出应该是15条记录,那么AspNetPager就应该显示15条记录 当显示第1/2页.
不知道我说清楚了没有,怎么实现。谢谢啦。
举个例子:我的表中一共有30条记录。在页面初始化时DataGrid显示30条记录 当显示第1/3页(其中DataGrid每页显示10条记录).按照名称查询出应该是15条记录,那么AspNetPager就应该显示15条记录 当显示第1/2页.
不知道我说清楚了没有,怎么实现。谢谢啦。
<tr>
<td>
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="sname">软件名称</asp:ListItem>
<asp:ListItem Value="stype">软件种类</asp:ListItem>
<asp:ListItem Value="department">所属部门</asp:ListItem>
<asp:ListItem Value="buydate">购买日期</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="DropDownList2" runat="server">
<asp:ListItem Value="like">包含</asp:ListItem>
<asp:ListItem Value="=">等于</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="DropDownList3" runat="server">
<asp:ListItem Value="网络版">网络版</asp:ListItem>
<asp:ListItem Value="单机版">单机版</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="DropDownList4" runat="server"></asp:DropDownList>
<asp:TextBox id="txtQuery" runat="server"></asp:TextBox>
<asp:Button id="btnQuery" runat="server" Text="搜索"></asp:Button>
<asp:Button id="btnExcel" runat="server" Text="导出到Excel"></asp:Button></td>
</tr>
<TR>
<TD><asp:datagrid id="DataGrid" runat="server" Width="744px" Height="102px" AllowSorting="True" ShowFooter="True"
AutoGenerateColumns="False" HorizontalAlign="Center" PageSize="20" CssClass="datagridDiv">
<SelectedItemStyle Wrap="False" HorizontalAlign="Center"></SelectedItemStyle>
<EditItemStyle Wrap="False" HorizontalAlign="Center"></EditItemStyle>
<AlternatingItemStyle Wrap="False" HorizontalAlign="Center"></AlternatingItemStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<HeaderStyle Wrap="False" HorizontalAlign="Center" CssClass="fixedHeaderTr" VerticalAlign="Middle"
BackColor="DarkGray"></HeaderStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center" VerticalAlign="Middle" BackColor="Gainsboro"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="sid" SortExpression="sid" HeaderText="软件编号">
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="80px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center"></FooterStyle>
</asp:BoundColumn>
<asp:HyperLinkColumn Target="_self" DataNavigateUrlField="sid" DataNavigateUrlFormatString="softwaredetail.aspx?sid={0}"
DataTextField="sname" SortExpression="sname" HeaderText="软件名称">
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="300px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center"></FooterStyle>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="stype" SortExpression="stype" HeaderText="软件种类">
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="70px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="number" SortExpression="number" HeaderText="数量">
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="50px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="buydate" SortExpression="buydate" HeaderText="购买日期">
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="80px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="cost" SortExpression="cost" HeaderText="费用(元)">
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="80px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="department" SortExpression="department" HeaderText="所属部门">
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="100px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<FooterStyle Wrap="False" HorizontalAlign="Center"></FooterStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" PageButtonCount="9" Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid></TD>
</TR>
<TR>
<TD><webdiyer:aspnetpager id="AspNetPager1" runat="server" Width="672px" PageSize="20" UrlPaging="True" ShowCustomInfoSection="Left"
NumericButtonTextFormatString="[{0}]" ShowBoxThreshold="5" AlwaysShow="True" OnPageChanged="AspNetPager1_PageChanged"
NextPageText="下一页" PrevPageText="上一页" FirstPageText="首页" LastPageText="尾页"></webdiyer:aspnetpager></TD>
</TR>
</TABLE>
public class WebForm1 : System.Web.UI.Page
{
protected Wuqi.Webdiyer.AspNetPager AspNetPager1;
protected System.Web.UI.WebControls.DataGrid DataGrid;
SqlDataAdapter adapter;
SqlCommand cmd;
DataSet ds;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.TextBox txtQuery;
protected System.Web.UI.WebControls.Button btnQuery;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
public Department dep=new Department();
protected System.Web.UI.WebControls.DropDownList DropDownList4;
protected System.Web.UI.WebControls.Button btnExcel;
SqlConnection myConn=new SqlConnection("server=192.168.16.10;database=softwaremanage;uid=sa;pwd=sa");
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{ this.DropDownList3.Visible=false;
this.DropDownList4.Visible=false;
string strdep = "select department from DepartmentInfo";
dep.Listdep(DropDownList4,strdep);
SqlConnection conn = new SqlConnection("server=192.168.16.10;database=softwaremanage;uid=sa;pwd=sa;");
conn.Open();
cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandText="select count(*) from SoftwareInfo ";
AspNetPager1.AlwaysShow=true;
AspNetPager1.PageSize=20;
AspNetPager1.RecordCount=(int)cmd.ExecuteScalar();
conn.Close();
DataListDataBind();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
this.DataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_ItemDataBound);
this.AspNetPager1.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.AspNetPager1_PageChanged);
this.btnExcel.Click += new System.EventHandler(this.btnExcel_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion public void DataListDataBind()
{
SqlConnection conn=new SqlConnection("server=192.168.16.10;database=softwaremanage;uid=sa;pwd=sa");
adapter=new SqlDataAdapter("select * from SoftwareInfo",conn);
ds=new DataSet();
adapter.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"SoftwareInfo");
DataGrid.DataSource=ds.Tables["SoftwareInfo"];
DataGrid.DataBind();
AspNetPager1.CustomInfoText = "总计:<font color='blue'><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>条记录";
AspNetPager1.CustomInfoText += " 当显示第<font color='red'><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>/<font color='blue'><b>" + AspNetPager1.PageCount.ToString() + "</b></font>页";
}
protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex=e.NewPageIndex;
DataListDataBind();
} private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(this.DropDownList1.SelectedValue=="sname")
{
this.DropDownList2.Items.Clear();
ListItem listItem1 = new ListItem();
listItem1.Text="包含";
listItem1.Value="like";
this.DropDownList2.Items.Add(listItem1);
ListItem listItem2 = new ListItem();
listItem2.Text="等于";
listItem1.Value="=";
this.DropDownList2.Items.Add(listItem2);
this.DropDownList2.Visible=true;
this.DropDownList3.Visible=false;
this.DropDownList4.Visible=false;
this.txtQuery.Visible=true;
}
if(this.DropDownList1.SelectedValue=="stype")
{ this.DropDownList2.Items.Clear();
ListItem listItem = new ListItem();
listItem.Text="等于";
listItem.Value="=";
this.DropDownList2.Items.Add(listItem);
this.DropDownList3.Visible=true;
this.DropDownList4.Visible=false;
this.txtQuery.Visible=false;
}
if(this.DropDownList1.SelectedValue=="department")
{ this.DropDownList2.Items.Clear();
ListItem listItem = new ListItem();
listItem.Text="等于";
listItem.Value="=";
this.DropDownList2.Items.Add(listItem);
this.DropDownList3.Visible=false;
this.DropDownList4.Visible=true;
this.txtQuery.Visible=false;
}
} private void btnQuery_Click(object sender, System.EventArgs e)
{
string sql = "";
if (this.DropDownList1.SelectedValue == "sname")
{
if (this.DropDownList2.SelectedValue == "=")
{
sql = "select * from SoftwareInfo where " + this.DropDownList1.SelectedValue + " " + this.DropDownList2.SelectedValue + " '" + this.txtQuery.Text + "' ";
}
else
{
sql = "select * from SoftwareInfo where "+ this.DropDownList1.SelectedValue +" like '%"+this.txtQuery.Text+"%' ";
}
}
if (this.DropDownList1.SelectedValue == "stype")
{
if (this.DropDownList2.SelectedValue == "=")
{
sql = "select * from SoftwareInfo where " + this.DropDownList1.SelectedValue + " " + this.DropDownList2.SelectedValue + " '" + this.DropDownList3.SelectedValue + "' ";
}
}
if (this.DropDownList1.SelectedValue == "department")
{
if (this.DropDownList2.SelectedValue == "=")
{
sql = "select * from SoftwareInfo where " + this.DropDownList1.SelectedValue + " " + this.DropDownList2.SelectedValue + " '" + this.DropDownList4.SelectedValue + "' ";
}
}
//重新绑定DataGrid
SqlConnection conn1=new SqlConnection("server=192.168.16.10;database=softwaremanage;uid=sa;pwd=sa");
adapter=new SqlDataAdapter(sql,conn1);
ds=new DataSet();
adapter.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"SoftwareInfo");
DataGrid.DataSource=ds.Tables["SoftwareInfo"];
DataGrid.DataBind();
AspNetPager1.CustomInfoText = "总计:<font color='blue'><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>条记录";
AspNetPager1.CustomInfoText += " 当显示第<font color='red'><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>/<font color='blue'><b>" + AspNetPager1.PageCount.ToString() + "</b></font>页";
} private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//添加客户端鼠标挪动时变色
if(e.Item.ItemIndex<0)
{
return;
}
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='pink'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
}
} private void btnExcel_Click(object sender, System.EventArgs e)
{
// this.DataGrid.AllowPaging=false;
this.DataGrid.AllowSorting=false;
this.DataGrid.DataBind();
Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8";
string FileFlow = DateTime.Now.ToShortDateString();
Response.AppendHeader("Content-Disposition","attachment;filename="+FileFlow+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
DataGrid.RenderControl(oHtmlTextWriter );
Response.Write(oStringWriter.ToString());
Response.End();
}
}
可以参考一下
重新获取符合条件的记录总数绑定到aspnetpager的RecordCount属性
然后把文本框的条件放到viewstate里面
绑定gridview多加一个判断就行了
if(viewstste["title"]==null)
{
模糊查询
}
else
{
正常查询
}
不明白的话加我QQ393559600