我怎样才能正确地让我的 'Calculate' 按钮计算 ASP.NET C# 中的总价和折扣金额?
How can I properly get my 'Calculate' button to calculate the total price and discount amount in ASP.NET C#?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace XEx02Quotation
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsValid)
{
int SalesPrice = Convert.ToInt32(TextBox1.Text);
double DiscountPercentage= Convert.ToDouble(TextBox2.Text);
double discountAmount = Convert.ToDouble(Label1.Text);
double TotalPrice = Convert.ToDouble(Label2.Text);
double discountValue = this.CalculateDiscountValue(SalesPrice, DiscountPercentage, discountAmount);
double TotalValue = this.TotalPriceCalculate(TotalPrice, SalesPrice, discountAmount);
Label1.Text = discountValue.ToString("c");
Label2.Text = TotalPrice.ToString("c");
}
}
protected double CalculateDiscountValue(int SalesPrice, double DiscountPercentage, double discountAmount)
{
discountAmount = SalesPrice * DiscountPercentage;
return discountAmount;
}
protected double TotalPriceCalculate(double TotalPrice, int SalesPrice, double discountAmount)
{
TotalPrice = SalesPrice - discountAmount;
return TotalPrice;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XEx02Quotation.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Price quotation</title>
<style type="text/css">
.auto-style1 {
width: 54%;
height: 213px;
}
.auto-style15 {
width: 114px;
height: 23px;
}
.auto-style16 {
width: 114px;
}
.auto-style17 {
width: 114px;
height: 28px;
}
.auto-style18 {
width: 193px;
height: 23px;
}
.auto-style20 {
width: 193px;
height: 28px;
}
.auto-style21 {
width: 193px;
}
.auto-style22 {
margin-left: 12px;
}
.auto-style23 {
margin-left: 16px;
}
.auto-style25 {
width: 193px;
height: 5px;
}
.auto-style26 {
width: 114px;
height: 5px;
}
.auto-style27 {
width: 143px;
height: 23px;
}
.auto-style28 {
width: 143px;
height: 5px;
}
.auto-style29 {
width: 143px;
}
.auto-style30 {
width: 143px;
height: 28px;
}
.auto-style31 {
width: 143px;
height: 25px;
}
.auto-style32 {
width: 193px;
height: 25px;
}
.auto-style33 {
width: 114px;
height: 25px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h1>Price quotation</h1>
<br />
<br />
<table class="auto-style1">
<tr>
<td class="auto-style27">Sales Price</td>
<td class="auto-style18">
<asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style23" Width="173px" Font-Bold="True"></asp:TextBox>
</td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style27"></td>
<td class="auto-style18"></td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style27">Discount percent</td>
<td class="auto-style18">
<asp:TextBox ID="TextBox2" runat="server" CssClass="auto-style22" Width="169px"></asp:TextBox>
</td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style28">Discount amount</td>
<td class="auto-style25"> <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Label"></asp:Label>
</td>
<td class="auto-style26"></td>
</tr>
<tr>
<td class="auto-style27"></td>
<td class="auto-style18"></td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style30">Total price</td>
<td class="auto-style20">
<asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Label"></asp:Label>
</td>
<td class="auto-style17"></td>
</tr>
<tr>
<td class="auto-style31">
</td>
<td class="auto-style32"></td>
<td class="auto-style33"></td>
</tr>
<tr>
<td class="auto-style29">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Calculate" Width="90px" />
</td>
<td class="auto-style21"> </td>
<td class="auto-style16"> </td>
</tr>
</table>
</form>
</body>
</html>
大家好,
我已经在 Web 应用程序中创建了一个 ASP.NET 4.6 Web 表单,并且我使用 C# 来实现一些 Web 服务器控件的功能,但是我无法理解为什么当我点击 'Calculate' 按钮,当我在文本框中输入销售价格和折扣百分比时,它不会计算相应标签中的 折扣金额和总价 以粗体显示。这是我的 Default.aspx 代码和 Default.aspx.cs (C#) 代码。我做错了什么?
通过查看您的代码,我确定您遇到了异常。您正在尝试将 Label1.Text
转换为 Double,但 Label1.Text
没有任何值。
此外,您将该值传递给方法只是为了它被 returned。这没有意义。
如果您想 return 方法中的任何值,则不需要将其作为参数传递给该方法。
按如下方式更改您的代码。
protected void Button1_Click(object sender, EventArgs e)
{
if (IsValid)
{
int SalesPrice = Convert.ToInt32(TextBox1.Text);
double DiscountPercentage= Convert.ToDouble(TextBox2.Text);
double discountValue = this.CalculateDiscountValue(SalesPrice, DiscountPercentage);
double TotalPrice = this.TotalPriceCalculate(SalesPrice, discountValue);
Label1.Text = discountValue.ToString("c");
Label2.Text = TotalPrice.ToString("c");
}
}
protected double CalculateDiscountValue(int SalesPrice, double DiscountPercentage)
{
doubl discountAmount = SalesPrice * DiscountPercentage;
return discountAmount;
}
protected double TotalPriceCalculate(int SalesPrice, double disountAmount)
{
double TotalPrice = SalesPrice - discountAmount;
return TotalPrice;
}
这应该可以帮助您解决问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace XEx02Quotation
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsValid)
{
int SalesPrice = Convert.ToInt32(TextBox1.Text);
double DiscountPercentage= Convert.ToDouble(TextBox2.Text);
double discountAmount = Convert.ToDouble(Label1.Text);
double TotalPrice = Convert.ToDouble(Label2.Text);
double discountValue = this.CalculateDiscountValue(SalesPrice, DiscountPercentage, discountAmount);
double TotalValue = this.TotalPriceCalculate(TotalPrice, SalesPrice, discountAmount);
Label1.Text = discountValue.ToString("c");
Label2.Text = TotalPrice.ToString("c");
}
}
protected double CalculateDiscountValue(int SalesPrice, double DiscountPercentage, double discountAmount)
{
discountAmount = SalesPrice * DiscountPercentage;
return discountAmount;
}
protected double TotalPriceCalculate(double TotalPrice, int SalesPrice, double discountAmount)
{
TotalPrice = SalesPrice - discountAmount;
return TotalPrice;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XEx02Quotation.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Price quotation</title>
<style type="text/css">
.auto-style1 {
width: 54%;
height: 213px;
}
.auto-style15 {
width: 114px;
height: 23px;
}
.auto-style16 {
width: 114px;
}
.auto-style17 {
width: 114px;
height: 28px;
}
.auto-style18 {
width: 193px;
height: 23px;
}
.auto-style20 {
width: 193px;
height: 28px;
}
.auto-style21 {
width: 193px;
}
.auto-style22 {
margin-left: 12px;
}
.auto-style23 {
margin-left: 16px;
}
.auto-style25 {
width: 193px;
height: 5px;
}
.auto-style26 {
width: 114px;
height: 5px;
}
.auto-style27 {
width: 143px;
height: 23px;
}
.auto-style28 {
width: 143px;
height: 5px;
}
.auto-style29 {
width: 143px;
}
.auto-style30 {
width: 143px;
height: 28px;
}
.auto-style31 {
width: 143px;
height: 25px;
}
.auto-style32 {
width: 193px;
height: 25px;
}
.auto-style33 {
width: 114px;
height: 25px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h1>Price quotation</h1>
<br />
<br />
<table class="auto-style1">
<tr>
<td class="auto-style27">Sales Price</td>
<td class="auto-style18">
<asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style23" Width="173px" Font-Bold="True"></asp:TextBox>
</td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style27"></td>
<td class="auto-style18"></td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style27">Discount percent</td>
<td class="auto-style18">
<asp:TextBox ID="TextBox2" runat="server" CssClass="auto-style22" Width="169px"></asp:TextBox>
</td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style28">Discount amount</td>
<td class="auto-style25"> <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Label"></asp:Label>
</td>
<td class="auto-style26"></td>
</tr>
<tr>
<td class="auto-style27"></td>
<td class="auto-style18"></td>
<td class="auto-style15"></td>
</tr>
<tr>
<td class="auto-style30">Total price</td>
<td class="auto-style20">
<asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Label"></asp:Label>
</td>
<td class="auto-style17"></td>
</tr>
<tr>
<td class="auto-style31">
</td>
<td class="auto-style32"></td>
<td class="auto-style33"></td>
</tr>
<tr>
<td class="auto-style29">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Calculate" Width="90px" />
</td>
<td class="auto-style21"> </td>
<td class="auto-style16"> </td>
</tr>
</table>
</form>
</body>
</html>
大家好,
我已经在 Web 应用程序中创建了一个 ASP.NET 4.6 Web 表单,并且我使用 C# 来实现一些 Web 服务器控件的功能,但是我无法理解为什么当我点击 'Calculate' 按钮,当我在文本框中输入销售价格和折扣百分比时,它不会计算相应标签中的 折扣金额和总价 以粗体显示。这是我的 Default.aspx 代码和 Default.aspx.cs (C#) 代码。我做错了什么?
通过查看您的代码,我确定您遇到了异常。您正在尝试将 Label1.Text
转换为 Double,但 Label1.Text
没有任何值。
此外,您将该值传递给方法只是为了它被 returned。这没有意义。
如果您想 return 方法中的任何值,则不需要将其作为参数传递给该方法。
按如下方式更改您的代码。
protected void Button1_Click(object sender, EventArgs e)
{
if (IsValid)
{
int SalesPrice = Convert.ToInt32(TextBox1.Text);
double DiscountPercentage= Convert.ToDouble(TextBox2.Text);
double discountValue = this.CalculateDiscountValue(SalesPrice, DiscountPercentage);
double TotalPrice = this.TotalPriceCalculate(SalesPrice, discountValue);
Label1.Text = discountValue.ToString("c");
Label2.Text = TotalPrice.ToString("c");
}
}
protected double CalculateDiscountValue(int SalesPrice, double DiscountPercentage)
{
doubl discountAmount = SalesPrice * DiscountPercentage;
return discountAmount;
}
protected double TotalPriceCalculate(int SalesPrice, double disountAmount)
{
double TotalPrice = SalesPrice - discountAmount;
return TotalPrice;
}
这应该可以帮助您解决问题。