Articles in this section
Category / Section

How to place dropdown in .NET WebForms Grid`s pager to control page size?

4 mins read

You can place a dropdown in the Grid pager using pager template feature along with the default pager and on change event of the dropdown, change the page size through set model of Grid.

 

Initially, append some li elements in the div tag and assign them as a targeID for the ejDropDownList and also include the input element in the e-pagercontainer.

 

<div id="pagesize">
    <ul>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
    </ul>
</div>
 
<script type="text/x-jsrender" id="template">
    <div class="e-pagercontainer">
        <input type="text" id="pager" />
    </div>
</script>
 
<div id="Grid"></div>

 

<script type="text/javascript">
    $(function () {
        $("#Grid").ejGrid({
            dataSource: window.gridData,
            allowPaging: true, 
            dataBound: 'dataBound',
            pageSettings: { enableTemplates: true, template: "#template", showDefaults: true },
            columns: [
                    { field: "OrderID" },
                    { field: "CustomerID" },
                    { field: "EmployeeID" },
                    { field: "Freight", format: "{0:C}" },
                    { field: "ShipCountry" }
            ]
        });
    }); 
</script>

 

ASPX

        <ej:Grid ID="Grid" runat="server" AllowPaging="True">
            <PageSettings ShowDefaults="true" EnableTemplates="true" Template="#template" />
            <Columns>
                <ej:Column Field="OrderID" />
                <ej:Column Field="CustomerID" />
                <ej:Column Field="EmployeeID" />
                <ej:Column Field="Freight" Format="{0:C}" />
                <ej:Column Field=" ShipCountry" />
            </Columns>
        </ej:Grid>

 

Code behind

    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Grid.DataSource = OrderRespository.GetAllRecords().ToList();
        }
    }

 

MVC Razor

@(Html.EJ().Grid<OrdersView>("Grid")
        .Datasource((IEnumerable<object>)ViewBag.dataSource) 
        .AllowPaging()
        .PageSettings(page => page.EnableTemplates().Template("#template").ShowDefaults())
        .Columns(col =>
        {
            col.Field("OrderID").Add();
            col.Field("CustomerID").Add();
            col.Field("EmployeeID").Add();
            col.Field("Freight").Format("{0:C}").Add();
            col.Field("ShipCountry").Add();
        })
        .ClientSideEvents(events => { events.DataBound("dataBound"); })
)

 

Controller

namespace MvcApplication66.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index() {
            var data = new NorthwindDataContext().OrdersViews.ToList();
            ViewBag.dataSource = data;
            return View();
        }
    }
}

 

Within the dataBound event render the DropDownList and bind change event. In the change event, update the pageSize of the Grid through set model as follows.

 

<script type="text/javascript">
    function dataBound(args) {
        $('#pager').ejDropDownList({
            change: "change",
            targetID: "pagesize"
        });
    }
    function change(args) {
        var gridObj = $("#Grid").ejGrid("instance");
        gridObj.option({ "pageSettings": { pageSize: parseInt(args.text) } });
        gridObj.getPager().find("input").ejDropDownList({
            selectedIndex: args.itemId,
            change: "change",
            dataSource: pagerData
        });
    }
</script>

 

The following screenshot will be displayed as the result of the above code example.

 

Figure: Dropdown in the Grid’s pager using pager template.

Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied