关于网页表单的问题?~~
发布网友
发布时间:2022-04-21 22:39
我来回答
共1个回答
热心网友
时间:2022-04-22 00:09
---------Default.aspx---------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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="Num1" runat="server" AutoPostBack="true" OnTextChanged="Text_Changed">0</asp:TextBox><br />
第二个数:<asp:TextBox ID="Num2" runat="server" AutoPostBack="true" OnTextChanged="Text_Changed">0</asp:TextBox><br />
第三个数:<asp:TextBox ID="Num3" runat="server" AutoPostBack="true" OnTextChanged="Text_Changed">0</asp:TextBox><br />
三数之和:<asp:TextBox ID="Sum" runat="server" ReadOnly="true"></asp:TextBox></div>
</form>
</body>
</html>
---------Default.aspx.cs----------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Text_Changed(sender, e);
}
}
protected void Text_Changed(object sender, EventArgs e)
{
double dSum,dNum1,dNum2,dNum3;
if (System.Double.TryParse(Num1.Text, out dNum1) &&
System.Double.TryParse(Num2.Text, out dNum2) &&
System.Double.TryParse(Num3.Text, out dNum3))
{
dSum = dNum1 + dNum2 + dNum3;
Sum.Text = dSum.ToString();
}
}
}