0

I am trying to extend the Paging property of our Gridview to allow a user to select how many items they would like to see per page by clicking on the respective text links; i.e., 25 | 50 | 75 | 100. The page property is set in the code behind as such:

    /// <summary>
    /// Gets or sets the number of items displayed on a result page.
    /// </summary>
    #region  PageSize;
    [Bindable(true),
    Category("Paging"),
    Description("Paging Size"),
    NotifyParentProperty(true)]
    public virtual int PageSize
    {
        get
        {
            return (ViewState["PageSize"] == null) ? 25 : (int)ViewState["PageSize"];
        }

        set
        {
            //return ((int)ViewState["LastPage"]);
            ViewState["PageSize"] = value;
        }
     }

The events are:

    /// <exclude/>
    /// <summary>
    /// Click event on '25' paging text link
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">EventArgs</param>
    protected virtual void PageSize25_Click(object sender, EventArgs e)
    {
        // ToDo: Pass new page size 25 to click event
        // Bind data to new page size
        // ViewState["PageSize"] = 25;
    }

    /// <exclude/>
    /// <summary>
    /// Click event on '50' paging text link
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">EventArgs</param>
    protected virtual void PageSize50_Click(object sender, EventArgs e)
    {
        // ToDo: Pass new page size 50 to click event
        // Bind data to new page size 
        // ViewState["PageSize"] = 50;
    }

    /// <exclude/>
    /// <summary>
    /// Click event on '75' paging text link
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">EventArgs</param>
    protected virtual void PageSize75_Click(object sender, EventArgs e)
    {
        // ToDo: Pass new page size 75 to click event
        // Bind data to new page size            

    }

    /// <exclude/>
    /// <summary>
    /// Click event on '100' paging text link
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">EventArgs</param>
    protected virtual void PageSize100_Click(object sender, EventArgs e)
    {
        // ToDo: Pass new page size 100 to click event
        // Bind data to new page size     

    }

I somehow need to pass the value of each text link (25 | 50 | 75 | 100) to the PageSize property.

Thanks. Any help would be greatly appreciated.

Carol
  • 3
  • 2

1 Answers1

1

Well to start with, since you're doing the same thing, you're just using a different parameter (page size), I would create a single event to handle it and have all of your LinkButtons call that event with a CommandArgument set:

Html side:

<asp:LinkButton ID="lnkPageSize75" runat="server" CommandArgument="75" Text="75" OnClick="PageSize_Click" />

Code Behind:

protected virtual void PageSize_Click(object sender, EventArgs e)
{
    LinkButton lnk = (LinkButton)sender;
    ViewState["PageSize"] = Convert.ToInt32(lnk.CommandArgument);

    BindGridView();        
}

private void BindGridView()
{
    // Treat as psuedo-code. May take some tweaking with casting.
    myGridView.PageSize = ViewState["PageSize"]; 
    MyDataType data = MyDataLayer.GetData();
    myGridView.DataBind();        
}
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • Thanks, I will give this a go as it's given me a good place to start. I will let you know if it works :)! – Carol Jul 16 '12 at 13:32
  • Actually, we're wanting to create the entire paging event in the ascx.cs file as we have hundreds of html pages that will be calling the extended pager. Any further ideas? – Carol Jul 16 '12 at 14:21
  • @Carol: I'm not sure what you mean by your last comment. This code is meant to be enclosed in a webusercontrol that can be used site wide. If you want the value to persist between different pages, you'll have to use Session instead of ViewState. ViewState only lives for the current page. – Joel Etherton Jul 16 '12 at 14:56
  • Joel, after tweaking some of the code you gave me, I got it to work just the way I needed it. I incorporated the html side of things into the customized gridview control using specially defined class data. Your solution above was a big help. So, thanks!! – Carol Jul 18 '12 at 15:05