Solved

codebehind best practices

Posted on 2004-09-20
8
554 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

21 Experts available now in Live!

Get 1:1 Help Now