Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Centralizing Code Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DK - 30 Dec 2007 21:54 GMT
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;
   }
Andrew - 30 Dec 2007 22:36 GMT
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
Eliyahu Goldin - 31 Dec 2007 09:49 GMT
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);
}

Signature

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

>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
[quoted text clipped - 122 lines]
> Resources.Searching.RecordsFound;
>    }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.