开发者

Event problem inside grid view with custom paging

开发者 https://www.devze.com 2023-04-05 09:16 出处:网络
I want to provide custom paging in grid view. <asp:GridView ID=\"gvFirst\" runat=\"server\" AutoGenerateColumns=\"false\"

I want to provide custom paging in grid view.

 <asp:GridView ID="gvFirst" runat="server" AutoGenerateColumns="false" 
        AllowPaging="true" 
        ondatabound="gvFirst_DataBound" >
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ProductID"/>
            <asp:BoundField DataField="Name" HeaderText="ProductName" />
        </Columns>
        <PagerTemplate>
            <asp:Panel ID="pnlPager" runat="server">
            </asp:Panel>
        </PagerTemplate>
    </asp:GridView>

If I create button here and bind click event then it is fire, but problem is this event occur for each row bind with grid

protected void gvFirst_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            Panel pnPager = e.Row.FindControl("pnlPager") as Panel;
            if (pnPager != null)
            {
                Button btnFirst = new Button();
                btnFirst.Text = "1";
                btnFirst.Click += new EventHandler(btnFirst_Click);
                pnPager.Controls.Add(btnFirst);
            }
        }
    }

If I create button here and bind click event then it is not fire; this event fire after all the rows bind to grid, so it will occur only once.

protected void gvFirst_DataBound(object sender, EventArgs e)
    {
        GridViewRow gvRow = gvFirst.BottomPagerRow;
        if (gvRow != null)
        {
            Panel pnPager = gvRow.FindControl("pnlPager") as Panel;
            if (pnPager != null)
            {
                Button btnFirst = new Button();
                btnFirst.Text = "1";
                btnFirst.Click += new EventHandler(btnFirst_Click);
                pnPager.Controls.Add(btnFirst);
            }
        }
    }

    void btnFirst_Click(object sender, EventArgs e)
    {
        using (_NorthWindDataContext = new NorthWindDataContext())
        {
            var ProductInformation = from p in _NorthWindDataContext.Products
                                     select new
                                     {
                                         ID = p.ProductID,
                                         Name = p.ProductName
                                     };
            gvFirst.DataSource = ProductInformation.Skip(5).Take(5);
            gvFirst.DataBind();
        }
    }

Another problem which I am开发者_JAVA百科 facing is I want to provide custom paging. Now I have set page size to 5 and I am fetching 5 record from query so my grid pager is not display.

public class Productinformation
{
    public int PID
    {
        get;
        set;
    }
    public string PName
    {
        get;
        set;
    }
}
 using (NorthWindDataContext _NorthWindDataContext = new NorthWindDataContext())
        {
            Proinfo = new List<Productinformation>();
            Proinfo = (from p in _NorthWindDataContext.Products
                       select new Productinformation
                       {
                           PID = p.ProductID,
                           PName = p.ProductName,
                       }).ToList();

            gvFirst.DataSource =  Proinfo.Take(PageSize) ;
            gvFirst.DataBind();
        }

Proinfo variable declare globally.

Now when I bind I run this code it will give me error the data source does not support server-side data paging. If I use var type of variable then it is worked but we can't declare var type of variable globally, so I used it then I have to call this method every time in paging, and I don't want to use Objectdatasource.


  1. GridView's RowCreated indeed will be called for every row in GridView, because that is the event where the GridView will (re)create the GridViewRows. But if you check for if (e.Row.RowType == DataControlRowType.Pager) there will be no overhead. This event is perfect for creating dynamic controls because it's called even on postback(as against RowDataBound). So your first way should be the correct one.
  2. If you want to show the pager also when there are less than PageSize rows, you should force the pager to be visible e.g. in GridView's overridden OnPreRender.

    GridViewRow pagerRow = (GridViewRow) this.BottomPagerRow;    
    if(pagerRow != null) pagerRow.Visible = true;
    
0

精彩评论

暂无评论...
验证码 换一张
取 消