GridView 实现分页问题
GridView 实现自定义分页的时候不知道为什么需要点两次才显示成功!大虾们帮忙看看。代码如下:
<%@ Page Language="C#" MasterPageFile="~/admin/MasterPage.master" AutoEventWireup="true" CodeFile="AdminView.aspx.cs" Inherits="admin_AdminView" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<center>
<table>
<tr>
<td style="width: 700px; height: 345px;" class="login2">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge"
BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="标题" SortExpression="Title" />
<asp:BoundField DataField="Gfrom" HeaderText="来源" SortExpression="Gfrom" />
<asp:BoundField DataField="Type" HeaderText="类型" SortExpression="Type" />
<asp:BoundField DataField="Datatime" HeaderText="添加时间" SortExpression="Datatime" />
<asp:BoundField DataField="YN" HeaderText="审核状态" SortExpression="YN" />
<asp:TemplateField HeaderText="审核/更新/删除">
<ItemTemplate>
<table>
<tr>
<td>
<a href="EditView.aspx?id=<%# Eval("ID") %>">更新</a>
<a href="CheckView.aspx?id=<%# Eval("ID") %>">审核</a>
<asp:LinkButton ID="del" runat="server" Text="删除" CommandName='<%# Eval("Title") %>' OnClientClick="return confirm('确实要执行此删除操作么?')" OnCommand="del_Command"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
<asp:label id="lblPageCount" Width="136px" BackColor="#deebff" Font-Size="10" Runat="server"></asp:label>
<asp:label id="lblCurrentIndex" Width="144px" BackColor="#deebff" Font-Size="10" Runat="server"></asp:label>
<asp:linkbutton id="btnFirst" Width="34px" BackColor="#deebff" Font-Size="10" Runat="server" CommandName="First" onclick="GridView1_PageIndexChanged">首页</asp:linkbutton>
<asp:linkbutton id="btnPrevious" Width="42px" BackColor="#deebff" Font-Size="10" Runat="server" CommandName="Previous" onclick="GridView1_PageIndexChanged">上一页</asp:linkbutton>
<asp:linkbutton id="btnNext" runat="Server" Width="50px" BackColor="#deebff" Font-Size="10" CommandName="Next" onclick="GridView1_PageIndexChanged">下一页</asp:linkbutton>
<asp:linkbutton id="btnEnd" Width="34px" BackColor="#deebff" Font-Size="10" Runat="server" CommandName="End" onclick="GridView1_PageIndexChanged">尾页</asp:linkbutton>
</td>
</tr>
</table>
</center>
</asp:Content>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_AdminView : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=127.0.0.1;uid=sa;pwd=sa;database=network");
protected void Page_Load(object sender, EventArgs e)
{
/*SqlDataAdapter da;
DataSet ds = new DataSet();
da = new SqlDataAdapter("select Title,Gfrom [Type],Datatime from gonggao ",conn);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();*/
SqlDataAdapter da = new SqlDataAdapter("select ID,Title,Gfrom,[Type],YN,Datatime from gonggao ", conn); ;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void del_Command(object sender, CommandEventArgs e)
{
string ty = e.CommandName.ToString().Trim();
SqlCommand cmd = new SqlCommand("delete from gonggao where Title='" + ty + "'", conn);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('信息删除成功');</scrript>");
Response.Redirect("AdminView.aspx");
conn.Close();
}
protected void update_Command(object sender,CommandEventArgs e)
{
Response.Redirect("EditView.aspx");
}
protected void Check_Command(object sender,CommandEventArgs e)
{
Response.Redirect("CheckView.aspx");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void ShowStats()
{
//显示当前第几页,因为从0开始计算,所以当前页要加1
lblCurrentIndex.Text="第 " + (GridView1.PageIndex + 1).ToString() + "/" + GridView1.PageCount + " 页";
// lblCurrentIndex.Text =
//显示总页数
// lblPageCount.Text="总共 " +DataGrid1.PageCount.ToString()+" 页";
// lblPageCount.Text="总共 " +DataGrid1.VirtualItemCount.ToString()+" 条记录";
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
ShowStats();
//direction表示当前的页的位置在什么地方
string direction = ((LinkButton)sender).CommandName.ToString();
switch (direction)
{
//表示向前翻页
case "Previous":
if (GridView1.PageIndex > 0)
{
GridView1.PageIndex -= 1;
}
break;
//表示向后翻页
case "Next":
if (GridView1.PageIndex < (GridView1.PageCount - 1))
{
//设置DataGrid控件的当前索引为下一页面
GridView1.PageIndex += 1;
}
break;
//最后页
case "End":
GridView1.PageIndex = (GridView1.PageCount - 1);
break;
default:
GridView1.PageIndex = 0;
break;
}
ShowStats();
}
}
--------------------编程问答-------------------- http://www.cnblogs.com/hongyuniu/archive/2008/05/23/1205865.html --------------------编程问答-------------------- aspNetPager分页控件 --------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SqlDataAdapter da = new SqlDataAdapter("select ID,Title,Gfrom,[Type],YN,Datatime from gonggao ", conn); ;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
} --------------------编程问答-------------------- asp.net的gridview控件就有分页功能。有标准件又何苦去开发新的控件? --------------------编程问答--------------------
楼上是否知道GridView自带的分页处理大批量数据的流程是怎么样的?效率如何? --------------------编程问答-------------------- 处理大数据还是用存储过程吧,我有一个不是用存储过程写的,你试试
PagedDataSource ps = new PagedDataSource();
int CurPage;
//共有商品是数
int rr = set.Tables["biao"].Rows.Count;
Label2.Text = "共有" + rr.ToString() + "种商品";
ps.DataSource = set.Tables["biao"].DefaultView;
ps.AllowPaging = true;//允许分页
ps.PageSize = 3;//显示几页
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
ps.CurrentPageIndex = CurPage - 1;
//显示1/13页
int ii = ps.PageCount;
Label1.Text = "页次: " + CurPage.ToString() + "/" + ii.ToString();
//控制下一页
if (!ps.IsLastPage)
{
LinkButton2.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
//控制上一页
if (!ps.IsFirstPage)
{
this.LinkButton3.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
//首页
LinkButton4.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + 1;
//尾页
LinkButton1.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + ii;
if (ps.IsLastPage)
{
LinkButton3.Enabled = true;
LinkButton2.Enabled = false;
LinkButton1.Enabled = false;
LinkButton4.Enabled = true;
}
if (ps.IsFirstPage)
{
LinkButton3.Enabled = false;
LinkButton2.Enabled = true;
LinkButton1.Enabled = true;
LinkButton4.Enabled = false;
}
if (!ps.IsFirstPage & !ps.IsLastPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton1.Enabled = true;
LinkButton4.Enabled = true;
}
DataList1.DataSource = ps;
DataList1.DataBind(); --------------------编程问答-------------------- 我用楼上的方法可以实现了,擦,哥们!你的代码的规范性好像也不行啊!
但是我就是想问我的问题出现在什么地方了!为什么要点击第二次才会显示正确的结果! --------------------编程问答-------------------- 我用楼上的方法可以实现了,擦,哥们!你的代码的规范性好像也不行啊!
==
楼上是我么?
但是我就是想问我的问题出现在什么地方了!为什么要点击第二次才会显示正确的结果!
==
没有判断回传,每次Page_Load都会数据绑定
补充:.NET技术 , C#