Common functionality across pages, what's the best solution?

A

Allan Ebdrup

Hi
I've created a asp.net 2.0 page with an Page_Init eventhandler that I want
to share among multiple pages, what's the best approach for this? Should I
inherit from the page, or is it better to create a library with the
eventhandler code and insert a call to the library on Page_Init on all pages
that should implement this feature?
I would also like to be able to create maste pages that support the same
functionality.

Kind Regards,
Allan Ebdrup
 
D

Dan

Its difficult to advise without knowing what your pageinit is doing.

For example if you are just setting a load of variables to starting values
then there are a few ways, you could store these contants in an xml file and
read those vars in on application start?

Can you provide some more info? Maybe some code of the pageinit method.
 
D

DavidG

I would put the handler in a class in the app_code folder and use a
delegate in the page_init handlers on the page to call the shared
handler function

HTH
 
A

Allan Ebdrup

The page Init swaps some controls if there is a special control defined
instead:
---
protected void Page_Init(object sender, EventArgs e)
{
int intPartner = System.Convert.ToInt32(Request["partner"]);
if (intPartner > 0)
{
CheckControlsForSwap(intPartner, Page.Controls);
}
}
protected void CheckControlsForSwap(int intPartner, ControlCollection
controlCollection)
{
foreach (Control c in controlCollection)
{
//Response.Write("c.GetType:" + c.GetType().ToString() + "<br>");
if (c.GetType().ToString().Substring(0, 13) == "ASP.controls_")
{
/* convert a string like "ASP.controls_default_dynamicload_ascx" to:
* "controls/partner/[partnerid]/dynamicload.ascx"
*/
string strControlPath = c.GetType().ToString();
//check that this is not already a partner specific control
if (strControlPath.IndexOf(intPartner.ToString()) == -1)
{
strControlPath = strControlPath.Replace("ASP.", "");
strControlPath = strControlPath.Replace("_ascx", ".ascx");
strControlPath = strControlPath.Replace("default", "partner/" +
intPartner.ToString());
strControlPath = strControlPath.Replace("_", "/");
//load partner control
Control partnerControl = null;
bool blnLoaded = true;
try
{
partnerControl = Page.LoadControl(strControlPath);
}
catch (System.Web.HttpException)
{
blnLoaded = false;
}
if (blnLoaded)
{
//we have loaded a partner specific control and want to swap it with
hte default control
int intControlIndex = controlCollection.IndexOf(c);
//remove the default control
controlCollection.Remove(c);
//add partner control
controlCollection.AddAt(intControlIndex, partnerControl);
//call CheckControlsForSwap on the same collection again to swap other
controls in this collection
CheckControlsForSwap(intPartner, controlCollection);
//return because the enumerater can't continue when we've added and
removed controls from the collection
return;
}
}
}
if (c.HasControls()) CheckControlsForSwap(intPartner, c.Controls);
}
}
---

This works fine when I run it directly in a page, but when using a maste
page the Request["partner"] is empty even though the master page contains a
dropdownlist with the ID "partner" that isn't empty.

Kind Regards,
Allan Ebdrup
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top