ASP.NET C#Web用户控件无法识别。 "名称' ControlName'在当前背景下不存在"

时间:2015-08-28 15:17:32

标签: c# asp.net webusercontrol visual-web-developer-2010 aspx-user-control

.aspx.cs文件中TodayIs1引用Control的行正在抛出错误; "名称' TodayIs1'在当前上下文中不存在"。我在以前的问题中尝试了一些类似于我的建议。任何帮助都会很棒,谢谢!

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

0 个答案:

没有答案