Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Subclassed Control on an ASP.NET Page?

I've subclassed DropDownList to add functionality specific to my application:

public class MyDropDownList : DropDownList
{
    ...
}

... then referenced it in Web.Config, which is where I figure things start to go wrong:

<pages theme="Main">
    <controls>
        <add tagPrefix="bob" tagName="MyDropDownList" src="~/Components/MyDropDownList.cs" />
    </controls>
</pages>

my reference to it does not work:

<tr><td>Category</td>
   <td><bob:MyDropDownList runat="server" ID="Category"... />

and my best clue is the complier error message:

"The file 'src' is not a valid [sic] here because it doesn't expose a type."

I figure I'm misapplying knowledge of how to create a Web User Control here. What I want to be able to do is refer to this control on an ASP.NET page just like I would the parent DropDownList. Refactoring back into a Web User Control that contains a DropDownList is not desirable, because I want to apply a RequiredFieldValidator to it.

like image 852
Bob Kaufman Avatar asked Mar 12 '10 13:03

Bob Kaufman


2 Answers

<pages theme="Main">
    <controls>
        <add tagPrefix="bob" namespace="MyProject" assembly="MyProject" />
    </controls>
</pages>

That should do the trick.

like image 105
Joop Avatar answered Nov 14 '22 21:11

Joop


@Joops answer saved me.

What I did differently was to register the namespace at the top of my page because I didn't need it everywhere.

ie.

<%@ Register TagPrefix="myTagPrefix" Namespace="MySolution.MyProject.Foo.Bar"
        Assembly="MySolution.MyProject" %>

cheers Joop!

like image 33
Pure.Krome Avatar answered Nov 14 '22 23:11

Pure.Krome