简体   繁体   English

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

[英]ASP.NET GridView, enabling/disabling buttons after paging

I have successfully implemented my GridView now, but, as always, the whole ASP.NET life cycle thing is bothering me. 我现在已经成功实现了GridView,但是与往常一样,整个ASP.NET生命周期的事情困扰着我。 I can't figure out why this doesn't work. 我不知道为什么这不起作用。 I have bound the GridView's OnPageIndexChanged as such: 我已经将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;
}

This is my ASCX: 这是我的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>

I debug my application and see that the code is being run and does the right thing but for some reason when the control is rendered, both of the buttons are always enabled. 我调试了我的应用程序,发现代码正在运行并且可以正常运行,但是由于某种原因,在呈现控件时,两个按钮始终处于启用状态。 What am I doing wrong here? 我在这里做错了什么?

If I were you, I would code it like this in the "GridView_PageIndexChanged" method 如果我是你,我会在“ GridView_PageIndexChanged”方法中像这样编码

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

Edit:Can you also try adding a setter ? 编辑:您还可以尝试添加二传手吗?

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

Edit: OK my friend, I finally worked out a solution. 编辑:好的,我的朋友,我终于找到了解决方案。 May be not very elegant,but it works and I tested it. 可能不是很优雅,但是它可以工作,我进行了测试。 There are a few things to take care of: 1. We are having a "Prev" and a "Next" button and we got to handle "OnCommand" events for those since we are using our own Pager Template 2. We would have to bind data after we handle our OnCommand event. 有几件事要注意:1.我们有一个“上一个”和“下一个”按钮,由于我们使用的是自己的Pager模板,我们必须为这些事件处理“ OnCommand”事件。2.我们必须在处理OnCommand事件后绑定数据。

I have a static List<String> which I populated during GET with random strings (Courtesy: http://www.kivela.be/index.php/2007/06/19/how-to-generate-a-random-string-in-c-20/ ) and bound them to my grid. 我有一个静态的List<String> ,我在GET期间使用随机字符串填充了该字符串(礼貌: http : //www.kivela.be/index.php/2007/06/19/how-to-generate-a-random-string -in-c-20 / )并将它们绑定到我的网格。 You can substitute your own datasource here.Also, we have to change the grid's page index manually in our OnCommand Event. 您可以在此处替换自己的数据源。此外,我们还必须在OnCommand事件中手动更改网格的页面索引。

Here is my aspx/ascx grid 这是我的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>

and here is the codebehind 这是背后的代码

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();

    }


}

Hope this helps 希望这可以帮助

Is there any chance your CSS is setting the enabled property? 您的CSS是否有可能设置enabled属性?

I duplicated your code without the CSS and it works fine for me. 我在没有CSS的情况下复制了您的代码,对我来说效果很好。

How about posting your css? 如何发布您的CSS?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM