在GridView控件的<Columns></Columns>之外加入代码:
<PagerTemplate>
<asp:LinkButton ID="lbtn_first" runat="server" CausesValidation="false" CommandArgument="First" CommandName="page">首页</asp:LinkButton>
<asp:LinkButton ID="lbtn_prev" runat="server" CausesValidation="false" CommandArgument="Prev" CommandName="page">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_next" runat="server" CausesValidation="false" CommandArgument="Next" CommandName="page">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_last" runat="server" CausesValidation="false" CommandArgument="Last" CommandName="page">最后一页</asp:LinkButton>
第<asp:Label ID="lb_page" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex+1 %>"></asp:Label>页
共<asp:Label ID="lb_pagecount" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
跳到<asp:TextBox ID="txt_page" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex+1 %>" Width="27px"></asp:TextBox>页
<asp:LinkButton ID="lb_go" runat="server" CausesValidation="false" CommandArgument="-1" CommandName="page" Text="GO"></asp:LinkButton>
</PagerTemplate>
在GridView事件分页之前PageIndexChanging里写代码:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView grv = (GridView)sender;
if (e.NewPageIndex < 0)
{
TextBox pageNum = (TextBox)grv.BottomPagerRow.FindControl("txt_page");
int pa = int.Parse(pageNum.Text.ToString());
if (pa <= 0)
{
grv.PageIndex = 0;
}
else
{
grv.PageIndex = pa - 1;
}
}
else
{
grv.PageIndex = e.NewPageIndex;
}
Bind();
}
分布之后的事件PageIndexChanged中写代码:
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
Bind();
}
数据绑定Bind():
public void Bind()
{
string sqlstr = "select * from Company_Quote";//Company_Quote数据库表名
DataSet ds = db.getds(sqlstr, "companyQuote");//"companyQuote"ds表名,自己取的
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
ds.Dispose();
}
asp与sql数据库连接并完成相关操作的类DB():
namespace Siiit.Sun.Web
{
public class DB
{
public DB()
{
}
//连接数据库
public SqlConnection GetConnection()
{
string constring = "Data Source=(local);Initial Catalog=SunDecoration;Integrated Security=true";
SqlConnection con = new SqlConnection(constring);
return con;
}
//执行command命令
public int getcmd(string sqlstr)
{
SqlConnection con = this.GetConnection();
con.Open();
SqlCommand cmd = new SqlCommand(sqlstr, con);
int n=cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
con.Dispose();
return n;
}
//执行DataSet命令
public DataSet getds(string sqlstr, string tablename)
{
SqlConnection con = this.GetConnection();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
da.Fill(ds, tablename);
return ds;
}
//执行sqldatareader命令
public SqlDataReader getread(string sqlstr)
{
SqlConnection con = this.GetConnection();
con.Open();
SqlCommand cmd = new SqlCommand(sqlstr, con);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
}
}
四叶草颖
- 粉丝: 5
- 资源: 6
会员权益专享
最新资源
- python基础学习资料+配套题目+答案详解
- 倍福Twincat2 Tc211x64Engineering-R3-2.11.2308
- 基于jsp+sql的bbs论坛系统源码
- 图片展示多肉植物图鉴-微信小程序设计源代码【含图文文档教程+源码导入教程+操作界面截图】
- 基于Struts2+Spring+Hibernate+Jsp+Mysql5的项目申报系统源码
- PySD 将Vensim模型转化为Python语言
- Netscan是扫描网络或计算机系统以识别任何潜在漏洞或安全威胁的过程 这可能包括扫描开放端口,检查软件或固件中的已知漏洞
- 拼车同城拼车(完整带PHP后台)-微信小程序设计源代码【含图文文档教程+源码导入教程+操作界面截图】
- 【IrobotQ3D】无人驾驶70码模板 程序
- CreateList.c
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


