0

The lines where the Control is referenced by TodayIs1 in the .aspx.cs file are throwing the error ; "The name 'TodayIs1' does not exist in the current context". I have tried a number of suggestions made in previous questions similar to mine. Any help would be great, thanks!

.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex == 0)
        {
            TodayIs1.Format = TodayIs.DateFormat.TheShortD;
        }
        else if (RadioButtonList1.SelectedIndex == 1)
        {
            TodayIs1.Format = TodayIs.DateFormat.LongD;
        }
    }
}

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register TagPrefix="epm" TagName="TodayIs" Src="~/TodayIs.ascx" %>
<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <epm:TodayIs ID="TodayIs1" runat="server" />
    </div>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
        <asp:ListItem>Short Date</asp:ListItem>
        <asp:ListItem>Long Date</asp:ListItem>
    </asp:RadioButtonList>
    </form>
</body>
</html>

.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebUserControl : System.Web.UI.UserControl
{
    public enum DateFormat { LongD, ShortD }
    private DateFormat format;
    public DateFormat Format { get { return format; } set { format = value; }}

    protected void Page_Load(object sender, EventArgs e)
    {
        if (format == DateFormat.ShortD){ 
            Label1.Text = "Today is " + DateTime.Now.ToShortDateString(); 
        }
        else if (format == DateFormat.LongD)
        {
            Label1.Text = "Today is " + DateTime.Now.ToLongDateString();
        }
    }
}

.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TodayIs.ascx.cs" Inherits="WebUserControl" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
onzymorg
  • 19
  • 8

0 Answers0