我正在使用VS 2015,我的编码语言是C#。我试图在aspx中创建一个简单的网站,产生服务器时间,并允许您更改时钟的颜色。无论如何,我有这个错误说"名称timeLabel在当前上下文中不存在"到目前为止,这是我的代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebTime.aspx.cs" Inherits="Web.WebTime" %>
<!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>Server Time</title>
<style type="text/css">
.timeStyle {
color: red;
font-size: xx-large;
background-color: blanchedalmond;
}
</style>
</head>
<body>
<div>
<h2>
Current time on the Web server:</h2>
<p>
<span id="timeLabel" runat="server" class="timeStyle" >02:55:28</span>
</p>
<p>
New background color:
<asp:DropDownList ID="backColorDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>Black</asp:ListItem>
<asp:ListItem>White</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Purple</asp:ListItem>
<asp:ListItem>Yellow</asp:ListItem>
</asp:DropDownList>
</p>
<p>
New foreground color:
<asp:DropDownList ID="foreColorDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>Yellow</asp:ListItem>
<asp:ListItem>Black</asp:ListItem>
<asp:ListItem>White</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Purple</asp:ListItem>
</asp:DropDownList>
</p>
<p>
New font size:
<asp:DropDownList ID="fontSizeDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>24</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
</asp:DropDownList>
</p>
</div>
</body>
</html>
这也是我的CodeBehind文件:
// Code-behind file for a page that displays the current time
// and updates properties based on user input.
using System;
using System.Drawing;
namespace Web
{
public partial class WebTime : System.Web.UI.Page
{
// initializes the contents of the page
protected void Page_Init(object sender, EventArgs e)
{
// display the server's current time in timeLabel
timeLabel.Text = DateTime.Now.ToString("hh:mm:ss");
} // end method Page_Init
// when loading due to a postback, Page_Load modifies
// the Label's properties based on user input
protected void Page_Load(object sender, EventArgs e)
{
// if this is a postback, process user input
if (IsPostBack)
{
// retrieve the values submitted by the user
string backColor = backColorDropDownList.SelectedItem.Text;
string foreColor = foreColorDropDownList.SelectedItem.Text;
int fontSize =
Convert.ToInt32(fontSizeDropDownList.SelectedItem.Text);
// modify timeLabel based on the user's selections
timeLabel.BackColor = Color.FromName(backColor);
timeLabel.ForeColor = Color.FromName(foreColor);
timeLabel.Font.Size = fontSize;
} // end if
}
}
}
错误发生在我的CodeBehind文件中。
&#34; timeLabel&#34;,&#34; backColorDropDownList&#34;,&#34; foreColorDropDownList&#34;,&#34; fontSizeDropDownList&#34;有错误。
任何帮助都会非常感激!
答案 0 :(得分:0)
您应该将asp控件封装在表单元素中:
...
<body>
<form runat="server">
<div>
<h2>
Current time on the Web server:</h2>
<p>
<span id="timeLabel" runat="server" class="timeStyle" >02:55:28</span>
</p>
<p>
New background color:
<asp:DropDownList ID="backColorDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>Black</asp:ListItem>
<asp:ListItem>White</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Purple</asp:ListItem>
<asp:ListItem>Yellow</asp:ListItem>
</asp:DropDownList>
</p>
<p>
New foreground color:
<asp:DropDownList ID="foreColorDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>Yellow</asp:ListItem>
<asp:ListItem>Black</asp:ListItem>
<asp:ListItem>White</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Purple</asp:ListItem>
</asp:DropDownList>
</p>
<p>
New font size:
<asp:DropDownList ID="fontSizeDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>24</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
</asp:DropDownList>
</p>
</div>
</form>
</body>
...
&#13;
答案 1 :(得分:0)
我不确定为什么你会在这么简单的事情上收到错误。只需粘贴以下代码即可显示当前时间并查看
for (int i = 0; ; i++) {
int a[][]=new int[i][i];
System.out.println(i + " " + Arrays.toString(a));
}
让我知道出现的错误消息