Sep 4, 2008

Custom Paging in GridView

When we get data with WebService its little time consuming if we have large data.So we will get limited data(Page size of GridView) to improve performance.

private static int pageNumbers = 0;

protected void Page_Load(object sender, EventArgs e)

{

GetData();

if (IsPostBack && pageNumbers > 1)

{

this.panelPager.Controls.Clear();

for (int i = 0; i <>

{

LinkButton lnk = new LinkButton();

Label lblSpacer = new Label();

lblSpacer.Width = 3;

lnk.ID = i.ToString();

lnk.Text = (i + 1).ToString();

this.panelPager.Controls.Add(lnk);

this.panelPager.Controls.Add(lblSpacer);

lnk.Click += new EventHandler(lnk_Click);

}

}

}

public void GetData()

{

RekabuService.WebService rek = new RekabuService.WebService();

ListPropertyObject> propertyList = new ListPropertyObject>();

if (searchText != "")

propertyList = rek.GetProperties("userid='" + userId + "'", searchText, "id", 0, gvPropertyList.PageSize).ToList();

this.gvPropertyList.DataSource = propertyList;

this.gvPropertyList.DataBind();

CustomPaging(Convert.ToInt32(propertyList[0].totalCount));

}

private void CustomPaging(int recordCount)

{

if (recordCount % Convert.ToInt32(gvPropertyList.PageSize) > 0)

pageNumbers = recordCount / Convert.ToInt32(gvPropertyList.PageSize) + 1;

else

pageNumbers = recordCount / Convert.ToInt32(gvPropertyList.PageSize);

this.panelPager.Controls.Clear();

if (pageNumbers > 1)

{

for (int i = 0; i <>

{

LinkButton lnk = new LinkButton();

Label lblSpacer = new Label();

lblSpacer.Width = 3;

lnk.ID = i.ToString();

lnk.Text = (i + 1).ToString();

this.panelPager.Controls.Add(lnk);

this.panelPager.Controls.Add(lblSpacer);

lnk.Click += new EventHandler(lnk_Click);

}

}

}

void lnk_Click(object sender, EventArgs e)

{

LinkButton btn = (LinkButton)sender;

int offSet = Convert.ToInt32(btn.ID) * Convert.ToInt32(gvPropertyList.PageSize);

RekabuService.WebService rek = new RekabuService.WebService();

ListPropertyObject> propertyList = new ListPropertyObject>();

if (orderby != "")

propertyList = rek.GetProperties("userid='" + userId + "'", “”, orderby, offSet, Convert.ToInt32(gvPropertyList.PageSize)).ToList();

gvPropertyList.DataSource = propertyList;

gvPropertyList.DataBind();

}

1 comments: