datagrid显示查找内容的问题 刚开始接触asp.net,做个小东西遇到点问题用datagrid显示数据,用关键字查找并显示,自动分页。问题是查找后的分页不管用,点下一页又显示全部数据的第二页了,而不是查找后内容的第二页。是哪里的问题呢?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 look my article:http://blog.csdn.net/ChengKing/archive/2005/09/18/483735.aspx 肯定是搜索语句每次都被初始化的问题,估计页面每次PostBack你会执行一次 搜索字符串=搜索默认数据 的语句。 前台:<%@ Page language="c#" Codebehind="DataGridPager.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.DataGridPager" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" runat="server"> <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 480px; POSITION: absolute; TOP: 24px; HEIGHT: 279px" cellSpacing="1" cellPadding="1" width="480" border="0"> <tr> <td align="center"> <h2>DataGrid分页的例子</h2> </td> </tr> <TR> <TD style="HEIGHT: 206px" vAlign="top"><asp:datagrid id="DataGridPage" runat="server" GridLines="Horizontal" BackColor="White" BorderStyle="None" Width="480px" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" BorderColor="#E7E7FF" BorderWidth="1px" CellPadding="3" Font-Name="Verdana" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee" HorizontalAlign="Center" AutoGenerateColumns="False"> <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle> <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle> <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle> <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle> <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle> <Columns> <asp:BoundColumn DataField="lastname" HeaderText="lastname"> <HeaderStyle Width="480px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="firstname" HeaderText="firstname"></asp:BoundColumn> </Columns> <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle> </asp:datagrid></TD> </TR> <TR> <TD align="right"><asp:label id="lblPageCount" runat="server"></asp:label><asp:label id="lblCurrentIndex" runat="server"></asp:label><asp:linkbutton id="btnFirst" onclick="PageButtonClick" runat="server" Font-Name="verdana" CommandArgument="0">最首页</asp:linkbutton><asp:linkbutton id="btnPrev" onclick="PageButtonClick" runat="server" CommandArgument="prev">前一页</asp:linkbutton><asp:linkbutton id="btnNext" onclick="PageButtonClick" runat="server" CommandArgument="next">下一页</asp:linkbutton><asp:linkbutton id="btnLast" onclick="PageButtonClick" runat="server" CommandArgument="last">最后页</asp:linkbutton></TD> </TR> </TABLE> </form> </body></HTML>后台:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Data.SqlClient;using System.Configuration;namespace CommonFunction{ /// <summary> /// testDataGridPager 的摘要说明。 /// </summary> public class DataGridPager : System.Web.UI.Page { protected System.Web.UI.WebControls.Label lblPageCount; protected System.Web.UI.WebControls.Label lblCurrentIndex; protected System.Web.UI.WebControls.LinkButton btnFirst; protected System.Web.UI.WebControls.LinkButton btnPrev; protected System.Web.UI.WebControls.LinkButton btnNext; protected System.Web.UI.WebControls.DataGrid DataGridPage; protected System.Web.UI.WebControls.LinkButton btnLast; private void Page_Load(object sender, System.EventArgs e) { //页面初试化时进行数据绑定 if(!IsPostBack) BindGrid(); } //显示当前分页信息 private void ShowStats() { //显示当前页面是第几页 lblCurrentIndex.Text = "第 " + (DataGridPage.CurrentPageIndex + 1).ToString() + " 页"; //显示总页数 lblPageCount.Text = "总共 " + DataGridPage.PageCount.ToString() + " 页"; } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.DataGridPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridPage_Page); this.Load += new System.EventHandler(this.Page_Load); } #endregion //分别处理”最首页“、“前一页”、“下一页”和“最后页”四个按钮单击时设置DataGrid控件的当前页的索引 public void PageButtonClick(object sender, EventArgs e) { //取得按钮单击时传递的命令参数 string arg = ((LinkButton)sender).CommandArgument.ToString(); switch(arg) { //如果点击的是“下一页” case "next": //如果当前页不是最后一页 if (DataGridPage.CurrentPageIndex < (DataGridPage.PageCount - 1)) { //设置DataGrid控件的当前页索引为下一页面 DataGridPage.CurrentPageIndex += 1; } break; //如果点击的是“前一页” case "prev": //如果当前页不是首页 if (DataGridPage.CurrentPageIndex > 0) { //设置DataGrid控件的当前页索引为上一页面 DataGridPage.CurrentPageIndex -= 1; } break; //如果点击的是“最后页” case "last": //设置当前页的索引为最后一页 DataGridPage.CurrentPageIndex = (DataGridPage.PageCount - 1); break; //默认为”最首页“ default: //设置当前页的索引为首页 DataGridPage.CurrentPageIndex = System.Convert.ToInt32(arg); break; } BindGrid(); ShowStats(); } private void BindGrid() { //定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的 SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString()); //创建数据适配器对象 SqlDataAdapter da = new SqlDataAdapter("select employeeid,lastname,firstname from employees",cnn); //创建DataSet对象 DataSet ds = new DataSet(); try { //填充数据集 da.Fill(ds, "testTable"); //进行数据绑定 DataGridPage.DataSource = ds; DataGridPage.DataBind(); } catch(Exception error) { Response.Write(error.ToString()); } } public void DataGridPage_Page(object sender, DataGridPageChangedEventArgs e) { //设置DataGrid当前页的索引值为用户选择的页的索引 DataGridPage.CurrentPageIndex = e.NewPageIndex; //重新绑定数据 BindGrid(); //显示当前分页信息 ShowStats(); } }} 页面加载之前的事件是哪个? web服务器和数据库服务器有时差,应该怎么解决? 关于类里面属性等一些问题 找个博客换皮肤的例子 怎样显示带有<script>的内容?谢谢! 微创的面试题?? 去北京发展好嘛? C#代码中如何调用web.config里的数据连接 散分100分,只求意见,今明两天来者有分!意见中肯者大大奖励哦 连接Acess出问题了----测试连接线显示“测试连接失败,因为初始化提供程序时发生错误。不能使用";文件正在使用中” datagrid问题 再线等!!!如何用表格从数据库中取数据并生成树状视图?
http://blog.csdn.net/ChengKing/archive/2005/09/18/483735.aspx
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 480px; POSITION: absolute; TOP: 24px; HEIGHT: 279px"
cellSpacing="1" cellPadding="1" width="480" border="0">
<tr>
<td align="center">
<h2>DataGrid分页的例子</h2>
</td>
</tr>
<TR>
<TD style="HEIGHT: 206px" vAlign="top"><asp:datagrid id="DataGridPage" runat="server" GridLines="Horizontal" BackColor="White" BorderStyle="None"
Width="480px" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" BorderColor="#E7E7FF"
BorderWidth="1px" CellPadding="3" Font-Name="Verdana" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee"
HorizontalAlign="Center" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="lastname" HeaderText="lastname">
<HeaderStyle Width="480px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="firstname" HeaderText="firstname"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:datagrid></TD>
</TR>
<TR>
<TD align="right"><asp:label id="lblPageCount" runat="server"></asp:label><asp:label id="lblCurrentIndex" runat="server"></asp:label><asp:linkbutton id="btnFirst" onclick="PageButtonClick" runat="server" Font-Name="verdana" CommandArgument="0">最首页</asp:linkbutton><asp:linkbutton id="btnPrev" onclick="PageButtonClick" runat="server" CommandArgument="prev">前一页</asp:linkbutton><asp:linkbutton id="btnNext" onclick="PageButtonClick" runat="server" CommandArgument="next">下一页</asp:linkbutton><asp:linkbutton id="btnLast" onclick="PageButtonClick" runat="server" CommandArgument="last">最后页</asp:linkbutton></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
后台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
/// <summary>
/// testDataGridPager 的摘要说明。
/// </summary>
public class DataGridPager : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.DataGrid DataGridPage;
protected System.Web.UI.WebControls.LinkButton btnLast;
private void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
BindGrid();
}
//显示当前分页信息
private void ShowStats()
{
//显示当前页面是第几页
lblCurrentIndex.Text = "第 " + (DataGridPage.CurrentPageIndex + 1).ToString() + " 页";
//显示总页数
lblPageCount.Text = "总共 " + DataGridPage.PageCount.ToString() + " 页";
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGridPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridPage_Page);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
//分别处理”最首页“、“前一页”、“下一页”和“最后页”四个按钮单击时设置DataGrid控件的当前页的索引
public void PageButtonClick(object sender, EventArgs e)
{
//取得按钮单击时传递的命令参数
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
//如果点击的是“下一页”
case "next":
//如果当前页不是最后一页
if (DataGridPage.CurrentPageIndex < (DataGridPage.PageCount - 1))
{
//设置DataGrid控件的当前页索引为下一页面
DataGridPage.CurrentPageIndex += 1;
}
break;
//如果点击的是“前一页”
case "prev":
//如果当前页不是首页
if (DataGridPage.CurrentPageIndex > 0)
{
//设置DataGrid控件的当前页索引为上一页面
DataGridPage.CurrentPageIndex -= 1;
}
break;
//如果点击的是“最后页”
case "last":
//设置当前页的索引为最后一页
DataGridPage.CurrentPageIndex = (DataGridPage.PageCount - 1);
break;
//默认为”最首页“
default:
//设置当前页的索引为首页
DataGridPage.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
} private void BindGrid()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select employeeid,lastname,firstname from employees",cnn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds, "testTable");
//进行数据绑定
DataGridPage.DataSource = ds;
DataGridPage.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
} public void DataGridPage_Page(object sender, DataGridPageChangedEventArgs e)
{
//设置DataGrid当前页的索引值为用户选择的页的索引
DataGridPage.CurrentPageIndex = e.NewPageIndex;
//重新绑定数据
BindGrid();
//显示当前分页信息
ShowStats();
}
}
}