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

Discussion in 'ASP .Net' started by Allan Ebdrup, Jan 13, 2006.

  1. Allan Ebdrup

    Allan Ebdrup Guest

    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
    Allan Ebdrup, Jan 13, 2006
    #1
    1. Advertising

  2. Allan Ebdrup

    Dan Guest

    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
    "Allan Ebdrup" <> wrote in message
    news:...
    > 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
    >
    Dan, Jan 13, 2006
    #2
    1. Advertising

  3. Allan Ebdrup

    DavidG Guest

    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)

    On Fri, 13 Jan 2006 11:03:18 +0100, "Allan Ebdrup" <>
    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
    >
    DavidG, Jan 13, 2006
    #3
  4. Allan Ebdrup

    Allan Ebdrup Guest

    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
    Allan Ebdrup, Jan 13, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bigbob
    Replies:
    1
    Views:
    346
    Cowboy \(Gregory A Beamer\)
    Sep 12, 2003
  2. Denny
    Replies:
    1
    Views:
    761
  3. Anirudha24.d
    Replies:
    0
    Views:
    1,694
    Anirudha24.d
    Feb 8, 2011
  4. bigbob

    Common problem... What is the best solution?

    bigbob, Sep 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    101
    Cowboy \(Gregory A Beamer\)
    Sep 12, 2003
  5. Jason Mellone
    Replies:
    3
    Views:
    78
    Jurko Gospodnetić
    May 7, 2014
Loading...

Share This Page