ASP.Gridview.Rows.Count给出错误的计数

本文关键字:错误 出错 Gridview Rows Count ASP | 更新日期: 2023-09-27 17:50:17

我有一个Gridview (BudgetGridView),默认情况下分页,每页30行。这个网格视图中的数据每年更新一次。为了做到这一点,我使用我在网上找到的一些非常光滑的代码(http://highoncoding.com/)对这个gridview的所有行进行编辑。作为编辑过程的一部分,我禁用了分页,这样用户就可以轻松地编辑和滚动整个网格视图,直到他们到达底部,并在一次操作中保存所有编辑。在这里禁用分页:

        if (myLabel == "  Edit All  ")
        {
            isEditMode = true;
            BudgetGridView.AllowPaging = false;
            EditAllButton.Text = "  Save Edits  ";
            BindBudgetGridView();
        }
在这一点上,gridview是完全可编辑的,它工作得很漂亮。当用户保存时,点击上面代码的"else",我遇到了这篇文章的主题问题。下面是else:
        else
        {
            isEditMode = false;
            EditAllButton.Text = "  Edit All  ";
            SaveData();  // Problem is here...
            BudgetGridView.AllowPaging = true;
            BindBudgetGridView();
        }
当上面对SaveData()进行调用时,它看起来像这样:
protected void SaveData()
    {
        // total count for an update loop
        int totalRowsCount = BudgetGridView.Rows.Count;

不是返回正确的计数(大约250行),而是给我一个值30,这是分页值,但是分页是禁用的。如果有帮助的话,下面是gridview的属性:

                <asp:GridView 
                    ID="BudgetGridView" runat="server"
                    HeaderStyle-ForeColor="#FFFFFF" 
                    ShowFooter="True" 
                    HeaderStyle-BackColor="#546E96"
                    FooterStyle-BackColor="#C3CFDD" 
                    AllowPaging="True" 
                    PageSize="30" 
                    OnPageIndexChanging="BudgetGridView_PageIndexChanging"
                    AlternatingRowStyle-BackColor="#FFFFCC"  
                    AutoGenerateColumns="False" 
                    DataKeyNames="KeyID"   
                    BackColor="White"
                    HorizontalAlign="Center" 
                    OnRowDeleting="BudgetGridView_RowDeleting"
                    EnableViewState="false">

有什么想法,为什么我得到30行计数,而不是完整的250左右?

ASP.Gridview.Rows.Count给出错误的计数

解决了!!

解决方案是在代码背后处理分页,并将其从aspx中删除。只要它在aspx中的GridView的属性中,我在代码后面所做的任何事情都没有任何影响。因此,我将aspx更改为:
                <asp:GridView 
                ID="BudgetGridView" runat="server"
                HeaderStyle-ForeColor="#FFFFFF" 
                ShowFooter="True" 
                HeaderStyle-BackColor="#546E96"
                FooterStyle-BackColor="#C3CFDD" 
                OnPageIndexChanging="BudgetGridView_PageIndexChanging"
                AlternatingRowStyle-BackColor="#FFFFCC"  
                AutoGenerateColumns="False" 
                DataKeyNames="KeyID"   
                BackColor="White"
                HorizontalAlign="Center" 
                OnRowDeleting="BudgetGridView_RowDeleting"
                EnableViewState="false">

…并将这些语句放在我需要它们的代码后面的地方:

            //kill pagination
        BudgetGridView.AllowPaging = false;
        BudgetGridView.PageSize = 5000;
        //resurrect pagination
        BudgetGridView.AllowPaging = true;
        BudgetGridView.PageSize = 30;

现在,我们正在用煤气做饭!!

谢谢你的帮助。

相关文章: