1

我的页面 RadGrid1 和 RadGrid2 中有两个 RadGrid 控件,当我更改其中任何一个的页面大小时,我希望另一个自动更改为相同的页面大小,我尝试了这个:

protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    RadGrid2.PageSize = e.NewPageSize;
}

protected void RadGrid2_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    RadGrid1.PageSize = e.NewPageSize;
}

但这会导致 stackoverflow 异常,因为每个事件都调用了另一个事件。

4

1 回答 1

2

请尝试以下代码片段。

.aspx

 <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnItemDataBound="RadGrid1_ItemDataBound"
    OnNeedDataSource="RadGrid1_NeedDataSource" AllowFilteringByColumn="true" 
     PageSize="2" AllowPaging="true" onpagesizechanged="RadGrid1_PageSizeChanged">
    <MasterTableView Name="Parent" DataKeyNames="ID" EditMode="PopUp">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn>
            </telerik:GridEditCommandColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

<telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid2_NeedDataSource"  onpagesizechanged="RadGrid2_PageSizeChanged"
PageSize="2" AllowPaging="true">
    <MasterTableView Name="Parent" DataKeyNames="ID" EditMode="PopUp">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

.aspx.cs

 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
    new { ID = 1, Name ="Name1",path="1.jpg"},
    new { ID = 2, Name = "Name2",path="2.jpg"},
    new { ID = 3, Name = "Name3",path="3.jpg"},
     new { ID = 4, Name = "Name4",path="2.jpg"},
    new { ID = 5, Name = "Name5",path="3.jpg"}
};
    RadGrid1.DataSource = data;
}


protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode && e.Item is GridEditableItem)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        string strID = item.GetDataKeyValue("ID").ToString();
        // Access your DataKey here
    }
}



protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
    new { ID = 1, Name ="Name1",path="1.jpg"},
    new { ID = 2, Name = "Name2",path="2.jpg"},
    new { ID = 3, Name = "Name3",path="3.jpg"},
     new { ID = 4, Name = "Name4",path="2.jpg"},
    new { ID = 5, Name = "Name5",path="3.jpg"}
};
    RadGrid2.DataSource = data;
}


protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    RadGrid2.PageSize = e.NewPageSize;
    RadGrid2.Rebind();
}

protected void RadGrid2_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    RadGrid1.PageSize = e.NewPageSize;
    RadGrid1.Rebind();
}
于 2012-09-04T05:24:28.567 回答