Question About Code Behind

S

SAI

I have 2 files which are copied from Microsoft for testing code behind.
But always show error "Parser Error Message: Could not load type 'MyClass'."
and highlight the aspx file code, Is my server has problem? thx.

Line 1: <%@ Page Language="C#" Inherits="MyClass" %>

aspx file content :
<%@ Page Language="C#" Inherits="MyClass" %>
<html>
<head>
</head>
<body>
<form id="MyForm" runat="server">
<asp:textbox id="MyTextBox" runat="server" text="Hello
World"></asp:textbox>
<asp:button id="MyButton" onclick="MyButton_Click" runat="server"
text="Echo Input"></asp:button>
<asp:Label id="MyLabel" runat="server"></asp:Label>
</form>
</body>
</html>

cs file content :
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyStuff
{
public class MyClass : Page
{
protected System.Web.UI.WebControls.Label MyLabel;
protected System.Web.UI.WebControls.Button MyButton;
protected System.Web.UI.WebControls.TextBox MyTextBox;

public void MyButton_Click(Object sender, EventArgs e)
{
MyLabel.Text = MyTextBox.Text.ToString();
}
}
}
 
J

Juan T. Llibre

You must compile the code-behind before attempting to inherit.

Also, if you're inheriting from page, the format is as follows :

<%@ Import Namespace="MyNameSpace" %>
<%@ Page language="C#" Inherits="MyNameSpace.MyClass" %>

For you, that translates into :
<%@ Import Namespace="MyStuff" %>
<%@ Page language="C#" Inherits="MyStuff.MyClass" %>
 
S

SAI

I use following method to compile. Is it right? thanks.

csc.exe /out:mycodebehind.dll /t:library mycodebehind.cs
 
J

Juan T. Llibre

Yes, and if you need to reference a .Net Framework class,
add /r: classname.dll /r:anotherclassname.dll to the command :

csc.exe /t:library /r:system.dll /r:system.web.dll /out:cb.dll cb.cs

I shortened the filename and dll name to fit the command in one line.
Substitute your own names, of course.

If you want a number of files compiled into a single assembly, just put
all your .cs files into a directory and use a wildcard in the command :

csc.exe /t:library /r:system.dll /r:system.web.dll /out:cb.dll *.cs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top