Monday, March 26, 2012

simple code behind problem

I was following a simple tutorial for the WebMatrix using C# and I came across a problem. I compiled Sample1.cs in c prompt using "csc /target:library /out:BIN\Sample1.dll Sample1.cs".
When I ran Sample1.aspx I got this error.

Parser Error Message: 'CSample' is not a valid base class because it does not extend class 'System.Web.UI.Page'.

but if you look at my code closely it clearly does...

--------Sample1.aspx---------
<%@dotnet.itags.org. Page Language="C#" codebehind="Sample1.cs" Inherits="CSample" autoeventwireup="false"%
<html>
<head>
</head>
<body>
<form runat="server" id="frmWebForm" method="post">
<asp:TextBox runat="server" id="txtName"></asp:TextBox>
<asp:Button runat="server" id="btnSubmit" Text="Submit"></asp:Button>
</form>
</body>
</html>
------------------------

--------Sample1.cs-------------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public class CSample : Page
{
protected TextBox txtName;
protected Button btnSubmit;
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
Response.Write(Request.Form["txtName"]);
}
}

override protected void OnInit(EventArgs e)
{
InitializeComponents();
base.OnInit(e);
}

protected void InitializeComponents()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
---------------------Sorry, I didn't read the original post on using the <code > </code > formatter
*bump*
You need to declare the namespace in the code behind file. Just before you declare your class include the line "Namespace Test". Then in your aspx file, modify the Inherits declaration to "Test.CSample". It looks like this should solve your problem.

0 comments:

Post a Comment