Centralizing Code Question

D

DK

I have an asp.net web site which has GridViews on many pages. They are
different except they all have a common Pager Template. On the DataBound
event, I customize this template based on the number of records, etc. This
is exactly the same for every grid so rather than copy and paste this code
in every page, I am hoping there is a way to better manage it. If I had to
make a change to the way the pager works I don't want to go to every page.

I thought about creating a base page and overriding the OnDataBound event
but my web pages already inherit a base page of Page.

Any ideas?

Thanks.

My code for reference:

protected void GridView1_DataBound(object sender, EventArgs e)
{
// This event allows us to find the controls in the PagerTemplate
and
// customize them
GridViewRow pagerRowTop = GridViewImages.TopPagerRow;
GridViewRow pagerRowBot = GridViewImages.BottomPagerRow;

LinkButton firstRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("FirstRecord");
LinkButton nextRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("NextRecord");
LinkButton previousRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("PreviousRecord");
LinkButton lastRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("LastRecord");
Label pageLabel =
(Label)pagerRowTop.Cells[0].FindControl("PageLabel");
Label recordCount =
(Label)pagerRowTop.Cells[0].FindControl("RecordCount");

nextRecord.Text = Resources.Navigation.Next + " ";
previousRecord.Text = Resources.Navigation.Previous + " ";
firstRecord.Text = Resources.Navigation.First + " ";
lastRecord.Text = Resources.Navigation.Last + " ";

((LinkButton)pagerRowBot.Cells[0].FindControl("FirstRecord")).Text =
Resources.Navigation.First + " ";
((LinkButton)pagerRowBot.Cells[0].FindControl("NextRecord")).Text =
Resources.Navigation.Next + " ";
((LinkButton)pagerRowBot.Cells[0].FindControl("PreviousRecord")).Text
=
Resources.Navigation.Previous + " ";
((LinkButton)pagerRowBot.Cells[0].FindControl("LastRecord")).Text =
Resources.Navigation.Last + " ";

if (GridViewImages.PageIndex == 0)
{
firstRecord.Visible = false;
previousRecord.Visible = false;
((LinkButton)pagerRowBot.Cells[0].FindControl("FirstRecord")).Visible
= false;
((LinkButton)pagerRowBot.Cells[0].FindControl("PreviousRecord")).Visible
= false;
}
else if (GridViewImages.PageIndex == GridViewImages.PageCount - 1)
{
nextRecord.Visible = false;
lastRecord.Visible = false;
((LinkButton)pagerRowBot.Cells[0].FindControl("NextRecord")).Visible
= false;
((LinkButton)pagerRowBot.Cells[0].FindControl("LastRecord")).Visible
= false;
}

if (GridViewImages.PageCount == 1)
{
nextRecord.Visible = false;
lastRecord.Visible = false;

((LinkButton)pagerRowBot.Cells[0].FindControl("NextRecord")).Visible
= false;
((LinkButton)pagerRowBot.Cells[0].FindControl("LastRecord")).Visible
= false;

// always show PagerRow, by default it does not show it records
are less then 1 page
GridViewImages.TopPagerRow.Visible = true;
GridViewImages.BottomPagerRow.Visible = true;
}

pageLabel.Text = Resources.Searching.Page + " " +
(GridViewImages.PageIndex + 1) + " " +
Resources.Searching.of + " " +
GridViewImages.PageCount.ToString();

recordCount.Text = calcRecordCount();

((Label)pagerRowBot.Cells[0].FindControl("PageLabel")).Text =
Resources.Searching.Page + " " +
(GridViewImages.PageIndex + 1) + " " +
Resources.Searching.of + " " +
GridViewImages.PageCount.ToString();

((Label)pagerRowBot.Cells[0].FindControl("RecordCount")).Text =
calcRecordCount();
}

private string calcRecordCount()
{
// Formula to show: Showing a to b of c records found
Int32 b = (GridViewImages.PageIndex + 1) * GridViewImages.PageSize;
Int32 a = (b - GridViewImages.PageSize) + 1;
Int32 c = recordsFound;

// if it's the last page then b = c
if (GridViewImages.PageIndex + 1 == GridViewImages.PageCount)
{ b = c; }

return Resources.Searching.Showing + " " + a.ToString() + " " +
Resources.Searching.to + " " + b.ToString() + " " +
Resources.Searching.of + " " + c.ToString() + " " +
Resources.Searching.RecordsFound;
}
 
E

Eliyahu Goldin

The easiest thing would be to make a class with a static method SetupPaging
(GridView gridToSetup) and call this method from the DataBound event of all
gridviews

protected void GridView1_DataBound(object sender, EventArgs e)
{
utilityClass.SetupPaging(sender as GridView);
}

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


DK said:
I have an asp.net web site which has GridViews on many pages. They are
different except they all have a common Pager Template. On the DataBound
event, I customize this template based on the number of records, etc. This
is exactly the same for every grid so rather than copy and paste this code
in every page, I am hoping there is a way to better manage it. If I had to
make a change to the way the pager works I don't want to go to every page.

I thought about creating a base page and overriding the OnDataBound event
but my web pages already inherit a base page of Page.

Any ideas?

Thanks.

My code for reference:

protected void GridView1_DataBound(object sender, EventArgs e)
{
// This event allows us to find the controls in the PagerTemplate
and
// customize them
GridViewRow pagerRowTop = GridViewImages.TopPagerRow;
GridViewRow pagerRowBot = GridViewImages.BottomPagerRow;

LinkButton firstRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("FirstRecord");
LinkButton nextRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("NextRecord");
LinkButton previousRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("PreviousRecord");
LinkButton lastRecord =
(LinkButton)pagerRowTop.Cells[0].FindControl("LastRecord");
Label pageLabel =
(Label)pagerRowTop.Cells[0].FindControl("PageLabel");
Label recordCount =
(Label)pagerRowTop.Cells[0].FindControl("RecordCount");

nextRecord.Text = Resources.Navigation.Next + " ";
previousRecord.Text = Resources.Navigation.Previous + " ";
firstRecord.Text = Resources.Navigation.First + " ";
lastRecord.Text = Resources.Navigation.Last + " ";

((LinkButton)pagerRowBot.Cells[0].FindControl("FirstRecord")).Text
=
Resources.Navigation.First + " ";
((LinkButton)pagerRowBot.Cells[0].FindControl("NextRecord")).Text =
Resources.Navigation.Next + " ";

((LinkButton)pagerRowBot.Cells[0].FindControl("PreviousRecord")).Text =
Resources.Navigation.Previous + " ";
((LinkButton)pagerRowBot.Cells[0].FindControl("LastRecord")).Text =
Resources.Navigation.Last + " ";

if (GridViewImages.PageIndex == 0)
{
firstRecord.Visible = false;
previousRecord.Visible = false;

((LinkButton)pagerRowBot.Cells[0].FindControl("FirstRecord")).Visible =
false;

((LinkButton)pagerRowBot.Cells[0].FindControl("PreviousRecord")).Visible =
false;
}
else if (GridViewImages.PageIndex == GridViewImages.PageCount - 1)
{
nextRecord.Visible = false;
lastRecord.Visible = false;

((LinkButton)pagerRowBot.Cells[0].FindControl("NextRecord")).Visible =
false;

((LinkButton)pagerRowBot.Cells[0].FindControl("LastRecord")).Visible =
false;
}

if (GridViewImages.PageCount == 1)
{
nextRecord.Visible = false;
lastRecord.Visible = false;


((LinkButton)pagerRowBot.Cells[0].FindControl("NextRecord")).Visible =
false;

((LinkButton)pagerRowBot.Cells[0].FindControl("LastRecord")).Visible =
false;

// always show PagerRow, by default it does not show it records
are less then 1 page
GridViewImages.TopPagerRow.Visible = true;
GridViewImages.BottomPagerRow.Visible = true;
}

pageLabel.Text = Resources.Searching.Page + " " +
(GridViewImages.PageIndex + 1) + " " +
Resources.Searching.of + " " +
GridViewImages.PageCount.ToString();

recordCount.Text = calcRecordCount();

((Label)pagerRowBot.Cells[0].FindControl("PageLabel")).Text =
Resources.Searching.Page + " " +
(GridViewImages.PageIndex + 1) + " " +
Resources.Searching.of + " " +
GridViewImages.PageCount.ToString();

((Label)pagerRowBot.Cells[0].FindControl("RecordCount")).Text =
calcRecordCount();
}

private string calcRecordCount()
{
// Formula to show: Showing a to b of c records found
Int32 b = (GridViewImages.PageIndex + 1) * GridViewImages.PageSize;
Int32 a = (b - GridViewImages.PageSize) + 1;
Int32 c = recordsFound;

// if it's the last page then b = c
if (GridViewImages.PageIndex + 1 == GridViewImages.PageCount)
{ b = c; }

return Resources.Searching.Showing + " " + a.ToString() + " " +
Resources.Searching.to + " " + b.ToString() + " " +
Resources.Searching.of + " " + c.ToString() + " " +
Resources.Searching.RecordsFound;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top