ASP.NET GridView,在分页后启用/禁用按钮

时间:2009-12-19 19:10:22

标签: c# asp.net gridview user-controls pagination

我现在已经成功实现了我的GridView,但是,一如既往,整个ASP.NET生命周期的事情都困扰着我。我无法弄清楚为什么这不起作用。我已经将GridView的OnPageIndexChanged绑定为:

protected void GridView_PageIndexChanged(object sender, EventArgs e)
{
    // Enable/disable the previous/next buttons.
    LinkButton btnNextPage = (LinkButton)gvTable.BottomPagerRow.FindControl("btnNextPage");
    LinkButton btnPreviousPage = (LinkButton)gvTable.BottomPagerRow.FindControl("btnPreviousPage");
    btnNextPage.Enabled = false;
    btnPreviousPage.Enabled = false;
}

这是我的ASCX:

<asp:GridView ID="gvTable" runat="server" ShowHeader="true" PageSize="1" 
  AllowPaging="true" AllowSorting="true" DataSourceID="dsLinqActivities"
  AutoGenerateColumns="false" OnRowDataBound="GridView_DataBound"
  OnPageIndexChanged="GridView_PageIndexChanged">
  <Columns>
    <asp:BoundField DataField="Edited" HeaderText="Date" />
    <asp:BoundField DataField="Status" HeaderText="Status" />
    <asp:BoundField DataField="Activity" />
  </Columns>
  <PagerSettings Position="Bottom" Visible="true" />
  <PagerStyle CssClass="pager" />
  <PagerTemplate>
    <asp:LinkButton ID="btnPreviousPage" class="navbtn prev left"
      runat="server" CommandName="Page" CommandArgument="Prev">
      <span>Newer activities</span></asp:LinkButton>
    <asp:LinkButton ID="btnNextPage" class="navbtn next right"
      runat="server" CommandName="Page" CommandArgument="Next">
      <span>Older activities</span></asp:LinkButton>
  </PagerTemplate>
</asp:GridView>

我调试我的应用程序,看到代码正在运行并做正确的事情,但出于某种原因,当呈现控件时,两个按钮始终都是启用的。我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

如果我是你,我会在“GridView_PageIndexChanged”方法中将其编码为

(gvTable.BottomPagerRow.FindControl("btnNextPage") as LinkButton).Enabled = true/false;

编辑:你还可以尝试添加一个setter吗?

set
{
 gvTable.BottomPagerRow.FindControl("btnNextPage") as LinkButton  =value;
}

编辑:好的,我的朋友,我终于找到了解决方案。可能不是很优雅,但它有效,我测试了它。有几件事需要注意: 1.我们有一个“上一个”和一个“下一个”按钮,我们必须处理“OnCommand”事件,因为我们使用自己的寻呼机模板 2.在处理OnCommand事件后,我们必须绑定数据。

我有一个静态List<String>我在GET期间用随机字符串填充(Courtesy:http://www.kivela.be/index.php/2007/06/19/how-to-generate-a-random-string-in-c-20/)并将它们绑定到我的网格。您可以在此替换您自己的数据源。此外,我们必须在OnCommand事件中手动更改网格的页面索引。

这是我的aspx / ascx网格

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView_DataBound" 
    AllowPaging="true" PagerSettings-Mode="NextPrevious" PagerSettings-Position="Bottom" PageSize="10"
  OnPageIndexChanged="GridView_PageIndexChanged">
    <PagerSettings Position="Bottom" Visible="true" />
    <PagerStyle CssClass="pager" />
    <PagerTemplate>
      <asp:LinkButton ID="btnPreviousPage" OnCommand="ChangePage"
        runat="server" CommandName="Prev"   Text="prev">
        </asp:LinkButton>
      <asp:LinkButton ID="btnNextPage" OnCommand="ChangePage"
        runat="server" CommandName="Next"  Text="next">
        </asp:LinkButton>
    </PagerTemplate>

  </asp:GridView>

这是代码隐藏

public partial class TestPage : System.Web.UI.Page 
{
    private static Random _random = new Random();
    static List<string> lst = new List<string>();
    protected void Page_Load(object sender, EventArgs e) 
    {


        if (!Page.IsPostBack)
        {
            for (int i = 1; i <= 30; i++)
            {
                lst.Add(RandomString(i));
            }

            GridView1.DataSource = lst;
            GridView1.DataBind();
            SetPageNumbers();
        }

    }

    private void SetPageNumbers()
    {
        if (GridView1.PageIndex == 0)
        {
            (GridView1.BottomPagerRow.FindControl("btnPreviousPage")as LinkButton).Enabled = false;

         }

        if(GridView1.PageIndex ==GridView1.PageCount-1)
        {
            (GridView1.BottomPagerRow.FindControl("btnNextPage") as LinkButton).Enabled = false; 
        }

    }

    protected void ChangePage(object sender, CommandEventArgs e)
    {

        switch (e.CommandName)
        {
            case "Prev":
                GridView1.PageIndex = GridView1.PageIndex - 1;
                break;

            case "Next":
                GridView1.PageIndex = GridView1.PageIndex + 1;
                break;
        }
        GridView1.DataSource = lst;
        GridView1.DataBind();
        SetPageNumbers();
    }


    public static string RandomString(int size)
    {

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < size; i++)
        {

            //26 letters in the alfabet, ascii + 65 for the capital letters
            builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * _random.NextDouble() + 65))));

        }
        return builder.ToString();

    }


}

希望这有帮助

答案 1 :(得分:0)

您的CSS是否有可能设置启用的属性?

我没有CSS复制你的代码,它对我来说很好。

发布你的CSS怎么样?