分页不在Gridview上工作

时间:2013-05-04 09:55:26

标签: asp.net gridview pagination

我有一个Gridview Control,其中包含标签模板字段。在RowDataBound期间填充标签值。当我进行除此模板字段之外的分页时,所有分页都是分页的。我认为,在进行分页时,每次在RowDataBound上填写标签模板字段。

现在我无法pagination。请帮忙

这是我的代码

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnDataBound="GridView1_DataBound" OnPageIndexChanging="GridView1_PageIndexChanging1" OnRowDataBound="GridView1_RowDataBound1" PageSize="3">
                    <Columns>
                        <asp:TemplateField HeaderText="First Row">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server">Text</asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="White" ForeColor="#000066" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                    <RowStyle ForeColor="#000066" />
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#007DBB" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#00547E" />
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>

GridView.cs

protected void Page_Load(object sender, EventArgs e)
        {
            Getdata();
        }

        public void Getdata()
        {
            SqlConnection con = new SqlConnection("Data Source=CMH-SOSQL\\SQ1;Initial Catalog=RPT2020_DEV;Integrated Security=True");
            con.Open();
            string qry = "use PID2020_DEV select * from batch_void_rsn_cd";
            SqlDataAdapter da = new SqlDataAdapter(qry, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();    
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView1.Columns[0].HeaderText = "First Row";
                Label lblKey = (Label)e.Row.FindControl("Label1");
                lblKey.Text = e.Row.RowIndex.ToString();
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Getdata(); 
        }

1 个答案:

答案 0 :(得分:0)

尝试以下代码

文件背后的代码

    using System;


  namespace ASP.NETFORUMS.Gridview
 {
  public partial class Gridviewtemp : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                Getdata();
                ViewState["InitialPage"] = 3;//page count
            }
        }
        catch (Exception)
        { }
    }

    public void Getdata()
    {

        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

        string query = "Select * from Products";

        DataSet ds = new DataSet();

        SqlDataAdapter sqladp = new SqlDataAdapter(query, sqlcon);

        sqladp.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int count = 0;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (ViewState["count"] != null)
            {
                count = Convert.ToInt32(ViewState["count"]);
            }


            if (ViewState["InitialPage"] != null)
            {
                count = (int)ViewState["InitialPage"];
            }
            GridView1.Columns[0].HeaderText = "First Row";
            Label lblKey = (Label)e.Row.FindControl("Label1");
            lblKey.Text = count.ToString();


            count--;
            if (ViewState["InitialPage"] != null)
            {
                ViewState["InitialPage"] = count;

            }
            else
            {

                ViewState["count"] = count;
            }

        }
    }



    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;

        ViewState["count"] = e.NewPageIndex;
        ViewState["InitialPage"] = null;
        Getdata();
    }
}

}

使用viewstate解决问题。

希望它能回答你的问题