为什么在ASP.NET Web应用程序的当前上下文中找不到控件

时间:2014-05-23 18:02:49

标签: c# html asp.net

我正在尝试将我的网站转换为网络应用程序,但我遇到了一些阻止我部署它的错误。

我有以下.ASCX文件,它位于我的一个ASP.net页面中:

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

<div style="z-index: 15; position: relative; left: 0; padding-left: 20px; padding-top: 30px; font-weight: bold; color: #00A0BE;">
    LABORATORY TESTING CENTER HOURS
</div>
<div style="width: 100%; text-align: center; padding-left: 15px; padding-top: 25px;">
    <div style="font-weight: bold; color: #00A0BE; position: relative; margin: 0 auto; width: 280px; height: 125px; background: url('theImages/labHoursHeader.png') no-repeat;">
        <br />
        stchester Avenue<br />
    <span id="lhWP" runat="server">White Plains</span>, New York 10629<br />
        2.6580
    <div id="dvWP" runat="server" style="width: 100%; text-align: center; padding-top: 10px;"></div>
    </div>
    <div style="text-align: center; position: relative; margin: 0 auto; width: 280px; height: 80px; background: url('theImages/labHoursHeaderMiddle.png') repeat-y;">
        <div style="text-align: left; padding-left: 15px; width: 260px; margin: 0 auto;">
            Monday &amp; Thursday: 7AM - 7:30PM<br />
            Tuesday &amp; Wednesday: 7AM - 7PM<br />
            Friday: 7AM - 5:30PM<br />
            Saturday: 8AM - 1:30PM <br />
            Sunday: Closed
        </div>
    </div>
    <div style="position: relative; margin: 0 auto; width: 280px; height: 26px; background: url('theImages/labHoursHeaderFooter.png') no-repeat;">
    </div>
</div>

以下代码隐藏文件:

public partial class includeNav_labHours : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime now = DateTime.Now;
        //DateTime test = DateTime.Now.AddHours(8);
        string time = now.ToString("T");
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + time + "');", true);

        var wpInput = lhWP.InnerText;
        if (wpInput == "White Plains")
        {
            if (now.DayOfWeek == DayOfWeek.Monday || now.DayOfWeek == DayOfWeek.Thursday)
            {
                if (IsTimeOfDayBetween(now, new TimeSpan(7, 0, 0), new TimeSpan(19, 30, 0)))
                {
                    //OPEN MONDAY & THURSDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locOpen.png' alt='OPEN' title='Location is OPEN' width='60px' height='52px' />"));
                }
                else
                {
                    //CLOSE MONDAY & THURSDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locClose.png' alt='CLOSED' title='Location is CLOSED' width='60px' height='52px' />"));
                }
            }
            else if (now.DayOfWeek == DayOfWeek.Tuesday || now.DayOfWeek == DayOfWeek.Wednesday)
            {
                if (IsTimeOfDayBetween(now, new TimeSpan(7, 0, 0), new TimeSpan(19, 0, 0)))
                {
                    //OPEN TUESDAY & WEDNESDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locOpen.png' alt='OPEN' title='Location is OPEN' width='60px' height='52px' />"));
                }
                else
                {
                    //CLOSE TUESDAY & WEDNESDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locClose.png' alt='CLOSED' title='Location is CLOSED' width='60px' height='52px' />"));
                }
            }
            else if (now.DayOfWeek == DayOfWeek.Friday)
            {
                if (IsTimeOfDayBetween(now, new TimeSpan(7, 0, 0), new TimeSpan(17, 30, 0)))
                {
                    //OPEN FRIDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locOpen.png' alt='OPEN' title='Location is OPEN' width='60px' height='52px' />"));
                }
                else
                {
                    //CLOSE FRIDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locClose.png' alt='CLOSED' title='Location is CLOSED' width='60px' height='52px' />"));
                }
            }
            else if (now.DayOfWeek == DayOfWeek.Saturday)
            {
                if (IsTimeOfDayBetween(now, new TimeSpan(8, 0, 0), new TimeSpan(13, 30, 0)))
                {
                    //OPEN SATURDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locOpen.png' alt='OPEN' title='Location is OPEN' width='60px' height='52px' />"));
                }
                else
                {
                    //CLOSE SATURDAY
                    dvWP.Controls.Add(new LiteralControl("<img src='theImages/locClose.png' alt='CLOSED' title='Location is CLOSED' width='60px' height='52px' />"));
                }
            }
            else if (now.DayOfWeek == DayOfWeek.Sunday)
            {
                //CLOSE SUNDAY
                dvWP.Controls.Add(new LiteralControl("<img src='theImages/locClose.png' alt='CLOSED' title='Location is CLOSED' width='60px' height='52px' />"));
            }
        }
    }

    static public bool IsTimeOfDayBetween(DateTime time, TimeSpan startTime, TimeSpan endTime)
    {
        if (endTime == startTime)
        {
            return true;
        }
        else if (endTime < startTime)
        {
            return time.TimeOfDay <= endTime || time.TimeOfDay >= startTime;
        }
        else
        {
            return time.TimeOfDay >= startTime && time.TimeOfDay <= endTime;
        }
    }
}

当我运行应用程序时,我收到以下错误:

  

当前上下文中不存在名称“dvWP”c:\ Users \ z \ Documents \ Visual Studio 2012 \ Projects \ WMEXEC SLN \ WMEXEC \ includeNav \ urgCareTime.ascx.cs 23 17 WMEXEC

     

当前上下文中不存在名称“lhWP”c:\ Users \ z \ Documents \ Visual Studio 2012 \ Projects \ WMEXEC SLN \ WMEXEC \ includeNav \ urgCareTime.ascx.cs 23 17 WMEXEC

为什么我会收到该错误?以及如何修复它?

1 个答案:

答案 0 :(得分:1)

您也可以使用面板

<asp:Panel id="dvWP" runat="server">

一旦解析它将成为div,并且应该更容易访问。