Professional Web Applications Themes

Newbie: Multiple codebehind files - ASP.NET General

Hi All, I'm not certain this can be done but it sure seems like it. I'd like to use multiple C# codebehind files in an aspx page. The idea is that a number of common functions, like wiring data sources, could be done in a similar fashion for various controls across multiple pages. What makes sense to me then would be to have an aspx page that uses a codebehind file. That file would reference classes in a second file that did the common work. Assuming I'm on the right track, how would you do this? Is it enough to ...

  1. #1

    Default Newbie: Multiple codebehind files

    Hi All,

    I'm not certain this can be done but it sure seems like it. I'd like to use
    multiple C# codebehind files in an aspx page. The idea is that a number of
    common functions, like wiring data sources, could be done in a similar
    fashion for various controls across multiple pages. What makes sense to me
    then would be to have an aspx page that uses a codebehind file. That file
    would reference classes in a second file that did the common work.

    Assuming I'm on the right track, how would you do this? Is it enough to
    simply have both .cs files in the same folder or should each be referenced
    in the Page's Inherits and Src attributes? Basically, I think the idea
    should work, but I'm missing the details on referencing and location.

    For example, a control:

    <asp:DropDownList id="ddlWidgets" runat="server" />

    would be referenced in the codebehind's Page_Load method:

    protected void Page_Load( Object Source, EventArgs Args )
    {
    if (!IsPostBack)
    {
    WireUpControl( ddlWidgets, "Widgets" );
    }
    }

    where WireUpControl is a method in a class located in a different file that
    takes a reference to the control and a table name and binds that table's
    data to the control.

    TIA,

    John


    John Guest

  2. #2

    Default Re: Newbie: Multiple codebehind files

    You don't need multiple code behinds, just multiple class files if you
    will... In which case you can add event handlers etc...

    you could have nothing but your event handlers in your code behind that make
    reference to other classes.. or, if you have a page that your going to use a
    lot of functions a lot.... inherit it from system.web.ui.page, creating
    your own class with those functions you want.

    Then inherit that page into all your otherpages... boom...

    This is really effective if you have say a search bar on every page... Just
    for example.

    -CJ
    "John Spiegel" <com> wrote in message
    news:phx.gbl... 
    use 
    of 
    me 
    that 


    CJ Guest

  3. #3

    Default Re: Newbie: Multiple codebehind files

    Hi John,

    Speaking from a C# background, and in an OO sense certainly, you should not
    be able to inherit from more than one superclass (unless you are using C or
    something). So it makes sense to assume you cannot inherit from more than
    one 'codebehind'.

    The usual practice is to place commonly used functions in a different
    abstract superclass, further up the heirarchy. However, don't lean too
    heavily on heirarchies, as they can become difficult to maintain in an
    extensibility sense. Consider instead using the strategy pattern as an
    alternative?

    hth,
    Ben

    "John Spiegel" <com> wrote in message
    news:phx.gbl... 
    use 
    of 
    me 
    that 


    Ben Guest

  4. #4

    Default Re: Newbie: Multiple codebehind files

    "John Spiegel" <com> wrote in message
    news:phx.gbl... 
    use 
    of 
    me 

    John,

    There are "CodeBehind" files, which are the .aspx.cs files. These are the
    "other side" of the .aspx files.

    Your web project can contain classes which are not related to a single page.
    You can always add these separate classes in .cs files and then reference
    them from your codebehind (.aspx.cs) files.
    --
    John Saunders
    Internet Engineer
    com


    John Guest

  5. #5

    Default Re: Newbie: Multiple codebehind files

    Thanks guys. Just the kind of stuff I'm needing.

    Desperately looking forward to the day when I can honestly drop the
    "Newbie:" tag on my subjects... <g>

    - John

    "John Spiegel" <com> wrote in message
    news:phx.gbl... 
    use 
    of 
    me 
    that 


    John Guest

Similar Threads

  1. Combine Multiple Files into 1 Layered Files
    By Xavier_Wynn@adobeforums.com in forum Adobe Photoshop Mac CS, CS2 & CS3
    Replies: 6
    Last Post: June 26th, 11:41 PM
  2. dynamically create multiple instances of usercontrol in codebehind
    By Padam in forum ASP.NET Building Controls
    Replies: 3
    Last Post: August 28th, 03:58 PM
  3. multiple datagrids dynamically from the codebehind
    By DaveF in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: May 5th, 05:20 AM
  4. codebehind files in different location
    By Maurice in forum ASP.NET General
    Replies: 6
    Last Post: August 26th, 08:55 PM
  5. Cocoa newbie -- help with multiple windows
    By Larry Fransson in forum Mac Programming
    Replies: 11
    Last Post: August 15th, 07:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139