简体   繁体   English

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

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

Currently I am unable to view page 2 of my gridview upon clicking it. 当前,单击它无法查看gridview的页面2。 I am not sure why and needed help. 我不确定为什么,需要帮助。 I did PageIndexChanging as told, set AllowPaging="True" but still unable to solve this. 我按照提示进行了PageIndexChanging ,设置AllowPaging =“ True”,但仍然无法解决。

Html: 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>

CodeBehind: 代码背后:

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

First, a suggestion for debugging: temporarily comment out all the complexity. 首先,建议进行调试:暂时注释掉所有复杂性。 just retrieve a simple dataset with paging enabled. 只需检索启用分页的简单数据集即可。 If Paging works as expected then starting adding complexity back in until it fails. 如果分页按预期工作,则开始增加复杂性,直到失败为止。

Regarding Paging 关于分页

In any PageIndexChanging event do not do this: 在任何PageIndexChanging事件中都不要这样做:

SiteGridView.PageIndex = e.NewPageIndex;

You are doing manually what the server control does automatically. 您正在手动执行服务器控件自动执行的操作。 The value is there so you can check several conditions: 该值在那里,因此您可以检查以下几种情况:

  1. Determine if you need to cancel a paging event 确定是否需要取消寻呼事件

     // Is SiteGridView in edit mode // SiteGridView是否处于编辑模式\nif( SiteGridView.EditIndex > -1 ) if(SiteGridView.EditIndex> -1)\n    e.Cancel = true;` e.Cancel = true;` 
  2. Determine if the page is before or after the current page by comparison 通过比较确定页面是在当前页面之前还是之后

     if( SiteGridView.PageIndex > e.NewPageIndex) 如果(SiteGridView.PageIndex> e.NewPageIndex)\n    // Page Down // 向下翻页\nelse 其他\n    // Page Up //向上翻页 
  3. Or change to some other page because you have some obscure paging algorithim: 或由于其他模糊的分页算法而切换到其他页面:

     e.NewPageIndex = PageReturnedBySomeObscurePagingAlgorithm(); e.NewPageIndex = PageReturnedBySomeObscurePagingAlgorithm(); 

Regarding the Page LifeCycle 关于页面生命周期

During a postback, like the one that occurs when you change gridview pages, a Page runs the entire lifecycle on every postback. 在回发期间,就像在更改gridview页面时发生的那样,在每个回发中,页面都会运行整个生命周期。

So make sure you are not Binding in multiple places or manually setting other gridview control values that may alter paging results. 因此,请确保您未在​​多个位置进行绑定或手动设置其他可能会更改分页结果的gridview控件值。 Like setting 喜欢设置

`SiteGridView.PageIndex = 1` 

in some event that occurs after the PageIndexChanged event in the LifeCycle. 在LifeCycle中的PageIndexChanged事件之后发生的某些事件中。

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

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