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.
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.
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.
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.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With