Gridview分页,清空,跳转到

 转自:http://blog.csdn.net/xb12369/article/details/8211555

Gridview分页,清空,跳转到

分类: C# Asp.net

----------------华丽的分节符------------------------------------------------------------------

************linkbutton的Enabled设置成false,仍然可以点击的解决办法************************************************************************

-----------------------------------------------------------------------------------------------------

 

可能是对Gridview生疏了,今天出来了效果,还是有点云里雾里!

 

不过上了代码,以防后面又忘记了!

 

 

[csharp] view plaincopy
 
  1. <asp:GridView ID="_gvGuest" runat="server" AutoGenerateColumns="False" CellPadding="4"  
  2.                             ForeColor="#333333" GridLines="Both" ShowFooter="true" Width="100%" AllowPaging="true"  
  3.                             PageSize="10" OnPageIndexChanging="_gvGuest_PageIndexChanging">  

它里面有个<PagerTemplate></PagerTemplate>标签:

 

 

[csharp] view plaincopy
 
  1. <PagerTemplate>  
  2.                                 当前第:<asp:Label ID="lblCurrentPage" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:Label>页  
  3.                                 |一共:<asp:Label ID="lblAllPage" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页  
  4.                                 <asp:LinkButton ID="lnkFirstPage" runat="server" CommandName="Page" CommandArgument="First">第一页</asp:LinkButton>  
  5.                                 <asp:LinkButton ID="lnkUpPage" runat="server" CommandName="Page" CommandArgument="Prev">上一页</asp:LinkButton>  
  6.                                 <asp:LinkButton ID="lnkDownPage" runat="server" CommandName="Page" CommandArgument="Next">下一页</asp:LinkButton>  
  7.                                 <asp:LinkButton ID="lnkLastPage" runat="server" CommandName="Page" CommandArgument="Last">最后一页</asp:LinkButton>  
  8.                             </PagerTemplate>  

[csharp] view plaincopy
 
  1. <RowStyle BackColor="#EFF3FB" />  
  2.                             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  3.                             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
  4.                             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
  5.                             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />  
  6.                             <EditRowStyle BackColor="#2461BF" />  
  7.                             <AlternatingRowStyle BackColor="White" />  
  8.                         </asp:GridView>  

 

 

ps:获取当前页和总共的页数,

 

[csharp] view plaincopy
 
  1. ((GridView)Container.Parent.Parent).PageIndex + 1  
[csharp] view plaincopy
 
  1. ((GridView)Container.Parent.Parent).PageCount  

 

而PagerTemplate自带的标记:First,Prev,Next,Last只需要绑定事件OnPageIndexChanging即可自动关联!

 

[csharp] view plaincopy
 
  1. protected void _gvGuest_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  2.         {  
  3.             _gvGuest.PageIndex = e.NewPageIndex;  
  4.             BindData();  
  5.         }  

前台绑定:OnPageIndexChanging="_gvGuest_PageIndexChanging"

 

效果图:【没有美工,丑了点,(*^__^*) 嘻嘻】

 

补充:首页,尾页是否可以点击!

 

 

[csharp] view plaincopy
 
  1. <asp:LinkButton ID="lnkFirstPage" runat="server" CommandName="Page" CommandArgument="First"  
  2.                                     Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>">第一页</asp:LinkButton>  
  3.                                 <asp:LinkButton ID="lnkUpPage" runat="server" CommandName="Page" CommandArgument="Prev"  
  4.                                     Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>">上一页</asp:LinkButton>  
  5.                                 <asp:LinkButton ID="lnkDownPage" runat="server" CommandName="Page" CommandArgument="Next"  
  6.                                     Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != ((GridView)Container.Parent.Parent).PageCount - 1 %>">下一页</asp:LinkButton>  
  7.                                 <asp:LinkButton ID="lnkLastPage" runat="server" CommandName="Page" CommandArgument="Last"  
  8.                                     Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != ((GridView)Container.Parent.Parent).PageCount - 1 %>">最后一页</asp:LinkButton>  

都是跟PageIndex做比较:首页就直接跟0做比较!尾页就跟PageCount做比较!

 

Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != ((GridView)Container.Parent.Parent).PageCount - 1 %>"

 

绑定行,绑定数据行!

 

如果我们需要给显示的数据添加%,我们可以用RowDataBind()来绑定!

 

[csharp] view plaincopy
 
  1. //格式化绑定:添加%  
  2.         protected void _gvGuest_RowDataBound(object sender, GridViewRowEventArgs e)  
  3.         {  
  4.             //判断行为数据行  
  5.             if (e.Row.RowType == DataControlRowType.DataRow)  
  6.             {  
  7.                 if (e.Row.Cells[6].Text.Length == 1)  
  8.                     e.Row.Cells[6].Text += ".0%";  
  9.                 else  
  10.                     e.Row.Cells[6].Text += "%";  
  11.   
  12.                 if (e.Row.Cells[7].Text.Length == 1)  
  13.                     e.Row.Cells[7].Text += ".0%";  
  14.                 else  
  15.                     e.Row.Cells[7].Text += "%";  
  16.   
  17.                 if (e.Row.Cells[8].Text.Length == 1)  
  18.                     e.Row.Cells[8].Text += ".0%";  
  19.                 else  
  20.                     e.Row.Cells[8].Text += "%";  
  21.             }  
  22.         }  

 

跳转页:TextBox或者DropDownList

 

[csharp] view plaincopy
 
  1. 跳转到:  
  2.                                 <asp:TextBox ID="txtNeedPage" Width="20px" runat="server" onkeyup='value=value.replace(/[^\d]/g,"") '  
  3.                                     onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"  
  4.                                     Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:TextBox>  
  5.                                 <asp:LinkButton ID="lnkGoto" runat="server" CommandName="Page" CommandArgument="-2">Go</asp:LinkButton>  
  6.   
  7.                                 跳转到:  
  8.                                 <asp:DropDownList ID="ddlNeedPage" runat="server" AutoPostBack="true"  
  9.                                 OnSelectedIndexChanged="ddlNeedPage_SelectedIndexChanged">  
  10.                                 </asp:DropDownList>  

对应的更改PageIndexChanging事件:

 

 

[csharp] view plaincopy
 
  1. //分页  
  2.         protected void _gvGuest_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  3.         {  
  4.             GridView gv = sender as GridView;  
  5.             int newPageIndex = 0;  
  6.             if (e.NewPageIndex == -3)  //<span style="color:#ff0000;">【这里的规律是:跳转到(lnkGoto)的CommandArgument -1】</span>  
  7.             {  
  8.                 TextBox txtGoto = null;  
  9.   
  10.                 GridViewRow gvRow = gv.BottomPagerRow;  
  11.                 if (gvRow != null)  
  12.                 {  
  13.                     txtGoto = gvRow.FindControl("txtNeedPage") as TextBox;  
  14.                 }  
  15.                 if (txtGoto != null)  
  16.                 {  
  17.                     newPageIndex = Convert.ToInt32(txtGoto.Text) - 1;  
  18.                 }  
  19.             }  
  20.             else  
  21.             {  
  22.                 newPageIndex = e.NewPageIndex;  
  23.             }  
  24.   
  25.             //防止输入负数  
  26.             newPageIndex = newPageIndex <= 0 ? 0 : newPageIndex;  
  27.             //防止越位  
  28.             newPageIndex = newPageIndex >= gv.PageCount ? gv.PageCount - 1 : newPageIndex;  
  29.   
  30.             //得到新的PageIndex  
  31.             gv.PageIndex = newPageIndex;  
  32.   
  33.             BindData();  
  34.   
  35.   
  36.         }  

下拉框的首先在Bind()事件里添加绑定:

 

 

[csharp] view plaincopy
 
  1. int allPage = _gvGuest.PageCount;  
  2.                 for (int i = 0; i < allPage; i++)  
  3.                 {  
  4.                     ListItem item = new ListItem();  
  5.                     item.Text = (i + 1).ToString();  
  6.                     item.Value = i.ToString();  
  7.                     drp.Items.Add(item);  
  8.                 }  
  9.   
  10.                 //textbox和drp的值一致  
  11.                 drp.SelectedValue = Convert.ToString(_gvGuest.PageIndex);  

PS:为了让drp和textbox与页数对应,在绑定之后加上让drp的selectValue一致就ok!

 

 

然后在下拉事件里:

 

[csharp] view plaincopy
 
  1. protected void ddlNeedPage_SelectedIndexChanged(object sender, EventArgs e)  
  2.         {  
  3.             DropDownList drop = sender as DropDownList;  
  4.             int newPageIndex = Convert.ToInt32(drop.SelectedValue.ToString());  
  5.   
  6.             this._gvGuest.PageIndex = newPageIndex;  
  7.             BindData();  
  8.         }  

最后的效果图:

 

 

 

 

假设:我们的_gvGuest跟UpdatePanel,UpdateProgress一起使用,为了能够让UpdateProgress达到提醒的作用,必定要清空_gvGuest

jquery代码:

 

[csharp] view plaincopy
 
  1. function clearData() {  
  2.             //$("#<%=_gvGuest.ClientID %>").empty();  
  3.             $("#_gvGuest").empty();  
  4.         }  
ps:第一种写法实在有母版页的情况下,否则报错!没有母版页也可以!

 

第二种没有母版页使用!

然后在button或者linkbutton的OnClientClick="return clearData();"

 

--补充于:2012年11月30日 16:01:42,欢迎补充!!!!!

————————————————共赢才是王道————————————————

 

虽然设置了linkbutton的Enabled为false,但是他的click还在,解决办法:

在_gvGuest_RowDataBound中设置linkbutton的OnClientClick为“”

 

[csharp] view plaincopy
 
  1. protected void _gvGuest_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.         {if (e.Row.RowType == DataControlRowType.Pager)  
  3.             {  
  4.                 LinkButton lnkFirstPage = e.Row.FindControl("lnkFirstPage") as LinkButton;  
  5.                 if (lnkFirstPage.Enabled == false)  
  6.                     lnkFirstPage.OnClientClick = "";  
  7.                 LinkButton lnkUpPage = e.Row.FindControl("lnkUpPage") as LinkButton;  
  8.                 if (lnkUpPage.Enabled == false)  
  9.                     lnkUpPage.OnClientClick = "";  
  10.                 LinkButton lnkDownPage = e.Row.FindControl("lnkDownPage") as LinkButton;  
  11.                 if (lnkDownPage.Enabled == false)  
  12.                     lnkDownPage.OnClientClick = "";  
  13.                 LinkButton lnkLastPage = e.Row.FindControl("lnkLastPage") as LinkButton;  
  14.                 if (lnkLastPage.Enabled == false)  
  15.                     lnkLastPage.OnClientClick = "";  
  16.             }  
  17.         }  

这样他就点不动了,哈哈!
posted @ 2014-10-14 10:05  devin2  阅读(162)  评论(0)    收藏  举报