CS0103:该名称在当前上下文中不存在,对于页面中的所有控件?

CS0103: The name does not exist in the current context, For all controls in page?

我已将自定义控件导入 ASP.NET (C#),控件名称为 HijriGregDatePicker.ascx,它在伊斯兰历和公历上都有一个弹出式日历。我在编译时遇到的问题 HijriGregDatePicker.ascx.cs 我收到了很多

类型的错误

Error CS0103 The name 'ctlCalendarLocalized' does not exist in the current context certificate \certificate\HijriGregDatePicker.ascx.cs

虽然在 ASCX 文件中,日历在 PANELDIV 中以 below:its 形式存在

<asp:Calendar ID="ctlCalendarLocalized" runat="server" BackColor="White" 
                BorderColor="White" 
                Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="125px"  
                Width="275px" BorderWidth="1px" NextPrevFormat="FullMonth" 
                ShowDayHeader="True" ShowNextPrevMonth="False"
                OnSelectionChanged="ctlCalendarLocalized_SelectionChanged">
                <DayHeaderStyle Font-Bold="True" Font-Size="7pt"  Font-Names="Tahoma" />
                <NextPrevStyle VerticalAlign="Bottom" Font-Bold="True" Font-Size="8pt" 
                    ForeColor="#333333" />
                <OtherMonthDayStyle ForeColor="#999999" />
                <SelectedDayStyle BackColor="#333399" ForeColor="White" />
                <TitleStyle BackColor="White" BorderColor="Black" Font-Bold="True" 
                    BorderWidth="1px" Font-Size="9pt" ForeColor="#333399" BorderStyle="Ridge" Font-Names="sans-serif" />
                <TodayDayStyle BackColor="#CCCCCC" />
</asp:Calendar>

我在网上找到的解决方案之一是导入所有 Web 引用,如下所示

using System;
using System.Drawing;
using System.Web.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

但这没有帮助,另一个解决方案是 HijriGregDatePicker.ascx

中的继承

原来是这样的:

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

我试过删除继承标签,但还是一样。

Inherits="HijriGregDatePicker.ascx" 属性 分配完全错误并导致不存在的控件名称错误(您不能使用 ASCX 文件扩展名作为另一个用户控件的继承基础)。使用与代码隐藏文件命名空间同名的正确 class 名称,如下例所示:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="HijriGregDatePicker.ascx.cs" 
Inherits="YourNamespace.HijriGregDatePicker" %>

另外需要注意的是UserControl class继承不包括asp:Calendar这样的前端视觉元素,需要使用Register指令来[=24] =] 另一个用户控件内的基本日历控件(参见 this issue):

<%@ Register TagPrefix="dtp" TagName="HijriGregDatePicker" Src="HijriGregDatePicker.ascx" %>

<dtp:HijriGregDatePicker ID="datepicker" runat="server" ...>
</dtp:HijriGregDatePicker>

相关问题:How to inherit a UserControl from another UserControl?

因为它是一个ascx文件,你不一定要使用隐藏代码。在 ascx 页面中创建一个脚本

<script runat="server">

</script>

添加runat="server"时,脚本不会出现在浏览器源代码中。 为避免错误,请使用智能为 OnSelectionChange=""

创建新控件