Associating a single code behind file with 2 aspx pages

Last post 05-20-2008 2:34 AM by karang. 7 replies.

Sort Posts:

  • Associating a single code behind file with 2 aspx pages

    05-17-2008, 5:39 AM
    • Loading...
    • karang
    • Joined on 04-20-2007, 7:57 AM
    • Gurgaon, India
    • Posts 218

    Hi

     

    Is it possible to associate 2 aspx file with a single code behind files.

     

    Regards

    Karan     

    For .net,Javascript and Sql server articles and interview question visit


    http://guptakaran21.googlepages.com/mainpage
  • Re: Associating a single code behind file with 2 aspx pages

    05-17-2008, 6:37 AM
    • Loading...
    • joteke
    • Joined on 06-16-2002, 11:24 AM
    • Kyro, Finland
    • Posts 6,490
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Yes,

    just set it properly in @Page directive of the Page (e.g that both pages point to the same codebehind file and are set to derive from the same code-behind type). Note also the existence of CodeFileBaseClass attribute in case you think you need some sort of base class in this scenario:

    CodeFileBaseClass in @Control and @Page directives (via a sample)
    http://aspadvice.com/blogs/joteke/archive/2006/03/08/15655.aspx

     

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: Associating a single code behind file with 2 aspx pages

    05-17-2008, 7:02 AM

     Either you can manually change the CodeFile value in the <%@ Page Directive or you can use Code Directory which is a standard option new in ASP 2.0.

    Please follow this link for details: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/pages/code.aspx

  • Re: Associating a single code behind file with 2 aspx pages

    05-20-2008, 12:01 AM
    Answer

    Hi,

    Page1: 

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="page.aspx.cs" Inherits="_Default" %>

    Page2:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="page.aspx.cs" Inherits="_Default" %>

    page.aspx.cs:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }

    page.aspx.cs will be as the file Code Behind for Page1 and Page2.

    Hope it helps.

     

    Sincerely,
    Vince Xu
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
  • Re: Associating a single code behind file with 2 aspx pages

    05-20-2008, 12:46 AM
    • Loading...
    • karang
    • Joined on 04-20-2007, 7:57 AM
    • Gurgaon, India
    • Posts 218

    Hi

    I am getting an error

    Could not load type 'Default'.

    Regards

    Karan

    For .net,Javascript and Sql server articles and interview question visit


    http://guptakaran21.googlepages.com/mainpage
  • Re: Associating a single code behind file with 2 aspx pages

    05-20-2008, 1:08 AM

     Please provide the code you are using. Smile

  • Re: Associating a single code behind file with 2 aspx pages

    05-20-2008, 1:11 AM
    Answer

    change the code as following ---- Since Default is a keyword in c#

    Page1:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="page.aspx.cs" Inherits=_Default" %>

    Page2:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="page.aspx.cs" Inherits="_Default" %>

    page.aspx.cs:

    public partial class _Default : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e) { }

    }

    page.aspx.cs will be as the file Code Behind for Page1 and Page2.

  • Re: Associating a single code behind file with 2 aspx pages

    05-20-2008, 2:34 AM
    • Loading...
    • karang
    • Joined on 04-20-2007, 7:57 AM
    • Gurgaon, India
    • Posts 218

    Hi

    Working fine

     

    Thanks

    Karan

    For .net,Javascript and Sql server articles and interview question visit


    http://guptakaran21.googlepages.com/mainpage
Page 1 of 1 (8 items)