Solved

codebehind best practices

Posted on 2004-09-20
8
557 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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