如何在 ASP.NET 页面上使用子类控件?子类、控件、页面、如何在

2023-09-07 15:25:02 作者:全球少女萌主

我已将 DropDownList 子类化以添加特定于我的应用程序的功能:

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

public class MyDropDownList : DropDownList
{
    ...
}

...然后在 Web.Config 中引用它,我认为事情开始出错了:

... 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."

我认为我在这里误用了有关如何创建 Web 用户控件的知识.我想要做的是在 ASP.NET 页面上引用这个控件,就像我引用父 DropDownList 一样.重构回包含 DropDownList 的 Web 用户控件是不可取的,因为我想对其应用 RequiredFieldValidator.

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.

推荐答案

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

这应该可以解决问题.