ASP.NET GridView,在分页后启用/禁用按钮 [英] ASP.NET GridView, enabling/disabling buttons after paging

查看:196
本文介绍了ASP.NET GridView,在分页后启用/禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre $ protected void GridView_PageIndexChanged(object sender,EventArgs e)
{
//启用/禁用上一个/下一个按钮。
LinkBut​​ton btnNextPage =(LinkBut​​ton)gvTable.BottomPagerRow.FindControl(btnNextPage);
LinkBut​​ton btnPreviousPage =(LinkBut​​ton)gvTable.BottomPagerRow.FindControl(btnPreviousPage);
btnNextPage.Enabled = false;
btnPreviousPage.Enabled = false;
}

这是我的ASCX:

 < asp:GridView ID =gvTablerunat =serverShowHeader =truePageSize =1
AllowPaging =trueAllowSorting = trueDataSourceID =dsLinqActivities
AutoGenerateColumns =falseOnRowDataBound =GridView_DataBound
OnPageIndexChanged =GridView_PageIndexChanged>
<列>
< asp:BoundField DataField =EditedHeaderText =Date/>
< asp:BoundField DataField =StatusHeaderText =Status/>
< asp:BoundField DataField =Activity/>
< /列>
< PagerSettings Position =BottomVisible =true/>
< PagerStyle CssClass =pager/>
< PagerTemplate>
< asp:LinkBut​​ton ID =btnPreviousPageclass =navbtn prev left
runat =serverCommandName =PageCommandArgument =Prev>
< span>较新的活动< / span>< / asp:LinkBut​​ton>
< asp:LinkBut​​ton ID =btnNextPageclass =navbtn next right
runat =serverCommandName =PageCommandArgument =Next>
< span>旧版活动< / span>< / asp:LinkBut​​ton>
< / PagerTemplate>
< / asp:GridView>

我调试我的应用程序并查看代码正在运行并且执行正确的操作,但由于某种原因当呈现控件时,两个按钮始终处于启用状态。我在这里做错了什么?

解决方案

如果我是你,我会在GridView_PageIndexChanged / p>

 (gvTable.BottomPagerRow.FindControl(btnNextPage)as LinkBut​​ton).Enabled = true / false; 

编辑:您还可以尝试添加setter吗?

  set 
{
gvTable.BottomPagerRow.FindControl(btnNextPage)as LinkBut​​ton = value;
}

编辑:好的我的朋友,我终于找出了一个解决方案。可能不是很优雅,但它的工作原理和我测试它。有几件事需要注意:
1.我们有一个Prev和一个Next按钮,因为我们正在使用我们自己的Pager Template $ b,所以我们需要处理OnCommand事件$ b 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 =GridView1runat =serverOnRowDataBound =GridView_DataBound
AllowPaging =truePagerSettings -Mode =NextPreviousPagerSettings-Position =BottomPageSize =10
OnPageIndexChanged =GridView_PageIndexChanged>
< PagerSettings Position =BottomVisible =true/>
< PagerStyle CssClass =pager/>
< PagerTemplate>
< asp:LinkBut​​ton ID =btnPreviousPageOnCommand =ChangePage
runat =serverCommandName =PrevText =prev>
< / asp:LinkBut​​ton>
< asp:LinkBut​​ton ID =btnNextPageOnCommand =ChangePage
runat =serverCommandName =NextText =next>
< / asp:LinkBut​​ton>
< / PagerTemplate>

< / asp:GridView>

这里是代码隐藏



<$ p $公共分部类TestPage:System.Web.UI.Page
{
private static Random _random = new Random();
static列表< 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();



$ b private void SetPageNumbers()
{
if(GridView1.PageIndex == 0)
{
(GridView1.BottomPagerRow.FindControl(btnPreviousPage)as LinkBut​​ton).Enabled = false;


$ b $ if(GridView1.PageIndex == GridView1.PageCount-1)
{
(GridView1.BottomPagerRow.FindControl(btnNextPage))作为LinkBut​​ton).Enabled = false;



$ b protected void ChangePage(object sender,CommandEventArgs e)
{

switch(e.CommandName )
{
casePrev:
GridView1.PageIndex = GridView1.PageIndex - 1;
休息;

caseNext:
GridView1.PageIndex = GridView1.PageIndex + 1;
休息;
}
GridView1.DataSource = lst;
GridView1.DataBind();
SetPageNumbers();


$ b public static string RandomString(int size)
{

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

// alfabet中的26个字母,ascii + 65中的大写字母
(Convert.ToInt32(Math.Floor(26 * _random.NextDouble()+ 65))));

}
return builder.ToString();

}


}

希望这有助于


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?

解决方案

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

这篇关于ASP.NET GridView,在分页后启用/禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆