Gridview分页,清空,跳转到
Gridview分页,清空,跳转到
----------------华丽的分节符------------------------------------------------------------------
************linkbutton的Enabled设置成false,仍然可以点击的解决办法************************************************************************
-----------------------------------------------------------------------------------------------------
可能是对Gridview生疏了,今天出来了效果,还是有点云里雾里!
不过上了代码,以防后面又忘记了!
- <asp:GridView ID="_gvGuest" runat="server" AutoGenerateColumns="False" CellPadding="4"
- ForeColor="#333333" GridLines="Both" ShowFooter="true" Width="100%" AllowPaging="true"
- PageSize="10" OnPageIndexChanging="_gvGuest_PageIndexChanging">
它里面有个<PagerTemplate></PagerTemplate>标签:
- <PagerTemplate>
- 当前第:<asp:Label ID="lblCurrentPage" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:Label>页
- |一共:<asp:Label ID="lblAllPage" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
- <asp:LinkButton ID="lnkFirstPage" runat="server" CommandName="Page" CommandArgument="First">第一页</asp:LinkButton>
- <asp:LinkButton ID="lnkUpPage" runat="server" CommandName="Page" CommandArgument="Prev">上一页</asp:LinkButton>
- <asp:LinkButton ID="lnkDownPage" runat="server" CommandName="Page" CommandArgument="Next">下一页</asp:LinkButton>
- <asp:LinkButton ID="lnkLastPage" runat="server" CommandName="Page" CommandArgument="Last">最后一页</asp:LinkButton>
- </PagerTemplate>
- <RowStyle BackColor="#EFF3FB" />
- <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
- <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
- <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
- <EditRowStyle BackColor="#2461BF" />
- <AlternatingRowStyle BackColor="White" />
- </asp:GridView>
ps:获取当前页和总共的页数,
- ((GridView)Container.Parent.Parent).PageIndex + 1
- ((GridView)Container.Parent.Parent).PageCount
而PagerTemplate自带的标记:First,Prev,Next,Last只需要绑定事件OnPageIndexChanging即可自动关联!
- protected void _gvGuest_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- _gvGuest.PageIndex = e.NewPageIndex;
- BindData();
- }
前台绑定:OnPageIndexChanging="_gvGuest_PageIndexChanging"
效果图:【没有美工,丑了点,(*^__^*) 嘻嘻】

补充:首页,尾页是否可以点击!
- <asp:LinkButton ID="lnkFirstPage" runat="server" CommandName="Page" CommandArgument="First"
- Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>">第一页</asp:LinkButton>
- <asp:LinkButton ID="lnkUpPage" runat="server" CommandName="Page" CommandArgument="Prev"
- Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>">上一页</asp:LinkButton>
- <asp:LinkButton ID="lnkDownPage" runat="server" CommandName="Page" CommandArgument="Next"
- Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != ((GridView)Container.Parent.Parent).PageCount - 1 %>">下一页</asp:LinkButton>
- <asp:LinkButton ID="lnkLastPage" runat="server" CommandName="Page" CommandArgument="Last"
- 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()来绑定!
- //格式化绑定:添加%
- protected void _gvGuest_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- //判断行为数据行
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- if (e.Row.Cells[6].Text.Length == 1)
- e.Row.Cells[6].Text += ".0%";
- else
- e.Row.Cells[6].Text += "%";
- if (e.Row.Cells[7].Text.Length == 1)
- e.Row.Cells[7].Text += ".0%";
- else
- e.Row.Cells[7].Text += "%";
- if (e.Row.Cells[8].Text.Length == 1)
- e.Row.Cells[8].Text += ".0%";
- else
- e.Row.Cells[8].Text += "%";
- }
- }
跳转页:TextBox或者DropDownList
- 跳转到:
- <asp:TextBox ID="txtNeedPage" Width="20px" runat="server" onkeyup='value=value.replace(/[^\d]/g,"") '
- onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
- Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:TextBox>
- <asp:LinkButton ID="lnkGoto" runat="server" CommandName="Page" CommandArgument="-2">Go</asp:LinkButton>
- 跳转到:
- <asp:DropDownList ID="ddlNeedPage" runat="server" AutoPostBack="true"
- OnSelectedIndexChanged="ddlNeedPage_SelectedIndexChanged">
- </asp:DropDownList>
对应的更改PageIndexChanging事件:
- //分页
- protected void _gvGuest_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- GridView gv = sender as GridView;
- int newPageIndex = 0;
- if (e.NewPageIndex == -3) //<span style="color:#ff0000;">【这里的规律是:跳转到(lnkGoto)的CommandArgument -1】</span>
- {
- TextBox txtGoto = null;
- GridViewRow gvRow = gv.BottomPagerRow;
- if (gvRow != null)
- {
- txtGoto = gvRow.FindControl("txtNeedPage") as TextBox;
- }
- if (txtGoto != null)
- {
- newPageIndex = Convert.ToInt32(txtGoto.Text) - 1;
- }
- }
- else
- {
- newPageIndex = e.NewPageIndex;
- }
- //防止输入负数
- newPageIndex = newPageIndex <= 0 ? 0 : newPageIndex;
- //防止越位
- newPageIndex = newPageIndex >= gv.PageCount ? gv.PageCount - 1 : newPageIndex;
- //得到新的PageIndex
- gv.PageIndex = newPageIndex;
- BindData();
- }
下拉框的首先在Bind()事件里添加绑定:
- int allPage = _gvGuest.PageCount;
- for (int i = 0; i < allPage; i++)
- {
- ListItem item = new ListItem();
- item.Text = (i + 1).ToString();
- item.Value = i.ToString();
- drp.Items.Add(item);
- }
- //textbox和drp的值一致
- drp.SelectedValue = Convert.ToString(_gvGuest.PageIndex);
PS:为了让drp和textbox与页数对应,在绑定之后加上让drp的selectValue一致就ok!
然后在下拉事件里:
- protected void ddlNeedPage_SelectedIndexChanged(object sender, EventArgs e)
- {
- DropDownList drop = sender as DropDownList;
- int newPageIndex = Convert.ToInt32(drop.SelectedValue.ToString());
- this._gvGuest.PageIndex = newPageIndex;
- BindData();
- }
最后的效果图:

假设:我们的_gvGuest跟UpdatePanel,UpdateProgress一起使用,为了能够让UpdateProgress达到提醒的作用,必定要清空_gvGuest!
jquery代码:
- function clearData() {
- //$("#<%=_gvGuest.ClientID %>").empty();
- $("#_gvGuest").empty();
- }
第二种没有母版页使用!
然后在button或者linkbutton的OnClientClick="return clearData();"
--补充于:2012年11月30日 16:01:42,欢迎补充!!!!!
————————————————共赢才是王道————————————————
虽然设置了linkbutton的Enabled为false,但是他的click还在,解决办法:
在_gvGuest_RowDataBound中设置linkbutton的OnClientClick为“”
- protected void _gvGuest_RowDataBound(object sender, GridViewRowEventArgs e)
- {if (e.Row.RowType == DataControlRowType.Pager)
- {
- LinkButton lnkFirstPage = e.Row.FindControl("lnkFirstPage") as LinkButton;
- if (lnkFirstPage.Enabled == false)
- lnkFirstPage.OnClientClick = "";
- LinkButton lnkUpPage = e.Row.FindControl("lnkUpPage") as LinkButton;
- if (lnkUpPage.Enabled == false)
- lnkUpPage.OnClientClick = "";
- LinkButton lnkDownPage = e.Row.FindControl("lnkDownPage") as LinkButton;
- if (lnkDownPage.Enabled == false)
- lnkDownPage.OnClientClick = "";
- LinkButton lnkLastPage = e.Row.FindControl("lnkLastPage") as LinkButton;
- if (lnkLastPage.Enabled == false)
- lnkLastPage.OnClientClick = "";
- }
- }
这样他就点不动了,哈哈!
浙公网安备 33010602011771号