Centralizing Code Question

Discussion in 'ASP .Net' started by DK, Dec 30, 2007.

  1. DK

    DK Guest

    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;
    }
     
    DK, Dec 30, 2007
    #1
    1. Advertising

  2. DK

    Andrew Guest

    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
     
    Andrew, Dec 30, 2007
    #2
    1. Advertising

  3. 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:D...
    >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;
    > }
    >
     
    Eliyahu Goldin, Dec 31, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,247
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Keith Patrick

    Centralizing MasterPage and Theme

    Keith Patrick, Nov 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    384
    Keith Patrick
    Nov 18, 2005
  3. D. Shane Fowlkes
    Replies:
    6
    Views:
    464
    D. Shane Fowlkes
    Feb 14, 2006
  4. Guest
    Replies:
    8
    Views:
    369
    Steven Cheng[MSFT]
    Dec 29, 2006
  5. Rhino

    Centralizing instantiation?

    Rhino, May 25, 2010, in forum: Java
    Replies:
    3
    Views:
    260
    Arne Vajhøj
    May 26, 2010
Loading...

Share This Page