Click here to Skip to main content
15,912,837 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
i want to create aspx page with code behind as a class file, which is located in class library,ie, precompiled class.
Posted

Hi there,

I don't think this is actually possible, however, you could create a normal ASPX page and code file and just get the code file to inherit from your pre-compiled class. That ought to work. Just make sure your pre-compiled class inherits from Page like the ASPX code file class does.

Example of ASPX code file class declaration:
public class MyPage : Page
goes to:
public class MyPage : MyLibraryPageClass
and your LibraryPageClass inherits page like so:
public class MyLibraryPageClass : Page

Hope this helps,
Ed


Edit: Sample Code

MyPage.aspx.cs file:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyPage : MyLibraryPage
{
}


MyLibraryPage.cs file:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LibraryPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Form.InnerHtml = "Hello world!";
    }
}
 
Share this answer
 
v2
Comments
Sanjeev Alamuri 17-May-12 8:57am    
please, can u elaborate this concept with sample code.
thanks in advance.
Ed Nutting 17-May-12 9:02am    
I have updated my solution with sample code of examples of class files you can use.
Ed

Edit: Please don't forget to up vote/accept solution. :)
Sanjeev Alamuri 17-May-12 9:27am    
Yeah, as u said is perfect but,in my project some aspx pages does not have aspx.cs files and those are accessing predefined compiled classes accordingly. can u tell me how it possible and how to create aspx page with out code behind page in VS2010.
Ed Nutting 17-May-12 9:32am    
Simple answer, I don't think you can directly have a library class as the aspx page class. You shouldn't at least. You just need to adapt this wrapper example to fit what you have. Your aspx pages all have code associated with them. Find it and adapt my sample. If the code is contained with the aspx file rather than a separate aspx.cs file then simply put in the aspx.cs sample code I gave, obviously adapt it for that specific page. It will do the exact same. You do not actually need the separate aspx.cs file.
Sanjeev Alamuri 17-May-12 9:58am    
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RefereClassLibrary.aspx.cs"
Inherits="BaseClassLibrary.RefereClassLibrary" %>
just i removed aspx.cs and referred the class library 'BaseClassLibrary' in page directive as above. here my class file name is 'RefereClassLibrary', and i have given same name for both aspx page and class. here CodeBehind="RefereClassLibrary.aspx.cs" same as while created the aspx page. its working finally.

thanks ED Nutting.
XML
yeah i too surprised, just look into the code

here is my aspx page without aspx.cs and aspx.designer.cs :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RefereClassLibrary.aspx.cs"
    Inherits="BaseClassLibrary.RefereClassLibrary" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>


and class in ClassLibrary:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LibraryPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Form.InnerHtml = "Hello world!";
    }
}




and i referred my Class Library to Project.finally got the solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900