Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Centralizing Code Question

Reply
Thread Tools

Centralizing Code Question

 
 
DK
Guest
Posts: n/a
 
      12-30-2007
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;
}

 
Reply With Quote
 
 
 
 
Andrew
Guest
Posts: n/a
 
      12-30-2007
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
 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      12-31-2007
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>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 + "&nbsp;";
> previousRecord.Text = Resources.Navigation.Previous + "&nbsp;";
> firstRecord.Text = Resources.Navigation.First + "&nbsp;";
> lastRecord.Text = Resources.Navigation.Last + "&nbsp;";
>
> ((LinkButton)pagerRowBot.Cells[0].FindControl("FirstRecord")).Text
> =
> Resources.Navigation.First + "&nbsp;";
> ((LinkButton)pagerRowBot.Cells[0].FindControl("NextRecord")).Text =
> Resources.Navigation.Next + "&nbsp;";
>
> ((LinkButton)pagerRowBot.Cells[0].FindControl("PreviousRecord")).Text =
> Resources.Navigation.Previous + "&nbsp;";
> ((LinkButton)pagerRowBot.Cells[0].FindControl("LastRecord")).Text =
> Resources.Navigation.Last + "&nbsp;";
>
> 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;
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Centralizing instantiation? Rhino Java 3 05-26-2010 12:33 AM
design guidance please :user controls and centralizing roles-related display stuff ASP .Net 8 12-29-2006 10:20 AM
Centralizing global variables and functions - good & simple method? D. Shane Fowlkes ASP .Net 6 02-14-2006 03:11 PM
Centralizing MasterPage and Theme Keith Patrick ASP .Net 0 11-18-2005 06:09 PM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM



Advertisments