首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Gridview控件的使用,解决方案

2012-05-24 
Gridview控件的使用,为什么我按条件查询完之后,查询完的数据在Gridview中显示出来,但我一点击下一页,Gridv

Gridview控件的使用,
为什么我按条件查询完之后,查询完的数据在Gridview中显示出来,但我一点击下一页,Gridview里显示的数据就不是我刚查询完的,而是数据库中所有的数据,也就是说下一页这类按钮不顶用,怎么解决这个问题,谢谢,

[解决办法]
你分页肯定不对。。。PageIndex 有木有值。。
[解决办法]
lz 你是用的 Gridview 自带的分页 还是你自己写的
[解决办法]
是用分页控件AspNetPager吗
贴代码出来看看
[解决办法]
那就是分页没写好
[解决办法]
PageIndex+1 然后重新绑定数据
[解决办法]
分页写得不对,写个方法BindGridView(int pageIndex),然后
在Page_Load事件里:
if(!IsPostBack)
BindGridView(20);

在GridView的PageIndexChanging事件里:
GridView1.PageIndex=e.NewPageIndex;
BindGridView(20);
[解决办法]
这是我以前收集的GridView分页方法给你:
方法一:
 关于GridView分页页码的讨论 
在GridView中实现分页的效果方法很简单,只需要在“GridView任务”对话框中进行设置就可以了。在“GridView任务”对话框中,选择“启用分页”命令,这样建立起简单的分页效果。

在使用“启用分页”命令的时候要注意两点。

(1) 是否允许分页

GridView的AllowPaging属性。AllowPaging:是否允许分页。如果AllowPaging=“true”就是允许分页。否则就是不允许使用分页。

(2) 每页记录数

GridView的PageSize属性。在GridView控件的属性中可以设置每页显示的数据记录的个数。默认情况下PageSize的值是10,也可以根据需要进行设置。

如果想要对分页编码进行设置的话,可以在HTML代码中为GridView控件添加分页导航条形式代码。也就是启用GridView的PagerSettings属性,在PagerSettings属性中可以设置根据需要设置Mode的值,来实现分页编码的显示效果。



<PagerSettings 

Mode = "NextPreviousFirstLast" 

FirstPageText = "第一页" 

LastPageText = "末页"> 

</PagerSettings> 

注意:

PagerSettings属性的Mode:Numeric,NextPrevious,NextPreviousFirstLast,NumericFirstLast。有这四种,可以根据不同需要进行不同的选择设置。

自动设置分页效果
现在想要在GridView控件上显示如下页码信息:总页数、当前页、首页、上一页、下一页、尾页。


 创建总页数

<asp:Label ID="Lab_PageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>

 创建但前页

<asp:Label ID="Lab_CurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>

创建首页

<asp:LinkButton ID="LBtn_FirstPage" runat="server" CommandArgument="First" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>

创建上一页

<asp:LinkButton ID="LBtn_PreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>

创建下一页

<asp:LinkButton ID="LBtn_NextPage" runat="server" CommandArgument="Next" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>

创建尾页

<asp:LinkButton ID="LBtn_LastPage" runat="server" CommandArgument="Last" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>

对应后台代码

public void GetDataSet()
{
string zhuangtmc;
zhuangtmc = Convert.ToString(DropDownList1.Text);


string sql;
sql = "Select L.LunWBH,L.LunWBT,Z.ZhuangTMC,L.ZhuCYHM,B.BianJM,L.TouGRQ From T_LunWXX L ,T_LunWZhT Z,T_BianJPL B ";
sql += "Where L.ZhuangTBH=Z.ZhuangTBH AND B.LunWBH=L.LunWBH AND";
sql += " B.BianJM='" + zhucyhm + "'";
if (zhuangtmc != "")
{
sql += " And Z.ZhuangTMC='" + zhuangtmc + "'";
}
sql += " Order By TouGRQ";
CommonDB = new Common();
GridView1.DataSource = CommonDB.DataSource(sql);
GridView1.DataBind();

//用lblCurrentIndex来显示当前页的页数。
LabelCurrentPage.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
//用LblPageCount来显示当前数据的总页数。
LabelPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";
//用LblrecordCount来显示数据的总条数。
LabelRecordCount.Text = "总共 " + CommonDB.DataSets(sql).Tables[0].Rows.Count.ToString() + " 条";

// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
//点击首页设定的值为1。
LinkButtonFirstPage.CommandName = "1";
//点击‘上一页’
LinkButtonPreviousPage.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
//点击‘下一页’
LinkButtonNextPage.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
//点击‘尾页’
LinkButtonLastPage.CommandName = GridView1.PageCount.ToString();
}

[解决办法]

方法二:
<asp:GridView>
<Columns>
</Columns>
<PagerTemplate>
<div style="text-align: right; color: Blue">
<asp:LinkButton ID="cmdFirstPage" runat="server" CommandName="Page" CommandArgument="First"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="cmdPreview" runat="server" CommandArgument="Prev" CommandName="Page"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">上页</asp:LinkButton>
第<asp:Label ID="lblcurPage" ForeColor="Blue" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1 %>'></asp:Label>页/共<asp:Label
ID="lblPageCount" ForeColor="blue" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label>页
<asp:LinkButton ID="cmdNext" runat="server" CommandName="Page" CommandArgument="Next"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">下页</asp:LinkButton>
<asp:LinkButton ID="cmdLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">尾页</asp:LinkButton>
&nbsp;
<asp:TextBox ID="txtGoPage" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1 %>'
Width="32px" CssClass="inputmini"></asp:TextBox>页<asp:Button ID="Button3" runat="server"
OnClick="Go_Click" Text="转到" />&nbsp;</div>
</PagerTemplate>
</asp:GridView>

 

 CS代码:



#region 转到触发方法
protected void Go_Click(object sender, EventArgs e)
{
GvData.PageIndex = int.Parse(((TextBox)GvData.BottomPagerRow.FindControl("txtGoPage")).Text) - 1;
BindData(); //重新绑定GridView
}
#endregion

#region 分页触发方法
protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GvData.PageIndex = e.NewPageIndex;
BindData(); //重新绑定GridView
}
#endregion

[解决办法]

方法三:
<PagerTemplate>
<table border="0px" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="height:23px; text-align:right;" >
<asp:LinkButton ID="cmdFirstPage" runat="server" CommandName="Page" CommandArgument="First"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="cmdPreview" runat="server" CommandArgument="Prev" CommandName="Page"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">上一页</asp:LinkButton>

<asp:LinkButton ID="cmdNext" runat="server" CommandName="Page" CommandArgument="Next"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="cmdLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">尾页</asp:LinkButton>
<asp:Label ID="LabSum" runat="server"></asp:Label>
位置:<asp:Label ID="LabPage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>/<asp:Label
ID="LabTotal" runat="server"></asp:Label>
转到<asp:DropDownList ID="PgLst" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PgLstData_SelectedIndexChanged" >
</asp:DropDownList>页
</td>
</tr>
</table>
</PagerTemplate>

热点排行