Home All Groups Group Topic Archive Search About

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

Author
13 Jan 2006 10:03 AM
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

Author
13 Jan 2006 10:57 AM
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.

--
Dan
Show quoteHide quote
"Allan Ebdrup" <com***@ofir.com> wrote in message
news:OsPmViCGGHA.2300@TK2MSFTNGP15.phx.gbl...
> 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
>
Are all your drivers up to date? click for free checkup

Author
13 Jan 2006 11:44 AM
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
Author
13 Jan 2006 11:40 AM
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

-----------------------------------------
David Gray
ASP.NET/C# Developer/Architect (MCAD.NET)

Show quoteHide quote
On Fri, 13 Jan 2006 11:03:18 +0100, "Allan Ebdrup" <com***@ofir.com>
wrote:

>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
>

Bookmark and Share