【问题标题】:Show Pager row, but hide page numbers in Gridview显示寻呼机行,但在 Gridview 中隐藏页码
【发布时间】:2017-03-22 04:31:04
【问题描述】:

简单的问题。

我有一个 ASP.net GridView (VS2005),它有页码,但是当每页的行数少于最大行数 (

我可以强制显示 Pager 行,但我需要隐藏第 1 页,因为很明显我们在 PAGE ONE 上!

                <asp:GridView ID="gvFTUNSENT" runat="server" 
                    AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" AllowSorting="True" CssClass="gvCSS" Width="100%"
                    DataKeyNames="StudentID,StudentUnitID" DataSourceID="sdsFTUNSENT" 
                    GridLines="Vertical" AllowPaging="True" PageSize="10" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                    OnPreRender="GridView_PreRender" 
                    OnLoad="GridView_Load" 
                    OnRowDataBound="GridView_RowDataBound" >
                    <RowStyle Wrap="True" Height="48px" />
                    <Columns>
...blahblah
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle CssClass="cssPager" BackColor="#6B696B" ForeColor="White" HorizontalAlign="Left" Height="100%" />
                    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    <EmptyDataTemplate>
                        <asp:Label ID="lblNDF1" runat="server" Text="NO DATA FOUND" Font-Size="X-Large" Width="500px" style="text-align:center" />
                    </EmptyDataTemplate>
                    <EmptyDataRowStyle HorizontalAlign="Center" />
                </asp:GridView>

这里我强制显示寻呼机行...

protected void GridView_PreRender(object sender, EventArgs e)
{
    GridView gv = (GridView)sender;

    //keep showing pager line even if there is only one row of data
    GridViewRow gvr = (GridViewRow)gv.BottomPagerRow;
    if (gvr != null)
        gvr.Visible = true;
}

但我不想在那里看到第 1 页,所以我尝试了这个...

if (e.Row.RowType == DataControlRowType.Pager)
{
    //keep showing pager line even if there is only one row of data
    GridViewRow gvr = (GridViewRow)e.Row;
    if (gvr != null)
    {
        gvr.Visible = true;

        //...but hide page number if there is only one page
        if (gv.PageCount == 1)
        {
            gv.ShowFooter = true;
            gv.PagerSettings.Visible = false;
        }
    }
}

但它实际上隐藏了整个 Pager 行!不!我只想隐藏页码。

ShowFooter 看起来像 gridview 是一个封闭的盒子。但它仍然很丑。如果我可以保持 Pager 行显示并能够隐藏其中的任何内容,我宁愿不使用它。即..保持背景颜色不变。

还有其他想法吗?谢谢

【问题讨论】:

    标签: c# asp.net gridview aspxgridview pager


    【解决方案1】:

    试试类似的东西

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
        if (e.Row.RowType == DataControlRowType.Pager && GridView1.PageCount==1 )
        { e.Row.Style.Add("color", "white"); }
     }
    

    或者,如果你不想处理颜色,你可以试试这个(但我认为它不太健壮)

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    
        if (e.Row.RowType == DataControlRowType.Pager && GridView1.PageCount==1 )
        {
            var a = e.Row.Controls;
            if (a.Count>0 && a[0] is TableCell)
            {
                var b = a[0].Controls[0].Controls[0] as TableRow;
                if (b != null)
                {
                    //This is actually your page 1 text
                    b.Cells[0].Text = "";
                }
             }
         }
    }
    

    【讨论】:

    • 谢谢伙计。上一个版本有效,但是因为现在什么都没有,所以 Pager 行的高度会缩小,因为当有数字时,编译器会稍微扩展它以为页码腾出空间。我试过 Height 属性,但不太正确。
    • 实际上我选择了你的第二个答案,但我没有完全删除文本,而是更改了颜色代码以匹配背景。完美运行。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    相关资源
    最近更新 更多