Search This Blog

Friday, October 1, 2010

ASP.NET GridView, enabling/disabling buttons after paging

Some times we need to make enable or disable the gridview controls in the gridview paging. Here I'm going to do with buttons in the gridivew. I would code it like this in the "GridView_PageIndexChanged" method.

ASPX of the GridView


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



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


 
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(); 
     } 
 } 
 I hope this may help to some one....

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.