0

I have imported a custom control to ASP.NET (C#), the control name is HijriGregDatePicker.ascx which has a pop-up calendar on both Islamic and Gregorian Calendars. My Problem when Compiling HijriGregDatePicker.ascx.cs I receive many errors of the type

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

While inside the ASCX file the calendar exists as below:its inside a DIV in a PANEL

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

One of the solutions I found on the web was importing all Web references like below

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

But this didn't help, another solution was the inheritance in HijriGregDatePicker.ascx

Which was like this:

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

I tried removing the inheritance tag, but still the same.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109

2 Answers2

1

The Inherits="HijriGregDatePicker.ascx" property assignment is totally wrong and causing non-existent control name errors (you cannot use ASCX file extension as inheritance base to another user control). Use proper class name which has same name to code-behind file namespace like example below:

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

Also worth to note that UserControl class inheritance does not include front-end visual elements like asp:Calendar, you need to use Register directive to use/show that base calendar control inside another user control (see this issue):

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

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

Related issue: How to inherit a UserControl from another UserControl?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

Since it's an ascx file, you may not necessarily use a code behind. Create a script right in the ascx page

<script runat="server">

</script>

When you add runat="server", the script won't appear in the browser source code. To avoid mistakes, use intellisence to create new control for OnSelectionChange=""

PopGlintz
  • 15
  • 4