PDA

View Full Version : Set page number in codebehind



ambruslaco
Mar 10, 2015, 2:39 PM
Hi,
how can i set page number of grid in codebehind?
In 1.6 I used to do this, but it do not work in 3.1.



this.storeExports.AutoLoadParams["start"] = "={" + recordNumber + "}";
this.storeExports.AutoLoadParams["limit"] = "={" + pageSize + "}";




<AutoLoadParams>
<ext:Parameter Name="start" Value="={0}" />
<ext:Parameter Name="limit" Value="={20}" />
</AutoLoadParams>


Thanks

Laco

RCN
Mar 13, 2015, 6:05 PM
Let me know whether the following example helps you.



<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
</head>
<body>
<ext:ResourceManager runat="server" Theme="Gray" ScriptMode="Debug" />
<ext:GridPanel Title="Ext.Net" Width="500" Height="500" runat="server">
<Store>
<ext:Store PageSize="10" AutoLoad="true" runat="server">
<Proxy>
<ext:AjaxProxy Url="~/Example/LoadFakeRecords/" StartParam="start" LimitParam="limit" PageParam="page" SortParam="sort">
<ActionMethods Read="POST" />
<Reader>
<ext:JsonReader RootProperty="data" />
</Reader>
</ext:AjaxProxy>
</Proxy>
<Model>
<ext:Model IDProperty="ID" runat="server">
<Fields>
<ext:ModelField Name="ID" />
<ext:ModelField Name="Name" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ColumnModel runat="server">
<Columns>
<ext:Column Text="ID" DataIndex="ID" runat="server" />
<ext:Column Text="Name" DataIndex="Name" Flex="1" runat="server" />
</Columns>
</ColumnModel>
<BottomBar>
<ext:PagingToolbar runat="server" />
</BottomBar>
</ext:GridPanel>
</body>
</html>




namespace SandBox.Controllers
{
public class ExampleController : System.Web.Mvc.Controller
{
public ActionResult Index()
{
return View();
}

public StoreResult LoadFakeRecords(int start, int limit, int page, string sort)
{
List<Entity> lst = new List<Entity>();

for (int index = start; index < (page * limit); index++)
{
lst.Add(new Entity
{
ID = index,
Name = string.Format("Name - {0}", index)
});
}

return new StoreResult(lst, (page * limit) + limit);
}
}

[Serializable]
public class Entity
{
public int ID { get; set; }

public string Name { get; set; }
}
}

ambruslaco
Mar 16, 2015, 10:01 AM
Thanks for reply, but I think that you misunderstood.
I want to set page number (or start parameter), so that Ext.Net.Store at its first request to server sends start paramater indicating it wants to load n-th page of list.
Something like: this.storeMain.CurrentPage = 3; (currentPage is property of Ext.data.Store on client side, Sencha docs (http://docs.sencha.com/extjs/5.0.1/#!/api/Ext.data.Store-property-currentPage))

My scenario:
I store grid settings (paging, sort, filter), so when user returns to page, grid looks like as last time he left (e.g. after returning from detail)

RCN
Mar 16, 2015, 1:53 PM
Thanks for reply, but I think that you misunderstood.
I want to set page number (or start parameter), so that Ext.Net.Store at its first request to server sends start paramater indicating it wants to load n-th page of list.
Something like: this.storeMain.CurrentPage = 3; (currentPage is property of Ext.data.Store on client side, Sencha docs)

All those parameters are present in LoadFakeRecords action.




My scenario:
I store grid settings (paging, sort, filter), so when user returns to page, grid looks like as last time he left (e.g. after returning from detail)

Let me know whether the following sample helps you.

Take a look on Store's Parameters (lines 29 to 32)


<!DOCTYPE html>
<html>
<head runat="server">
</head>
<body>
<ext:ResourceManager runat="server" />
<ext:NumberField ID="_p1" FieldLabel="P1" MinValue="1" MaxValue="100" runat="server" />
<ext:NumberField ID="_p2" FieldLabel="P2" MinValue="1" MaxValue="100" runat="server" />
<ext:GridPanel Title="Ext.Net" Border="true" Width="500" Height="500" runat="server">
<Store>
<ext:Store ID="_str" AutoLoad="false" runat="server">
<Proxy>
<ext:AjaxProxy Url="~/Example/LoadFakeRecords/">
<Reader>
<ext:JsonReader RootProperty="data" />
</Reader>
</ext:AjaxProxy>
</Proxy>
<Model>
<ext:Model IDProperty="ID" runat="server">
<Fields>
<ext:ModelField Name="ID" Type="String" />
<ext:ModelField Name="Name" Type="String" />
<ext:ModelField Name="LastName" Type="String" />
<ext:ModelField Name="Address" Type="String" />
</Fields>
</ext:Model>
</Model>
<Parameters>
<ext:StoreParameter Name="p1" Value="#{_p1}.getValue()" Mode="Raw" />
<ext:StoreParameter Name="p2" Value="#{_p2}.getValue()" Mode="Raw" />
</Parameters>
</ext:Store>
</Store>
<ColumnModel runat="server">
<Columns>
<ext:Column Text="ID" DataIndex="ID" runat="server" />
<ext:Column Text="Name" Flex="1" DataIndex="Name" runat="server" />
<ext:Column Text="Last Name" DataIndex="LastName" runat="server" />
<ext:Column Text="Address" DataIndex="Address" runat="server" />
</Columns>
</ColumnModel>
<Buttons>
<ext:Button Text="Click Me" runat="server">
<Listeners>
<Click Handler="#{_str}.loadPage(1)" />
</Listeners>
</ext:Button>
</Buttons>
</ext:GridPanel>
</body>
</html>




namespace SandBox.Controllers
{
public class ExampleController : System.Web.Mvc.Controller
{
public ActionResult Index()
{
return View();
}

public StoreResult LoadFakeRecords(Nullable<int> p1, Nullable<int> p2)
{
Debug.WriteLine("p1:'{0}' | p2:'{1}'", p1, p2);

List<Entity> lst = new List<Entity>();

for (int index = 0; index < 10; index++)
{
lst.Add(new Entity
{
ID = index,
Name = string.Format("Name - {0}", index)
});
}

return new StoreResult(lst, 100);
}
}

[Serializable]
public class Entity
{
public int ID { get; set; }

public string Name { get; set; }
}
}

ambruslaco
Mar 16, 2015, 2:58 PM
All those parameters are present in LoadFakeRecords action.

Method LoadFakeRecords is called in request initiated by store. Store sends parameters in this request. I want to preset those parameters for first, automatic request (with AutoLoad=true). Default value of start is 0 and page is 1. This is configured in Ext.data.Store.

Problem with your last example is that those are "custom" parameters and those are send in every request. If I used this, I would have to store somewhere information that this is first request and if so, then work with this custom parameter instead of traditional parameters start and page, which are send by store by default.

Your example made me thing about this (not neat) solution:



this.storeMain.AutoLoad = false;
this.storeMain.Listeners.AfterRender.Handler = string.Format( "this.loadPage({0});", gridConfigSaved.PageNumber); // simulating autoload

ambruslaco
Mar 16, 2015, 4:00 PM
I am so sorry, first I used AutoLoadParams in wrong way and it not worked, so I was looking for another solution.

The right way to set initial page number of store is by setting AutoLoadParams of Ext.Net.Store



this.store.AutoLoadParams["start"] = "40"; // or ((pageNumber - 1) * pageSize).ToString();

or


<AutoLoadParams>
<ext:Parameter Name="start" Value="40" /> <%-- for 3rd page --%>
</AutoLoadParams>


BUT!!!

a) this solution not sets right page to PagingToolbar

b)
This solution is not working with Store's property RemoteFilter set to true. Strange!
If I set RemoteFilter=true and grid has some Gridfilters, AutoLoadParams do not apply.

Example:



<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager runat="server" />
<ext:Store
ID="store"
runat="server"
AutoLoad="true"
RemoteFilter="true"
OnReadData="store_RefreshData">
<Proxy>
<ext:PageProxy />
</Proxy>
<Model>
<ext:Model runat="server" IDProperty="ID">
<Fields>
<ext:ModelField Name="ID" />
<ext:ModelField Name="Name" />
</Fields>
</ext:Model>
</Model>
<%-- <AutoLoadParams>
<ext:Parameter Name="start" Value="40" />
</AutoLoadParams>--%>
</ext:Store>
<ext:Viewport ID="viewport" runat="server" Layout="border">
<Items>
<ext:GridPanel
ID="gridMain"
runat="server"
Region="Center"
StoreID="store">
<ColumnModel>
<Columns>
<ext:Column Width="150" runat="server" DataIndex="Name" Text="Name">
<Filter>
<ext:StringFilter />
</Filter>
</ext:Column>
<ext:Column Width="140" runat="server" DataIndex="ID" Text="ID" />
</Columns>
</ColumnModel>
<View>
<ext:GridView runat="server" />
</View>
<Plugins>
<ext:GridFilters runat="server" />
</Plugins>
</ext:GridPanel>
</Items>
</ext:Viewport>
</form>
</body>
</html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest) this.store.AutoLoadParams["start"] = "40";
}
protected void store_RefreshData(object sender, StoreReadDataEventArgs e)
{
if (e.Start == 40)
{
// OK
}
else
{
// WRONG
}
}
</script>

I started new thread AutoLoadParams not applied with RemoteFilter (http://forums.ext.net/showthread.php?56701-AutoLoadParams-not-applied-with-RemoteFilter)