Solved

codebehind best practices

Posted on 2004-09-20
8
553 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now