Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET page without code behind use other library / DLL

Tags:

c#

.net

asp.net

dll

I'm adding a simple ASP.NET page to an existing page (i cannot alter/build the original project right now), that has no code behind file. All works well, except when i want to use some function that lives in another library within my projecct.

Example

<%@ Page Language="C#"  %>
<%@ Import Namespace="MyProject.BusinessLogic" %>
<HTML>
   <script runat="server" language="C#">
   public void Page_Load(object sender, EventArgs e)
   {
      bool myBool = false;
      MyLabel.Text = myBool.ToString();  //works fine
      MyLabel.Text = MyProject.BusinessLogic.StatusManager.Get().ToString(); //does not work
   }
   </script>
   <body>
      <form id="MyForm" runat="server">
         <asp:label id="MyLabel" runat="server"></asp:label>
      </form>
   </body>
</HTML>

The error i get is .. does not exist in the namespace 'MyProject.BusinessLogic' (are you missing an assembly reference?)

Any idea how to get this reference problem fixed?

I tried these options: http://msdn.microsoft.com/en-us/library/d864zc1k(v=vs.85).aspx , but with no luck.

like image 551
Tys Avatar asked Jul 29 '14 17:07

Tys


People also ask

How do you relate an ASPX page with its code behind page?

aspx page must specify to inherit from the code-behind base class. To do this, use the Inherits attribute for the @ Page directive. The . aspx page inherits from the code-behind class, and the code-behind class inherits from the Page class.

What is purpose of code behind?

Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your business logic.

Can we write C# code in ASPX page?

yes, you can write C# in aspx page. Yes, if you want write c# code in same aspx file. First time you add new item > web form > check/uncheck place code in separate file.


2 Answers

use <%@ Import %> for do this...

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.UI" %>

<script RunAt="server">


//here you can write your C# Code

 public void Page_Load(object sender, EventArgs e)
   {
      bool myBool = false;
      MyLabel.Text = myBool.ToString();  //works fine
      MyLabel.Text = MyProject.BusinessLogic.StatusManager.Get().ToString(); //does not work
   }



</script>
like image 173
user3786581 Avatar answered Oct 20 '22 12:10

user3786581


First off, your project has to reference the assembly in in the other dll that the code is in. This is accomplished with the "<%@ Assembly" tag,

For example

<%@ Assembly Name="SomeDll.NameHere.Dll" %>

However, note that the name there is a Full Type Name. E.g. that Dll has to be in the BIN directory of the app you want to use it in. Optionally you can install the dll in the global assembly cache, but then you have to give the dll a Strong Name with an SNK file (accomplished in the Class Libraries project settings)

If you have multiple projects in one visual studio solution, you can use project references and then reference the MyProject.BusinessLogic dll. When you build your web app it will copy the dll into the bin directory automatically.

Now maybe you already have the dll referenced in the project, but you still need the <% Assembly tag in your markup. The ASPX engine does not know about your dll.

Optionally you can register your dll for the ASPX engine with web.config entries.

Register assembly in ASP.NET (VS 2005) and web.config

like image 25
Ryan Mann Avatar answered Oct 20 '22 11:10

Ryan Mann