Solved

codebehind best practices

Posted on 2004-09-20
8
556 Views
Last Modified: 2008-03-10
Hi,

I've got a set of 3 aspx pages that all follow a certain template.  They all contain a repeater, dropdownlist and 4 navigation buttons.  I'm using the same codebhind for all 3 pages to handle click events and bind the data in the Page_Load.  However, on some pages, I need aditional coding in the Page_Load.  If I explicitly include a Page_Load in a <script runat="server"></script> block on this page, it overrides the Page_Load that is contained in the codebhind, so I'm forced to duplicate the Page_Load code in the codebehind and add the extra code required for this specific page...  What can I do to prevent duplicating the data?  Also, is it possible to have multiple files for the codebehind?  Keep in mind I want to follow best practices...

Thanks,

Trevor
0
Comment
Question by:trevorhartman
  • 3
  • 3
  • 2
8 Comments
 
LVL 17

Assisted Solution

by:AerosSaga
AerosSaga earned 250 total points
ID: 12107521
First off you should never have an inline page_load event and one in the codebehind, can't believe it let you do that and secondly if you want to share methods functions across multiple pages you should make a seperate class to contain these public methods or functions.  just right click on the project and hit new item, then you will see class listed.  Put all your shared functions/methods in it and call them as needed from your pages.  I do this all the time with image resizers, shipping modules, credit card gateways etc.  If you need any more help let me know.

Regards,

Aeros
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12107524
Hi trevorhartman,

first of all it is best to use only one file - not 3
one of the best practise is not to duplicate - code, controls - if you can modify them
so in your case you can use only one page and fr the aditional coding to use user control

the aditional coding will be in the page_load of the page ( aspx file ), and the page_load of the user control is executed after the page_load of the aspx file is finished

B..M
0
 
LVL 8

Author Comment

by:trevorhartman
ID: 12108066
Aeros:

Thanks for the tip.  Is there any way to avoid duplicating the code int he page load?  I'll show you what i'm doing.  Here is the generic Page_Load that I'm using for all three pages:

            if (!IsPostBack)
            {
                  ProductTypes.DataSource = CCMTEES.Products.GetAllCategories();
                  ProductTypes.DataBind();
                  ProductTypes.Items.Insert(0,"");
                  
                  ClientList.DataSource = CCMTEES.Client.GetCcmteesClients();
                  ClientList.DataBind();
            }

then, for another page, I have to also include the following in my page load:

            // populate the countries
            billCountry.DataSource = CCMTEES.ShoppingCart.GetCountryCodes();
            billCountry.DataBind();
            shipCountry.DataSource = CCMTEES.ShoppingCart.GetCountryCodes();
            shipCountry.DataBind();
            
            // set the default selected value to US
            billCountry.Items.FindByValue("US").Selected = true;
            shipCountry.Items.FindByValue("US").Selected = true;

now, do I just have to duplicate the code into multiple Page_Loads in seperate classes, or is there a better way?

mmarinov:

You would recommend making my entire page a user control?  Here is my content template:
http://www.ccmtees.com/new/contentTemplate.aspx

If you click on the buttons you can see that the other pages look the same and just have different content in the center section.  What do you think?


Thanks - Trevor
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
ID: 12108111
NO!
What i suggested is to use user control only for the specific need
in your case you can have only on aspx page with several user controls
 - for sign up
 - for support
 - for my account

then all the main funtionality will be in the aspx file and the special functionality will be in the user controls

B..M
0
 
LVL 8

Author Comment

by:trevorhartman
ID: 12108148
Ok... I see.  I have studied a little on user controls but never actually made one.  With user controls, can you encapsulate what would normally be inside a page_load inside the usercontrol instead?  Like:

OnUserControlLoaded=BindMyData

...

void BindMyData(object sender, EventArgs e)
{
  //bind some data..
}

?

If so, this would be a great solution.

-Trevor
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12108267
agreed classes are for operations, your rendering controls which is definately a job for .ascx !  
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12108304
Yes Trevor - this is the scenario and the solution

B..M
0
 
LVL 8

Author Comment

by:trevorhartman
ID: 12108318
excellent.  time to research ascx... thanks guys

-Trevor
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question