如何在不触发PageSizeChanged事件的情况下以编程方式设置RadGrid的页面大小

时间:2012-05-21 20:24:31

标签: c# asp.net webforms telerik

我正在使用带有自定义分页的Telerik RadGrid。我的问题是,当我手动设置页面大小时,会触发PageSizeChanged事件并将我的代码抛入无限循环。作为一个黑客,我创建了一个在触发PageSizeChanged事件时设置的布尔值。如何在不触发PageSizeChanged事件的情况下在RadGrid控件上设置页面大小?

private bool PageSizeChanged
{
    get { return Convert.ToBoolean(ViewState["PageSizeChanged"]); }
    set { ViewState["PageSizeChanged"] = value; }
}

protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    if (!PageSizeChanged)
    {
        PageSizeChanged = true;
        RadGrid1.PageSize = e.NewPageSize;
    }

    PageSizeChanged = false;                        
    RadGrid1.Rebind();
}

更新:我现在有一个解决方案。我最终从调用列表中删除了事件处理程序,设置了页面大小,然后将事件添加回调用列表。

protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    RadGrid1.PageSizeChanged -= RadGrid1_PageSizeChanged;
    RadGrid1.PageIndex = 0;
    RadGrid1.PageSize = e.NewPageSize;
    RadGrid1.Rebind();
    RadGrid1.PageSizeChanged += RadGrid1_PageSizeChanged;
}

2 个答案:

答案 0 :(得分:7)

试试这个 -

protected void RadGrid1_PageSizeChanged(object source, GridPageSizeChangedEventArgs e) 
    {                
       RadGrid1.PageSizeChanged-=new GridPageSizeChangedEventHandler(RadGrid1_PageSizeChanged);
       RadGrid1.PageSize =  e.NewPageSize;

      RadGrid1.PageSizeChanged += new GridPageSizeChangedEventHandler(RadGrid1_PageSizeChanged);

      RebindGrid();
    }

答案 1 :(得分:2)

删除RadGrid1页面大小已更改,并将页面大小指定为 -

RadGrid1.MasterTableView.PageSize = CustomerPageSize;