繁体   English   中英

Gridview中的分页不起作用第二页数据不显示asp.net C#

[英]Paging in Gridview not working 2nd page data not showing asp.net c#

当前,单击它无法查看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();
        }

首先,建议进行调试:暂时注释掉所有复杂性。 只需检索启用分页的简单数据集即可。 如果分页按预期工作,则开始增加复杂性,直到失败为止。

关于分页

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

SiteGridView.PageIndex = e.NewPageIndex;

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

  1. 确定是否需要取消寻呼事件

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

      如果(SiteGridView.PageIndex> e.NewPageIndex)\n     // 向下翻页\n 其他\n     //向上翻页 
  3. 或由于其他模糊的分页算法而切换到其他页面:

      e.NewPageIndex = PageReturnedBySomeObscurePagingAlgorithm(); 

关于页面生命周期

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

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

`SiteGridView.PageIndex = 1` 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM