ASP NET动态网站开发基础教程习题答案
Chap01参考答案
一、填空题
1、Web XML Web services
2、公共语言运行库、.NET Framework类库
3、属性方法事件
4、aspx
5、cs
二、选择题
1、ABCD
2、ABCD
3、AB
4、A
5、B
三、上机操作题
1、参考1.3.1。
2、参考1.4.2。
3、在ex1_1.aspx文件中的代码如下所示:
在ex1_1.aspx.cs中的代码如下所示:
public partial class _Default : System.Web.UI.Page
{
private static int i;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
i = 0;
}
protected void Button1_Click(object sender, EventArgs e)
{
i=i+1;
Label1.Text = "您单击了"+i.ToString()+"次按钮";
}
}
Chap02参考答案
一、填空题
1、引用类型
2、object
3、显式
4、多态性
5、初始化语句右侧
二、选择题
1、AC
2、A
3、B
4、B
5、AC
三、上机操作题
1、本习题的源代码如下:namespace ex2_1
{
struct student
{
public string name;
public string address;
public string tel;
};
class Program
{
static void Main(string[] args)
{
student[] stu = new student[5];
for (int i = 0; i < 5; i++)
{
stu[i].name = Console.ReadLine(); stu[i].address = Console.ReadLine(); stu[i].tel = Console.ReadLine();
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i.ToString()); Console.WriteLine(stu[i].name); Console.WriteLine(stu[i].address); Console.WriteLine(stu[i].tel);
}
Console.ReadLine();
}
}
}
2、本习题的源代码如下:namespace ex2_2
{
class Program
{
static void sort(int[] a, int n)
{
int temp;
for (int i = 1; i < n; i++)
for (int j = 0; j < n - i; j++)
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
static void Main(string[] args)
{
int n = 5;
int[] a = new int[5];
for (int i = 0; i < 5; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
sort(a, n);
for (int i = 0; i < 5; i++)
{
Console.WriteLine(a[i].ToString());
}
Console.ReadLine();
}
}
}
3、本习题的源代码如下:
namespace ex2_3
{
class student
{
protected string m_name;
protected string m_sex;
protected float m_tuition;
public student(string strname, string strsex, float ftuition)
{
m_name = strname;
m_sex = strsex;
m_tuition = ftuition;
}
virtual public void printfInfo()
{
Console.WriteLine(m_name + " " + m_sex + " " + m_tuition.ToString()); }
}
class graduate : student
{
protected string m_tutor;
public graduate(string strname, string strsex, float ftuition,string strtutor):base(strname,strsex,ftuition)
{
m_tutor = strtutor;
}
override public void printfInfo()
{
base.printfInfo();
Console.WriteLine("导师是"+m_tutor);
}
}
class Program
{
static void Main(string[] args)
{
student s = new student("张三", "男", 5000);
graduate g = new graduate("李四", "女", 9000, "王五");
s.printfInfo();
g.printfInfo();
Console.ReadLine();
}
}
}
Chap03参考答案
一、填空题
1、HttpResponse
2、客户端
3、服务器
4、状态变量
5、用户会话
二、选择题
1、C
2、ABCD
3、ABCD
4、AD
三、上机操作题
1、在ex_1_1.aspx文件中的代码如下所示:
在ex_1_1.aspx.cs文件中的代码如下所示:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("ex_1_2.aspx?name=张三");
}
}
在ex_1_2.aspx.cs文件中的代码如下所示:
public partial class ex_1_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["name"]);
}
}
2、在ex2_1.aspx文件中的代码如下所示:
在ex_2_1.aspx.cs文件中的代码如下所示:
public partial class __ex_2_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["name"] = "李四";
Response.Redirect("ex_2_2.aspx");
}
}
在ex_2_2.aspx文件中的代码如下所示:
在ex_2_2.aspx.cs文件中的代码如下所示:
public partial class _ex_2_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"].ToString();
}
}
3、在ex_3_1.aspx.cs文件中的代码如下所示:
public partial class _ex_3_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie1 = Request.Cookies["ttt"];
if (cookie1 == null)
{
//创建一个cookie实例
HttpCookie cookie = new HttpCookie("ttt");
//添加要存储的信息,采用键/值结合的方式
cookie.Values.Add("Name", "Tom");
cookie.Expires = DateTime.Now.AddYears(1);
//把cookie加如当前的页面的Response对象里面
Response.Cookies.Add(cookie);
Response.Redirect("https://www.360docs.net/doc/121226044.html,");
}
else
{
Response.Write(cookie1.Values["Name"]);
}
}
}
Chap04参考答案
一、填空题
1、System.Web.UI.WebControls
2.设置是否自动向服务器发送数据false
用户单击此控件时并不导致向服务器发送页面。
3.编程方式运行时更改页面中的文本
4.标头标题链接链接
5.图像作用点控件的集合
二、1.D 2.B 3.D 4.C 5.D
三、上机操作题
1、在ex4_1.aspx文件中代码如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex4_1.aspx.cs" Inherits="ex4_1" %>
ex4_1.aspx.cs的代码如下所示:
public partial class ex4_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DisplayType(object sender, EventArgs e)
{
Label1.Text = DropDownList1.Text;
}
}
2、在ex4_2.aspx文件中代码如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex4_2.aspx.cs" Inherits="ex4_2" %>
ex4_2.aspx.cs的代码如下所示:
public partial class ex4_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
e.Cell.BackColor = System.Drawing.Color.Yellow;
else if (!e.Day.IsOtherMonth && e.Day.IsWeekend)
{
e.Cell.BackColor = System.Drawing.Color.Red;
e.Day.IsSelectable = false;
}
}
}
3、在ex4_3.aspx文件中代码如下所示:
在ex4_3.aspx.cs文件中代码如下所示:
public partial class ex4_3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ItemsBulletedList_Click(object sender, BulletedListEventArgs e)
{
switch (e.Index)
{
case 0:
Label1.Text = "百度";
break;
case 1:
Label1.Text = "谷歌";
break;
case 2:
Label1.Text = "新浪";
break;
}
}
}
Chap05参考答案
一、填空题
1、客户端数据验证服务器端数据验证
2、RequiredFieldValidator
3、CompareValidator
4、RegularExpressionValidator
5、.ascx
二、1.D 2. D 3.ABCD 4、C 5、AB
三、上机操作题
1、ex5_1.aspx的源代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex5_1.aspx.cs" Inherits="ex5_1" %>
2、ex5_2.aspx的源代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex5_2.aspx.cs" Inherits="ex5_2" %>
3、身份验证用户控件的源代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="IDValidate.ascx.cs" Inherits="IDValidate" %>
ErrorMessage="您输入的身份证号码有误,请重新输入!" ValidationExpression="\d{17}[\d|X]|\d{15}">
ex5_3.aspx的源代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex5_3.aspx.cs" Inherits="ex5_3" %>
<%@ Register src="IDValidate.ascx" tagname="IDValidate" tagprefix="uc1" %>
Chap06 参考答案
一、1.Connection Command DataSetCommand DataSet DataReader
2.数据处理Connection Command DataReader DataAdapter
3.User ID(uid)Password(pwd)用户ID 口令
4.SqlDataSource ACCESS 密码ACCESS
5.数据命令数据库连接填充DataSet 更新数据源DataSet
二、1.B 2.AC 3.A 4.A 5.ABCD
三、上机操作题
1、ex6_1.aspx.cs的源代码如下:
protected void Page_Load(object sender, EventArgs e)
{
String sqlconn = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Database\\asp3.5Basic\\BookStore.mdb";
OleDbConnection myConnection = new OleDbConnection(sqlconn);
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand("select * from Book", myConnection); OleDbDataReader myReader;
myReader = myCommand.ExecuteReader();
Response.Write("
使用OleDbCommand类读取数据
");
Response.Write("
" + myReader.GetName(i) + " | ");
" + myReader[i].ToString() + " | ");
myReader.Close();
myConnection.Close();
}
2、ex6_2.aspx的源代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex6_2.aspx.cs" Inherits="ex6_2" %>
Ex6_2.aspx.cs的主要代码如下:
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionStr = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= E:\\Database\\asp3.5Basic\\BookStore.mdb ";
OleDbConnection myConn = new OleDbConnection(ConnectionStr);
myConn.Open();
OleDbCommand myCommand = new OleDbCommand("select * from Book", myConn); OleDbDataAdapter Adapter = new OleDbDataAdapter();
Adapter.SelectCommand = myCommand;
DataSet myDs = new DataSet();
Adapter.Fill(myDs);
GridView1.DataSource = myDs.Tables[0].DefaultView;
GridView1.DataBind();
myConn.Close();
}
3、ex6_3.aspx.cs的主要代码如下:
public partial class ex6_3 : System.Web.UI.Page
{
void CreateXmlDocument(string file)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(xmlDeclaration, doc.DocumentElement); XmlElement rootNode = doc.CreateElement("books");
doc.AppendChild(rootNode);
{
XmlElement parentNode = doc.CreateElement("book"); XmlElement title = doc.CreateElement("title");
XmlText strTitle = doc.CreateTextNode("https://www.360docs.net/doc/121226044.html,入门");
title.AppendChild(strTitle);
parentNode.AppendChild(title);
XmlElement author = doc.CreateElement("author");
XmlText txtAuth = doc.CreateTextNode("张三");
author.AppendChild(txtAuth);
parentNode.AppendChild(author);
doc.DocumentElement.PrependChild(parentNode);
}
{
XmlElement parentNode = doc.CreateElement("book"); XmlElement title = doc.CreateElement("title");
XmlText strTitle = doc.CreateTextNode("https://www.360docs.net/doc/121226044.html,精通");
title.AppendChild(strTitle);
parentNode.AppendChild(title);
XmlElement author = doc.CreateElement("author");
XmlText txtAuth = doc.CreateTextNode("李四");
author.AppendChild(txtAuth);
parentNode.AppendChild(author);
doc.DocumentElement.PrependChild(parentNode);
}
doc.Save(Server.MapPath(file));
}
void ReadXmlFile(string file)
{
//获得文件的物理路径
string xml = Server.MapPath(file);
XmlDocument doc = new XmlDocument();
doc.Load(xml);
XmlNodeList elemList1 = doc.GetElementsByTagName("books"); for (int i = 0; i < elemList1.Count; i++)
{
XmlNode book = elemList1[i];
for (int j = 0; j < book.ChildNodes.Count; j++)
{
XmlNode node = book.ChildNodes[j];
Response.Write("
名称:" + node.ChildNodes[0].InnerText); Response.Write("
作者:" + node.ChildNodes[1].InnerText);
Response.Write("
");}
}
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("
开始创建XML文档
");
CreateXmlDocument("books.xml");
Response.Write("成功创建Xml文档!");
Response.Write("
读取books.xml文件
");
ReadXmlFile("books.xml");
}
}
Chap07参考答案
一、填空题
1. 特定数据源https://www.360docs.net/doc/121226044.html,网页上的其他控件
2. Microsoft SQL Server OLE DB ODBC Oracle
3. Microsoft Access
4. 数据检索更新功能高级排序分页操作
5. 模板
二、选择题
1.ABC
2.ABAB
3.BC 4、A 5、C
三、上机操作题
1.ex7_1.aspx的源代码如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex7_1.aspx.cs" Inherits="ex7_1" %>
2.ex7_2.aspx的源代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex7_2.aspx.cs" Inherits="ex7_2" %>
除了网页中的代码外,我们还需要在DropDownList控件的SelectedIndexChanged事件中加入如下代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox1.DataBind();
}
3.ex7_3.aspx文件的源代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex7_3.aspx.cs" Inherits="ex7_3" %>
Chap08参考答案
一、填空题
1. 关键字运算符
2. from select group
3. SQL XQuery 查询表达式包含查询表达式
4. 数据库数据数据源类内存中集合
5. 内存XML
二、选择题
1.AB
2.C
3.D 4、BD 5、AC
三、上机操作题
1.ex8_1.aspx的源代码如下所示:
namespace ex8_1
{
class Program
{
static void Main(string[] args)
{
int[] scores = { 90, 71, 82, 93, 75, 82 };
IEnumerable
from score in scores
where score > 80
orderby score descending
select score;
Console.WriteLine("大于80的成绩有:");
foreach (int testScore in scoreQuery)
{
Console.WriteLine(testScore);
}
Console.ReadLine();
}
}
}
2.ex8_2.aspx.cs的源代码如下所示:
public partial class ex8_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String path = Server.MapPath(".\\App_Data\\BookStore.mdb");
String sqlconn = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path; OleDbConnection myConn = new OleDbConnection(sqlconn);
myConn.Open();
OleDbCommand myCommand = new OleDbCommand("select * from Book", myConn); OleDbDataAdapter Adapter = new OleDbDataAdapter();
Adapter.SelectCommand = myCommand;
DataSet myDs = new DataSet();
Adapter.Fill(myDs);
DataTable book = myDs.Tables[0];
var query =
from w in book.AsEnumerable()
select new
{
名称= w.Field
价格= w.Field
};
GridView1.DataSource = query;
GridView1.DataBind();
myConn.Close();
}
}
3.ex8_3.aspx的源代码如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ex8_3.aspx.cs" Inherits="ex8_3" %>