0

我正在尝试扩展我们的 Gridview 的 Paging 属性,以允许用户通过单击相应的文本链接来选择他们希望每页看到多少项目;即 25 | 50 | 75 | 100. page属性在后面的代码中设置如下:

    /// <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;
        }
     }

这些事件是:

    /// <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     

    }

我不知何故需要将每个文本链接的值 (25 | 50 | 75 | 100) 传递给 PageSize 属性。

谢谢。任何帮助将不胜感激。

4

1 回答 1

1

首先,由于您正在做同样的事情,您只是使用不同的参数(页面大小),我将创建一个事件来处理它,并让您的所有 LinkBut​​tons 使用 CommandArgument 集调用该事件:

html端:

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

代码背后:

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();        
}
于 2012-07-16T13:05:57.900 回答