trevorhartman
asked on
codebehind best practices
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.GetAllCat egories();
ProductTypes.DataBind();
ProductTypes.Items.Insert( 0,"");
ClientList.DataSource = CCMTEES.Client.GetCcmteesC lients();
ClientList.DataBind();
}
then, for another page, I have to also include the following in my page load:
// populate the countries
billCountry.DataSource = CCMTEES.ShoppingCart.GetCo untryCodes ();
billCountry.DataBind();
shipCountry.DataSource = CCMTEES.ShoppingCart.GetCo untryCodes ();
shipCountry.DataBind();
// set the default selected value to US
billCountry.Items.FindByVa lue("US"). Selected = true;
shipCountry.Items.FindByVa lue("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
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.GetAllCat
ProductTypes.DataBind();
ProductTypes.Items.Insert(
ClientList.DataSource = CCMTEES.Client.GetCcmteesC
ClientList.DataBind();
}
then, for another page, I have to also include the following in my page load:
// populate the countries
billCountry.DataSource = CCMTEES.ShoppingCart.GetCo
billCountry.DataBind();
shipCountry.DataSource = CCMTEES.ShoppingCart.GetCo
shipCountry.DataBind();
// set the default selected value to US
billCountry.Items.FindByVa
shipCountry.Items.FindByVa
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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=BindMy Data
...
void BindMyData(object sender, EventArgs e)
{
//bind some data..
}
?
If so, this would be a great solution.
-Trevor
OnUserControlLoaded=BindMy
...
void BindMyData(object sender, EventArgs e)
{
//bind some data..
}
?
If so, this would be a great solution.
-Trevor
agreed classes are for operations, your rendering controls which is definately a job for .ascx !
Yes Trevor - this is the scenario and the solution
B..M
B..M
ASKER
excellent. time to research ascx... thanks guys
-Trevor
-Trevor
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