Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

codebehind best practices

Posted on 2004-09-20
8
Medium Priority
?
562 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 17

Assisted Solution

by:AerosSaga
AerosSaga earned 1000 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
Technology Partners: 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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