2009-12-19 3 views
0

J'ai implémenté avec succès mon GridView maintenant, mais, comme toujours, tout le cycle de vie ASP.NET me dérange. Je ne peux pas comprendre pourquoi cela ne fonctionne pas. Je suis lié OnPageIndexChanged du GridView en tant que tel:ASP.NET GridView, activation/désactivation des boutons après la pagination

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

Ceci est mon 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> 

debugger mon application et que le code est en cours d'exécution et fait la bonne chose, mais pour une raison quelconque quand le contrôle est rendu, les deux boutons sont toujours activés. Qu'est-ce que je fais mal ici?

Répondre

2

Si je vous, je coderez comme ça dans la méthode « GridView_PageIndexChanged »

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

Edit: Pouvez-vous aussi essayer d'ajouter un setter?

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

Editer: OK mon ami, j'ai finalement trouvé une solution. Peut-être pas très élégant, mais ça marche et je l'ai testé. Il y a quelques choses à prendre en compte: 1. Nous avons un bouton "Prev" et un bouton "Next" et nous devons gérer les événements "OnCommand" pour ceux puisque nous utilisons notre propre pager Template 2. Nous voudrions avoir à lier des données après que nous traitons notre événement OnCommand.

J'ai un List<String> statique que j'ai rempli au cours de GET avec des chaînes aléatoires (Courtesy: http://www.kivela.be/index.php/2007/06/19/how-to-generate-a-random-string-in-c-20/) et lié à ma grille. Vous pouvez ici remplacer votre propre source de données. De plus, nous devons modifier l'index de page de la grille manuellement dans notre événement OnCommand.

Voici ma grille 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> 

et est ici le 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

+0

Intéressant, honnêtement, je n'ai pas essayé le code. J'avais l'habitude de le définir comme je l'ai mentionné plus tôt. Essayez ça? – ram

+0

pouvez-vous poster votre code avant (aspx/ascx) pour votre grille? Laisse-moi regarder. De plus, reliez-vous les données à votre grille après la pagination? – ram

+0

@Ram: Je ne fais qu'une connexion explicite à la GridView une fois et c'est dans 'OnLoad' (si'! IsPostBack'). J'ai ajouté le 'ascx' à la question. –

0

Est-il possible votre CSS est la propriété enabled est?

J'ai dupliqué votre code sans CSS et cela fonctionne très bien pour moi.

Comment publier votre css?

+0

Désolé, mais je suis sûr que le CSS n'est pas le problème ici. –