/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu




Friday, September 21, 2012

Dynamically set Gridview page size property in asp.net

Set Grid view page size property at run time in asp.net:

Here's an example:

ASPX 
<%@ page title="" language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true"
    codefile="AllowPageSizing.aspx.cs" inherits="GridView_AllowPageSizing" %>

 id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
    Page Size:
     id="DropDownList1" runat="server" autopostback="True"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        2

       
5
       
10
   

   
id="GridView1" runat="server" style="margin-top: 20px;"
       
allowpaging="True" onpageindexchanging="GridView1_PageIndexChanging">
   

CODE-BEHIND 

using System;
using System.Web.UI.WebControls;
public partial class GridView_AllowPageSizing : System.Web.UI.Page
{
    private void GetData()
    {
        GridView1.PageSize = Int32.Parse(DropDownList1.SelectedValue);
        GridView1.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        GridView1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.GetData();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = 0;
        this.GetData();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.GetData();
    }
}
Read More »

Recent Posts

My Blog List