0

我有一个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(); 
        }
4

1 回答 1

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

}

使用视图状态来解决问题。

希望它能回答你的问题

于 2013-05-04T14:55:23.503 回答