C# 更改页面后,RadGrid 分页无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11845265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RadGrid Paging does not work fine after changing page
提问by Mahdi Tahsildari
I have a RadGrid in my ASP.Net app and I have set the AllowPaging to True and PageSize to 10, now it load 10 items per RadGridPage which is what I wanted, but as soon as I press Next Page button (Arrow look button) nothing loads and RadGrid gets empty. How can I make it work normal?
我的 ASP.Net 应用程序中有一个 RadGrid,我已将 AllowPaging 设置为 True 并将 PageSize 设置为 10,现在它为每个 RadGridPage 加载 10 个项目,这正是我想要的,但是只要我按下下一页按钮(箭头查看按钮)没有任何负载,RadGrid 变空。我怎样才能让它正常工作?
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridOnLoad();
}
private void PopulateGridOnLoad()
{
rgCustomers.DataSource = odsCustomers;
// your datasource type
rgCustomers.MasterTableView.VirtualItemCount = 28;
//your datasource type total/count
rgCustomers.CurrentPageIndex = rgCustomers.MasterTableView.CurrentPageIndex;
rgCustomers.Rebind();
}
protected void grdName_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
rgCustomers.DataSource = odsCustomers;
// your datasource type
rgCustomers.MasterTableView.VirtualItemCount = 28;
//your datasource type total/count
rgCustomers.CurrentPageIndex = rgCustomers.MasterTableView.CurrentPageIndex;
//Donot rebind here
}
protected void btnLoad_Click(object sender, EventArgs e)
{
odsCustomers.SelectParameters["CustomerFullName"].DefaultValue = txtFullName.Text;
odsCustomers.SelectParameters["CustomerMelliCode"].DefaultValue = txtMelliCode.Text;
odsCustomers.SelectParameters["CustomerHomeAddress"].DefaultValue = txtHomeAddressPart.Text;
odsCustomers.SelectParameters["CustomerWorkAddress"].DefaultValue = txtWorkAddressPart.Text;
rgCustomers.DataSource = odsCustomers;
rgCustomers.DataBind();
}
采纳答案by Mahdi Tahsildari
After a long time I found the solution, the problem involved 2 minor issues :
1.I had both DataSource (in code) and DataSourceID (property) set and they didn't work together well
2.I had both AllowPaging and AllowCustomPaging set to true, when they are both true neither works :) that's the telerik team you know, but they are great I was kidding
很长一段时间后,我找到了解决方案,该问题涉及 2 个小问题:1.
我同时设置了 DataSource(在代码中)和 DataSourceID(属性),但它们不能很好地协同工作
2.我将 AllowPaging 和 AllowCustomPaging 设置为是的,当它们都是真的时,两者都不起作用:) 那是你知道的 Telerik 团队,但他们很棒,我在开玩笑
回答by Phist
You need to define the "onNeedDataSource" radgrid event where you shoud reset the datasource of your grid.
您需要定义“onNeedDataSource”radgrid 事件,您应该在其中重置网格的数据源。
protected void RadGrid_NeedDataSource(object sender, EventArgs e)
{
IsNeedDataSource = true;
}
and than in page OnPreRenderevent you shoul do smth like:
而不是在页面OnPreRender事件中,您应该这样做:
protected override void OnPreRender(object sender, EventArgs e)
{
DriverLinksGrid.DataSource = value;
DriverLinksGrid.DataBind();
}
I'm not shure, maybe you can bind data right in OnNeedDataSourceevent. But the DataBind()method may be not not available from there.
我不知道,也许你可以在OnNeedDataSource事件中绑定数据。但该 DataBind()方法可能无法从那里获得。
回答by Pravin Pawar
You will have to set up following attributes of the grid in design
您必须在设计中设置网格的以下属性
<telerik:RadGrid ID="grdName"
AllowPaging="True"
AllowCustomPaging="True"
VirtualItemCount="0" PageSize="15" >
Populate grid on load vb.net
在加载 vb.net 时填充网格
Private Sub PopulateGridOnLoad()
grdName.DataSource = source ' your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total 'your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex
grdName.Rebind()
End Sub
Populate grid on load c#.net
在加载时填充网格 c#.net
private void PopulateGridOnLoad()
{
grdName.DataSource = source;
// your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total;
//your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex;
grdName.Rebind();
}
Override NeedDatasource vb.net
覆盖 NeedDatasource vb.net
Protected Sub grdName_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdName.NeedDataSource
grdName.DataSource = source ' your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total 'your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex
'Donot rebind here
End Sub
Override NeedDatasource c#
覆盖 NeedDatasource c#
protected void grdName_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
grdName.DataSource = source;
// your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total;
//your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex;
//Donot rebind here
}

