views:

792

answers:

2

I have successfully implemented my GridView now, but, as always, the whole ASP.NET life cycle thing is bothering me. I can't figure out why this doesn't work. I have bound the GridView's OnPageIndexChanged as such:

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:

<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?

+1  A: 

If I were you, I would code it like this in the "GridView_PageIndexChanged" method

(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.

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. You can substitute your own datasource here.Also, we have to change the grid's page index manually in our OnCommand Event.

Here is my aspx/ascx grid

    <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

ram
Interesting,truthfully I did not try the code. I used to set it like how I had mentioned earlier. Try that ?
ram
can you post your code front (aspx/ascx) for your grid ? Let me take a look. Plus, are you binding data to your grid again, after pagination ?
ram
@Ram: I'm only databinding the GridView explicitly once and that's in `OnLoad` (if `!IsPostBack`). I added the `ascx` to the question.
Deniz Dogan
Thanks a lot, Ram, I'd give you 10 times "accepted" if I could for this great effort. Sad though that we can't seem to sort out the problem with OnPageIndexChanged. :(
Deniz Dogan
A: 

Is there any chance your CSS is setting the enabled property?

I duplicated your code without the CSS and it works fine for me.

How about posting your css?

Justin C
Sorry, but I'm positive the CSS is not the issue here.
Deniz Dogan