Articles in this section
Category / Section

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

1 min 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.

 

Conclusion

I hope you enjoyed learning about how to place dropdown in ASP.NET MVC Grid`s pager to control page size.

You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.

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
Please sign in to leave a comment
Access denied
Access denied