gpt4 book ai didi

c# - 文件未正确读取 Visual Studio 中 ASPX 文件的输入

转载 作者:行者123 更新时间:2023-11-28 03:42:36 24 4
gpt4 key购买 nike

这是我的问题。我有一个任务,我必须编写一个简单的 ASPX 页面,让用户输入 3 个文本框并单击一个计算按钮,该按钮将返回请求的项目数的结果。这就是我所拥有的。线假设将用户输入保存到 ID txtPlateQty 中,在下面的 C# 代码中,这是 visual studio 2010 中的一个单独文件,我想获取用户输入并将其转换为 int 进行计算。问题出在 C# 文件中,“Convert.ToInt32(txtPlateQty.Text); 无法识别变量“txtPlateQty”,就好像它超出了范围。这是一个类作业,我的教授有相同的例子和他的作品而我的则没有。如果有人能阐明导致此问题的原因,我将不胜感激。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CashRegister.aspx.cs" Inherits="ProjectX.CashRegister" %>

<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPlateQty" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSomethingElse1" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSomethingElse2" runat="server"></asp:TextBox>
<asp:Button ID="btnCalculate" runat="server" Text="Button" onclick="btnCalculate_Click" />
</div>
</form>
</body>
</html>

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


public partial class CashRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnCalculate_Click(object sender, EventArgs e)
{
int plateQty = Convert.ToInt32(txtPlateQty.Text);
int somethingElse1 = Convert.ToInt32(txtSomethingElse1.Text);
int somethingElse2 = Convert.ToInt32(txtSomethingElse2.Text);
}
}

<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation defaultLanguage="C#" debug="true"></compilation>
</system.web>
</configuration>

最佳答案

您的 CashRegister.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 CashRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnCalculate_Click(object sender, EventArgs e)
{
int plateQty = Convert.ToInt32(txtPlateQty.Text);
int somethingElse1 = Convert.ToInt32(txtSomethingElse1.Text);
int somethingElse2 = Convert.ToInt32(txtSomethingElse2.Text);
}
}

您的 CashRegister.aspx 标记应如下所示:

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

<!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>
<asp:TextBox ID="txtPlateQty" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSomethingElse1" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSomethingElse2" runat="server"></asp:TextBox>
<asp:Button ID="btnCalculate" runat="server" Text="Button" OnClick="btnCalculate_Click" />
</div>
</form>
</body>
</html>

Web.config

<?xml version="1.0"?>

<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>

<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>

</configuration>

关于c# - 文件未正确读取 Visual Studio 中 ASPX 文件的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9406134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com