Gridview分頁,清空,跳轉到

赤砂之蠍我愛羅發表於2012-11-22

----------------華麗的分節符------------------------------------------------------------------

************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)  //【這裡的規律是:跳轉到(lnkGoto)的CommandArgument -1】
            {
                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();
        }
ps:第一種寫法實在有母版頁的情況下,否則報錯!沒有母版頁也可以!

第二種沒有母版頁使用!

然後在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 = "";
            }
        }

這樣他就點不動了,哈哈!

相關文章