GridView 分页问题
GridView分页问题假如当我点5的时候跳到了5的页我有个按钮用了做统计的我点统计的时候应该刷新而GridView也该回到第一页撒而我这个还是始终在第5页1234567...
GridView 分页问题
假如当我点 5的时候 跳到了 5的页 我 有个按钮 用了做统计的
我点 统计的时候 应该刷新 而 GridView 也该回到第一页撒 而我这个 还是始终在 第5页
1234567890。。。。
我要 刷新的时候 回到第一页
我做的这个有 输出到 EXEL and wrod 的
关键代码
页面
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true"
CodeFile="TopBasicInformation.aspx.cs"
设置了 EnableEventValidation="false"
CS
//重载VerifyRenderingInServerForm方法,调用页面必须加入否则会提示错误
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string xiangmu1 = this.xiangmu.SelectedValue;
string dalou1 = this.dalou.SelectedValue;
int x = int.Parse(xiangmu1);
int d = int.Parse(dalou1);
baid(x, d);
this.GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
}
protected void btnView_Click(object sender, EventArgs e)
{
string xiangmu1 = this.xiangmu.SelectedValue;
string dalou1 = this.dalou.SelectedValue;
int x = int.Parse(xiangmu1);
int d = int.Parse(dalou1);
baid(x, d);
} 展开
假如当我点 5的时候 跳到了 5的页 我 有个按钮 用了做统计的
我点 统计的时候 应该刷新 而 GridView 也该回到第一页撒 而我这个 还是始终在 第5页
1234567890。。。。
我要 刷新的时候 回到第一页
我做的这个有 输出到 EXEL and wrod 的
关键代码
页面
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true"
CodeFile="TopBasicInformation.aspx.cs"
设置了 EnableEventValidation="false"
CS
//重载VerifyRenderingInServerForm方法,调用页面必须加入否则会提示错误
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string xiangmu1 = this.xiangmu.SelectedValue;
string dalou1 = this.dalou.SelectedValue;
int x = int.Parse(xiangmu1);
int d = int.Parse(dalou1);
baid(x, d);
this.GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
}
protected void btnView_Click(object sender, EventArgs e)
{
string xiangmu1 = this.xiangmu.SelectedValue;
string dalou1 = this.dalou.SelectedValue;
int x = int.Parse(xiangmu1);
int d = int.Parse(dalou1);
baid(x, d);
} 展开
2个回答
展开全部
我这里 有源代码 用的是 分页 控件 很好用的 就是 要下载 我把 代码 给你吧
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
<webdiyer:AspNetPager ID="Pager" runat="server" FirstPageText="首页"
LastPageText="尾页" NextPageText="下一页" onpagechanged="AspNetPager1_PageChanged"
PageSize="1" PrevPageText="上一页">
</webdiyer:AspNetPager>
</form>
</body>
</html>
c#代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class 分页 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
Wl wl = new Wl();
//分页核心代码
string sql = @"with OrdersTable as
(
select * ,ROW_NUMBER() OVER(ORDER BY picture ASC) as RowNumber
from [picture] where 1=1 )
select * from OrdersTable where RowNumber > {0} and RowNumber <= {1}";
sql = string.Format(sql, (Pager.CurrentPageIndex - 1) * Pager.PageSize, (Pager.CurrentPageIndex - 1) * Pager.PageSize + Pager.PageSize);//, query);
//
string recordcountstr = "select count(*) from [picture] ";
int recordcount = Convert.ToInt16(wl.ExecuteValue(recordcountstr));
//将满住条件的总的记录数给分页控件的RecordCount属性
Pager.RecordCount = recordcount;
GridView1.DataSource = wl.DataSetSql(sql, "news");
GridView1.DataBind();
Label2.Text = "总记录数:" + Pager.RecordCount.ToString() + "条; " + Pager.CurrentPageIndex.ToString() + "/" + Pager.PageCount.ToString();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bind();
}
}
最后 我写了 一个类库 放在 了 app_code里
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
///Wl 的摘要说明
/// </summary>
///
public class Wl
{
public Wl()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public PagedDataSource bind(string sql)//提供各种控件的数据库绑定
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
return pds;
}
public DataView binds(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["datawork"]);
//string sql = "select distinct province from [pro_city]";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0].DefaultView;
}
public DataTable bindss(string sql)//提供各种控件的数据库绑定
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
///提供数据插入 只针对 wl表 唯一字段 wl
public void insert(string wl)
{
///提供数据插入 只针对 wl表 唯一字段 wl
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
string sql="insert into wl(wl) values(@wl)";
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.Add("@wl", SqlDbType.NVarChar, 100);
com.Parameters["@wl"].Value = wl;
con.Open();
com.ExecuteNonQuery();
con.Close();
///提供数据插入 只针对 wl表 唯一字段 wl
}
///提供数据插入 只针对 wl表 唯一字段 wl
public string FormatString(string str)//防注入是攻击 可以将 html代码 直接输出
{
str = str.Replace("<br/>", " ");
str = str.Replace("<","<");
str = str.Replace(">",">");
return str;
}
public DataSet DataSetSql(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public DataSet DataSetSql(string sql, string table)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, table);
return ds;
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
con.Close();
}
}
public string ExecuteValue(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlCommand com = new SqlCommand(sql, con);
try
{
con.Open();
object ob = com.ExecuteScalar();
if (ob != null)
return ob.ToString();
else
return null;
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
com.Dispose();//释放资源
con.Close();
}
}
public int ExecuteSql(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlCommand com = new SqlCommand(sql, con);
try
{
con.Open();
com.ExecuteNonQuery();
return 1;
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
com.Dispose();//释放资源
con.Close();
}
}
}
里面用到的表
create table [picture]
(
picture nvarchar(50),
pictureAddress nvarchar(50)
)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
<webdiyer:AspNetPager ID="Pager" runat="server" FirstPageText="首页"
LastPageText="尾页" NextPageText="下一页" onpagechanged="AspNetPager1_PageChanged"
PageSize="1" PrevPageText="上一页">
</webdiyer:AspNetPager>
</form>
</body>
</html>
c#代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class 分页 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
Wl wl = new Wl();
//分页核心代码
string sql = @"with OrdersTable as
(
select * ,ROW_NUMBER() OVER(ORDER BY picture ASC) as RowNumber
from [picture] where 1=1 )
select * from OrdersTable where RowNumber > {0} and RowNumber <= {1}";
sql = string.Format(sql, (Pager.CurrentPageIndex - 1) * Pager.PageSize, (Pager.CurrentPageIndex - 1) * Pager.PageSize + Pager.PageSize);//, query);
//
string recordcountstr = "select count(*) from [picture] ";
int recordcount = Convert.ToInt16(wl.ExecuteValue(recordcountstr));
//将满住条件的总的记录数给分页控件的RecordCount属性
Pager.RecordCount = recordcount;
GridView1.DataSource = wl.DataSetSql(sql, "news");
GridView1.DataBind();
Label2.Text = "总记录数:" + Pager.RecordCount.ToString() + "条; " + Pager.CurrentPageIndex.ToString() + "/" + Pager.PageCount.ToString();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bind();
}
}
最后 我写了 一个类库 放在 了 app_code里
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
///Wl 的摘要说明
/// </summary>
///
public class Wl
{
public Wl()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public PagedDataSource bind(string sql)//提供各种控件的数据库绑定
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
return pds;
}
public DataView binds(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["datawork"]);
//string sql = "select distinct province from [pro_city]";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0].DefaultView;
}
public DataTable bindss(string sql)//提供各种控件的数据库绑定
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
///提供数据插入 只针对 wl表 唯一字段 wl
public void insert(string wl)
{
///提供数据插入 只针对 wl表 唯一字段 wl
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
string sql="insert into wl(wl) values(@wl)";
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.Add("@wl", SqlDbType.NVarChar, 100);
com.Parameters["@wl"].Value = wl;
con.Open();
com.ExecuteNonQuery();
con.Close();
///提供数据插入 只针对 wl表 唯一字段 wl
}
///提供数据插入 只针对 wl表 唯一字段 wl
public string FormatString(string str)//防注入是攻击 可以将 html代码 直接输出
{
str = str.Replace("<br/>", " ");
str = str.Replace("<","<");
str = str.Replace(">",">");
return str;
}
public DataSet DataSetSql(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public DataSet DataSetSql(string sql, string table)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, table);
return ds;
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
con.Close();
}
}
public string ExecuteValue(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlCommand com = new SqlCommand(sql, con);
try
{
con.Open();
object ob = com.ExecuteScalar();
if (ob != null)
return ob.ToString();
else
return null;
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
com.Dispose();//释放资源
con.Close();
}
}
public int ExecuteSql(string sql)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["wl"]);
SqlCommand com = new SqlCommand(sql, con);
try
{
con.Open();
com.ExecuteNonQuery();
return 1;
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
com.Dispose();//释放资源
con.Close();
}
}
}
里面用到的表
create table [picture]
(
picture nvarchar(50),
pictureAddress nvarchar(50)
)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询