web程序设计 第三版 课后题答案 主编 吉根林 顾云华 u8877@qq.com

Web程序设计第3章课后题

注:课后题***7题(除第一题和第九题),其中5和8由于还有些问题没有解决,就没有将答案附上。这里的答案仅供参考,希望在上机之前能自己练习一下。程序有很多地方可以改,不要照搬。

(2)设计一个网页,其中包含TextBox和Button控件各一个。当在TextBox中输入一个成绩,再单击Button控件时在网页上输出相应的等级信息。

.aspx

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

<!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="TextBox1" runat="server">请输入一个成绩</asp:TextBox>

<asp:Label ID="Label1" runat="server" Text="Label">待显示</asp:Label>

<br />

<asp:Button ID="Button1" runat="server" OnClick = "btmSubmit_Click" Text="检测" />

</div>

</form>

</body>

</html>

.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace homework_chap3

{

public partial class question2 : System.Web.UI.Page

{

protected void btmSubmit_Click(object sender, EventArgs e)

{

int iInput = int.Parse(TextBox1.Text);

if (iInput > 100)

Label1.Text = "请输入正确的分数";

else if(iInput >= 90)

Label1.Text = "优秀";

else if (iInput >= 80)

Label1.Text = "良好";

else if (iInput >= 60)

Label1.Text = "及格";

else if (iInput >= 0)

Label1.Text = "不及格";

else

Label1.Text = "请输入正确的分数";

}

}

}

效果

(3)在网页上输出九九乘法表

.aspx.cs(.aspx源文件可以不作处理)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace homework_chap3

{

public partial class question3 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

for (int i=1; i<= 9; i++)

{

for (int j = 1; j <= i; j++)

{

Response.Write(i + "*" + j + "=" + (i * j) + "");

}

Response.Write("</br>");

}

}

}

}

效果

(4)在网页上输出如下形状:

A

BBB

CCCCC

DDD

E

.aspx.cs(.aspx源文件可以不作处理)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace homework_chap3.questions

{

public partial class question4 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

String[] s = { "A", "B", "C", "D", "E" };

for (int i = 1; i <= 3; i++)

{

for (int j = 1; j <= 3 - i; j++)

{

Response.Write("?");

}

for(int k = 1; k <= 2*i-1; k++)

{

Response.Write(s[i-1]);

}

Response.Write("</br>");

}

for (int i = 1; i < 3; i++)

{

for (int j = 1; j <= i; j++)

{

Response.Write("?");

}

for (int k = 1; k <= 5 - 2*i; k++)

{

Response.Write(s[i + 2]);

}

Response.Write("</br>");

}

}

}

}

效果

(6)设计一个网页,其中包含两个TextBox和一个Button控件。当在TextBox中各输入一个数值,再单击Button控件时在网页上输出两者相除的数值。(要求包含异常处理)

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="question6.aspx.cs" Inherits="homework_chap3.questions.question6" %>

<!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:Label ID="Label1" runat="server" Text="Label">输入一个除数:</asp:Label>?

<asp:TextBox ID="TextBox1" runat="server" Width="104px"></asp:TextBox>

<br />

<asp:Label ID="Label2" runat="server" Text="Label">输入一个被除数:</asp:Label>?

<asp:TextBox ID="TextBox2" runat="server" Width="104px"></asp:TextBox>

<br />

<asp:Button ID="Button1" runat="server" OnClick="btm_click" Text="计算" />?

<asp:Label ID="Label3" runat="server" Text="Label">答案</asp:Label>

</div>

</form>

</body>

</html>

.aspx.ce

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace homework_chap3.questions

{

public partial class question6 : System.Web.UI.Page

{

protected void btm_click(object sender, EventArgs e)

{

int[] str = new int[1];

int iInput1 = int.Parse(TextBox1.Text);

int iInput2 = int.Parse(TextBox2.Text);

if (iInput2 == 0)

throw new Exception("除数不能为0");

else

Label3.Text = (iInput1 / iInput2).ToString();

}

}

}

效果

(7)设计一个用于用户注册页面的用户信息类UserInfo,它包括两个属性:姓名(Name)、生日(Birthday);一个方法DecideAge:用于判断用户是否达到规定年龄,对大于等于18岁的在页面上输出“您是成人了!”,而小于18岁的在页面上输出“您还没长大呢!”

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="question7.aspx.cs" Inherits="homework_chap3.questions.question71" %>

<!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:Label ID="Label3" runat="server" Text="Label">?注册</asp:Label>

<br /><br />

<asp:Label ID="Label1" runat="server" Text="Label">姓名</asp:Label>?

<asp:TextBox ID="TextBox1" runat="server">如“朱晓栋”</asp:TextBox>

<br />

<asp:Label ID="Label2" runat="server" Text="Label">生日</asp:Label>?

<asp:TextBox ID="TextBox2" runat="server">如“19890411”</asp:TextBox>

<br />

<asp:Button ID="Button1" runat="server" OnClick="btm_click" Text="注册" />

</div>

</form>

</body>

</html>

.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace homework_chap3.questions

{

public partial class question71 : System.Web.UI.Page

{

protected void btm_click(object sender, EventArgs e)

{

int iInput2 = int.Parse (TextBox2.Text);

question7 que = new question7("zhu",19890411);

que.DecideAge(iInput2);

}

}

}

.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace homework_chap3.questions

{

public class question7

{

private string _Name;

private int _Brithday;

public string Name

{

get

{

return this._Name;

}

set

{

this._Name = value;

}

}

public int Brithday

{

get

{

return this._Brithday;

}

set

{

this._Brithday = value;

}

}

public question7(String name, int brithday)

{

this._Name = name;

this._Brithday = brithday;

}

public void DecideAge(int brithday)

{

if (20101001 - brithday < 180000)

throw new Exception("您还没长大呢!");

else

throw new Exception("您是成人了!");

}

}

}

效果

是这个么