5

我正在尝试创建一个继承自 ASP.Net 的内置日历用户控件的自定义日历控件。

我的控件的代码隐藏文件如下所示:

public partial class WeeklyEventsCalendar : Calendar
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

并编译得很好。

但是,当我尝试将自定义控件放在 aspx 页面上时:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testbed.aspx.cs" Inherits="testbed" %>
<%@ Register Src="UserControls/WeeklyEventsCalendar.ascx" TagName="WeeklyEventsCalendar"
    TagPrefix="mvs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <link href="~/css/VitalSignsStyleSheet.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <div>
        <mvs:WeeklyEventsCalendar runat="server" />
    </div>
</body>
</html>

我收到警告“元素 WeeklyEventsCalendar 不是已知元素。如果网站中存在编译错误,或者 web.config 文件丢失,则可能会发生这种情况。尝试

当我输入错误的文件位置时,我没有像过去那样收到任何类型的“找不到文件”错误。

当我尝试在浏览器中加载 aspx 页面时,出现错误 CS0115: 'ASP.usercontrols_weeklyeventscalendar_ascx.FrameworkInitialize()': no proper method found to override

这更令人困惑,因为我的代码中没有任何地方尝试定义这样的函数。

这应该很简单。我哪里错了?

4

2 回答 2

5

我认为问题在于您试图从用户控件(基于您的 ASCX 扩展)继承日历控件(这是一个服务器控件)。你不能这样做。如果要从 Calendar 控件继承,则需要创建一个服务器控件。

如果您需要一些示例代码,请告诉我。

于 2008-10-09T17:52:03.217 回答
4

Bryant hit on it. One thing you might consider if all you're doing is customizing the existing control is embedding an instance of the calendar on your user control and exposing the properties you need from it. This way your user control can handle all of the requisite customizations, and also provide only a limited interface back out to the consuming application. (This is composition instead of inheritance.)

If you really do need to fully derive from asp:Calendar, then you need to create a standard class which derives from the Calendar control, and then perform your customizations. You'll have no UI design for that, however -- everything you do will need to be custom code. (And if you need to change the HTML being emitted, you'll need to write those custom streams out as well -- which, with a calendar control, could be painful.)

于 2008-10-09T18:04:23.563 回答