文本框在当前上下文中不存在

时间:2015-04-11 13:44:59

标签: c# asp.net textbox

为页面上的每个文本框收到错误,说明它在default.aspx.cs的当前上下文中不存在,即使它们存在于default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <link rel="stylesheet" type="text/css" href="css/bootstrap.css" media="all" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.css" media="all" />
    <link rel="stylesheet" type="text/css" href="css/style.css" media="all" /> 
</head>
<body>

    <form id="form1" runat="server">

        <table>
            <tr>
                <td><asp:Label ID="lblTemperature" runat="server" Text="Temperature"></asp:Label></td>
                <td><asp:TextBox ID="txtTemp" runat="server" Height="20px" Width="101px"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:Button ID="btnCalc" runat="server" OnClick="Button1_Click" Text="Calculate Score" /></td>
                <td><asp:Label ID="lblScore" runat="server" Text=""></asp:Label></td>
            </tr>
            <tr>
                <td><asp:Label ID="lblAlert" runat="server" Text=""></asp:Label></td>
            </tr>
        </table>

    </form>
</body>
</html>

和我的default.aspx.cs每次抛出错误,说明当前上下文中不存在文本框和标签

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

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}


protected void Button1_Click(object sender, EventArgs e)
{       
        int score;
        score = 0;
        //CHECK TEMP IS VALID
        double Temp;
        if (double.TryParse(txtTemp.Text, out Temp))
        { //SUCCESS
        }
        else
        {
            //FAIL
        }

        //TEMP RATE CALC
        if (Temp <= 35.0)
        {
            score = score + 3;
        }
        else if (Temp >= 35.1 && Temp <= 36.0)
        {
            score = score + 1;
        }
        else if (Temp >= 36.1 && Temp <= 38.0)
        {
            score = score + 0;
        }
        else if (Temp >= 38.1 && Temp <= 39.0)
        {
            score = score + 1;
        }
        else if (Temp >= 39.1)
        {
            score = score + 2;
        }

        //WriteScore
        lblScore.Text = score.ToString();


        if (score <= 1)
        {
            lblAlert.Text = "GREEN";
            lblAlert.ForeColor = Color.Green;
        }
        else if (score >= 2 && score <= 4)
        {
            lblAlert.Text = "AMBER";
            lblAlert.ForeColor = Color.Orange;
        }
        else if (score >= 5)
        {
            lblAlert.Text = "RED";
            lblAlert.ForeColor = Color.Red;

        }

}

}

2 个答案:

答案 0 :(得分:0)

尝试清洁解决方案,然后重新构建!

答案 1 :(得分:0)

只需在设计表单中再添加一个文本框,即可立即解决此错误,您可以删除新文本框