我的页面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异常,因为每个事件都调用另一个。
答案 0 :(得分: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();
}