user798080
user798080

Reputation:

C# Inheritance Issues

Again, I'm fairly new at this sort of thing and perhaps the error message is telling me what it is and I'm simply not understanding, but... This code in anon.cs

namespace cheese.pies.org
{
    using System;
    using System.IO;
    using System.Net;
    using System.Web;
    using System.Xml;

    public class CasLogin : System.Web.UI.Page
    {
        private const string CasHost = "notimportant";

        public static string GetId()
        {
        }
}

Ends up giving me an error when referenced here:

<% @Page Language="C#" Inherits="CasLogin" CodeFile="CasLogin.cs" %>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e) {
    String directoryId = CasLogin.GetId();
    FormsAuthentication.RedirectFromLoginPage(directoryId, false);
}
</script>

The error is on line one and is:

Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

If I change it from

public class CasLogin : System.Web.UI.Page

to

public class CasLogin : Page

I get this error:

Compiler Error Message: CS0246: The type or namespace name 'Page' could not be found (are you missing a using directive or an assembly reference?)

Upvotes: 2

Views: 1835

Answers (4)

Nikki9696
Nikki9696

Reputation: 6348

Looking at my own little test project/code, I see a few things noticably different: the "usings" come before the namespace declaration, and the use of partial class for code behind. Also, note that the Inherits has the fully qualified class (includes namespace).

in Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

in Default.aspx.cs

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Data;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {

Upvotes: 0

Tim M.
Tim M.

Reputation: 54378

If I change it from public class CasLogin : System.Web.UI.Page to public class CasLogin : Page I get this error

You are missing the correct using statement for the Page class (System.Web.UI) thus when you remove the full qualification the compiler can no longer find the Page class.

You should also fully-qualify the class name in the Page directive, i.e. Inherits="cheese.pies.org.CasLogin"

Good

<% @Page Language="C#" Inherits="cheese.pies.org.CasLogin" CodeFile="CasLogin.cs" %>

Bad

<% @Page Language="C#" Inherits="CasLogin" CodeFile="CasLogin.cs" %>

Per your comment regarding a missing partial modifier:

public class CasLogin : System.Web.UI.Page

Should be:

public partial class CasLogin : System.Web.UI.Page

This tells the compiler that the CasLogin class is defined in multiple files (which is the case with web forms; the designer file is separate from the code behind file).

If it still doesn't work, I suggest recreating the page and copying any relevant code into it. Normally Visual Studio handles all of this automatically and this is a non-issue.

Upvotes: 4

Chris Gessler
Chris Gessler

Reputation: 23113

inherits needs to be:

Inherits="cheese.pies.org.CasLogin"

Upvotes: 1

wegrata
wegrata

Reputation: 1656

I'm pretty sure it should be

<% @Page Language="C#" Inherits="cheese.pies.org.CasLogin" CodeFile="CasLogin.cs" %>

For the other error you can use

using System.Web.UI;

public class CasLogin : Page

Without fully qualifying the class name it doesn't know which Page you're talking about, so you can be explicit when you declare the class or you can use a using statement. The first error is the same issue, it can't see the CasLogin class inside the chees.pies.org namespace

Upvotes: 1

Related Questions