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

喜夏-厌秋 提交于 2019-12-19 15:02:21

问题


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.


回答1:


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

That should do the trick.




回答2:


@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!



来源:https://stackoverflow.com/questions/2432856/how-to-use-a-subclassed-control-on-an-asp-net-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!