【问题标题】:Paging in Gridview not working 2nd page data not showing asp.net c#Gridview中的分页不起作用第二页数据不显示asp.net c#
【发布时间】:2016-05-16 12:42:53
【问题描述】:

目前我无法在单击它时查看我的 gridview 的第 2 页。我不知道为什么并且需要帮助。我按照指示做了 PageIndexChanging,设置了 AllowPaging="True" 但仍然无法解决这个问题。

HTML:

    <asp:GridView ID="SiteGridView" runat="server" CssClass="datagrid" GridLines="Vertical" 
                        AutoGenerateColumns="False" Width="100%" AllowPaging="True" AllowSorting="True" PageSize="10"
                        EnableModelValidation="True" DataKeyNames="promoId"  OnRowCommand="GvPage_RowCommand" 
                        OnPageIndexChanging="GvPage_PageIndexChanging" OnSorting="GvPage_Sorting" onRowdatabound="SiteGridView_RowDataBound">
                        <Columns>                            
                          <asp:TemplateField HeaderText="PromotionId">
                        <ItemTemplate>
                            <asp:Label ID="lblId2" runat="server" Text='<%# Bind("promoId") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle CssClass="grid_view_hide" />
                        <ItemStyle CssClass="grid_view_hide" />
                    </asp:TemplateField>
                            <asp:TemplateField HeaderText ="Membership Name" SortExpression="name" ItemStyle-Width="50%">
                                <ItemTemplate>
                                    <asp:Label ID="lblSiteName" runat="server" Text='<%# Bind("titlePromo") %>' />
                                </ItemTemplate>
                                <ItemStyle Width="35%" />
                            </asp:TemplateField>
 </Columns>
                    </asp:GridView>

代码隐藏:

  if (!Page.IsPostBack)
            {
                BindData();
                populateDDL();
                ViewState["sortDirection"] = ASCENDING;
                ViewState["sortExpr"] = "titlePromo";
            }

  public void BindData()
        {
            String[,] catNameArr = populateCatID();

            name = (String)Session["titlePromo"];

            categoryName = (String)Session["membershipType"];

            if (categoryName != null)
            {
                DropDownList1.SelectedValue = categoryName;

            }
            else
            {
                Session["titlePromo"] = "";
                Session["membershipType"] = "All";
                categoryName = "All";
                name = "";
                DataSet ds = dal.getEventList();
                DataView myDataView = new DataView(ds.Tables[0]);
                SiteGridView.DataSource = myDataView;
                SiteGridView.DataBind();
                int count = ds.Tables[0].Rows.Count;

                if (count == 0)
                {
                    Label6.Visible = false;
                    Label15.Text = "No record found";
                    Label15.ForeColor = System.Drawing.Color.Gray;
                }
                else if (count == 1)
                {
                    Label6.Visible = true;
                    Label6.Text = count.ToString();
                    Label15.Text = "Total record:";
                    Label15.ForeColor = System.Drawing.Color.Black;
                    successMsg.Visible = false;
                }
                else
                {
                    Label6.Visible = true;
                    Label6.Text = count.ToString();
                    Label15.Text = "Total record:";
                    Label15.ForeColor = System.Drawing.Color.Black;
                    successMsg.Visible = false;
                }

            }
            //if(gridview.row[i].column[1].text/tostring() == ddlcatid.item[j].value.tostring())

            if (name == null)
            {

                    DataSet ds = dal.getPrjMgrList(categoryName);
                    DataView myDataView = new DataView(ds.Tables[0]);
                    SiteGridView.DataSource = myDataView;
                    SiteGridView.DataBind();
                    txtSearch.Text = name;
                    int count = ds.Tables[0].Rows.Count;

                    if (count == 0)
                    {
                        //show no record message
                        Label6.Visible = false;
                        Label15.Text = "No record found";
                        Label15.ForeColor = System.Drawing.Color.Gray;
                        //successMsg.Text = "No records found for this search";
                        //successMsg.Visible = true;
                    }
                    else
                    { //hide no record message
                        if (count == 1)
                        {
                            Label6.Visible = true;
                            Label6.Text = count.ToString();
                            Label15.Text = "Total record:";
                            Label15.ForeColor = System.Drawing.Color.Black;
                            successMsg.Visible = false;
                        }
                        else
                        {
                            Label6.Visible = true;
                            Label6.Text = count.ToString();
                            Label15.Text = "Total records:";
                            Label15.ForeColor = System.Drawing.Color.Black;
                            successMsg.Visible = false;
                        }
                    }

            }
            //changes as of 15/11/2012
            else
            {
                DataSet ds = dal.getPrjMgrList(categoryName, name);
                DataView myDataView = new DataView(ds.Tables[0]);
                SiteGridView.DataSource = myDataView;
                SiteGridView.DataBind();
                int count = ds.Tables[0].Rows.Count;
                txtSearch.Text = name;

                if (count == 0)
                {
                    //show no record message
                    Label6.Visible = false;
                    Label15.Text = "No record found";
                    Label15.ForeColor = System.Drawing.Color.Gray;
                    //successMsg.Text = "No records found for this category";
                    //successMsg.Visible = true;
                }
                else
                { //hide no record message

                    if (count == 1)
                    {
                        Label6.Visible = true;
                        Label6.Text = count.ToString();
                        Label15.Text = "Total record:";
                        Label15.ForeColor = System.Drawing.Color.Black;
                        successMsg.Visible = false;
                    }
                    else
                    {
                        Label6.Visible = true;
                        Label6.Text = count.ToString();
                        Label15.Text = "Total records:";
                        Label15.ForeColor = System.Drawing.Color.Black;
                        successMsg.Visible = false;
                    }
                }
            }

            if (categoryName == "All")
            {
                DataSet ds = dal.getPrjMgrListAll(categoryName, name);
                DataView myDataView = new DataView(ds.Tables[0]);
                SiteGridView.DataSource = myDataView;
                SiteGridView.DataBind();
                int count = ds.Tables[0].Rows.Count;
                txtSearch.Text = name;

                if (count == 0)
                {
                    Label6.Visible = false;
                    Label15.Text = "No record found";
                    Label15.ForeColor = System.Drawing.Color.Gray;
                }
                else if (count == 1)
                {
                    Label6.Visible = true;
                    Label6.Text = count.ToString();
                    Label15.Text = "Total record:";
                    Label15.ForeColor = System.Drawing.Color.Black;
                    successMsg.Visible = false;
                }
                else
                {
                    Label6.Visible = true;
                    Label6.Text = count.ToString();
                    Label15.Text = "Total record:";
                    Label15.ForeColor = System.Drawing.Color.Black;
                    successMsg.Visible = false;
                }


            }


            for (int i = 0; i < SiteGridView.Rows.Count; i++)
            {
                for (int j = 0; j < DropDownList1.Items.Count; j++)
                {
                    if (((Label)SiteGridView.Rows[i].FindControl("lblId2")).Text == DropDownList1.Items[j].Value)
                    {
                        ((Label)SiteGridView.Rows[i].FindControl("lblId2")).Text = DropDownList1.Items[j].Text;
                    }
                }
            }
        }


  protected void GvPage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            SiteGridView.PageIndex = e.NewPageIndex;
            BindData();
        }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    首先,关于调试的一个建议:暂时注释掉所有的复杂性。只需检索启用分页的简单数据集。如果分页按预期工作,则开始重新增加复杂性,直到它失败。

    关于分页

    在任何PageIndexChanging 事件中不要这样做:

    SiteGridView.PageIndex = e.NewPageIndex;
    

    您正在手动执行服务器控件自动执行的操作。该值在那里,因此您可以检查几个条件:

    1. 确定是否需要取消分页事件

      // SiteGridView 是否处于编辑模式
      如果(SiteGridView.EditIndex > -1)
          e.Cancel = true;`
    2. 通过比较判断页面是在当前页面之前还是之后

      if(SiteGridView.PageIndex > e.NewPageIndex)
          // 向下翻页
      别的
          // 上一页
    3. 或者因为你有一些晦涩的分页算法而切换到其他页面:

      e.NewPageIndex = PageReturnedBySomeObscurePagingAlgorithm();

    关于页面生命周期

    在回发期间,就像您更改 gridview 页面时发生的那样,页面在每次回发时都会运行整个生命周期。

    因此,请确保您没有在多个位置绑定或手动设置其他可能会改变分页结果的 gridview 控件值。喜欢设置

    `SiteGridView.PageIndex = 1` 
    

    在某些事件发生 LifeCycle 中的 PageIndexChanged 事件之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2011-06-15
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 2019-12-03
      相关资源
      最近更新 更多