Centralizing Code Question

Centralizing Code Question

am 30.12.2007 22:54:01 von 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("PreviousRecor d")).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("PreviousRecor d")).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")).Tex t =
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;
}

RE: Centralizing Code Question

am 30.12.2007 23:36:01 von Andrew

Hi DK,

like with any OO language, you can use inheritance to extend the
functionality of objects such as the GridView.

There are plenty of examples if you search google, but here's a nice &
simple tutorial to give the general idea:

http://blogs.msdn.com/mattdotson/articles/490868.aspx

Regards,
Andrew

Re: Centralizing Code Question

am 31.12.2007 10:49:09 von 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" wrote in message
news:D6C46F3D-06FE-412E-8051-E4242FFC17BB@microsoft.com...
>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("PreviousRecor d")).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("PreviousRecor d")).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")).Tex t =
> 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;
> }
>