Anj
Anj

Reputation: 1024

Inherit from ASP.NET ListView, "is not allowed here because it does not extend class 'System.Web.UI.UserControl'"

I'm attempting to create a custom control that inherits from System.Web.UI.WebControls.ListView. Codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Text;

namespace MyCompany.MyProject
{        //I've tried this without the "<T>" as well.  Either seems to compile
         //but neither works at runtime.
    public partial class MyListView<T> : ListView
    {
    ...
    }
}

Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyListView.ascx.cs" Inherits="MyCompany.MyProject.MyListView" %>

It all compiles, but when I try to load the page I get the following error:

"'MyCompany.MyProject.MyListView' is not allowed here because it does not extend class 'System.Web.UI.UserControl'. at System.Web.UI.TemplateParser.ProcessError(String message) at System.Web.UI.TemplateParser.ProcessInheritsAttribute(String baseTypeName, String codeFileBaseTypeName, String src, Assembly assembly) at System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary parseData) "

I have searched for quite some time trying to determine what is going wrong or for some kind of elegant work-around, but my searches have yielded nothing useful thus far.

Thanks in advance for taking the time to help me.

-A

Upvotes: 0

Views: 2243

Answers (3)

Ishmaeel
Ishmaeel

Reputation: 14373

Get rid of the markup. Add your inherited control as a simple class. If you reference the assembly properly, you can use your custom listview in other aspx pages without problems. Below is what I did with similar legacy code:

  • Add a plain MyListView.cs class to your project.
  • Copy all the code from your "codebehind" file to this new file.
  • Delete the original ascx, ascx.cs and ascx.designer.cs files.

In your aspx pages, reference the assembly like this:

<%@ Register Assembly="MyCompany.MyProject" Namespace="MyCompany.MyProject" TagPrefix="cc1" %>

And use your control in the markup like this:

<cc1:MyListView ID="MyListView1" runat="server" />

Upvotes: 1

Avinash
Avinash

Reputation: 25

As the error says , You have to inherit from 'System.Web.UI.UserControl' to create a custom user control. For extending a server control you have to create custom web server control not custom user control . The following link explain how to do this: Walkthrough: Creating a Web Custom Control

Upvotes: 1

scottm
scottm

Reputation: 28701

Your problem is that your markup defines a Control and a ListView is a WebControl.

Upvotes: 3

Related Questions