ASP整合的材料.docx

上传人:b****8 文档编号:9091226 上传时间:2023-02-03 格式:DOCX 页数:44 大小:198.08KB
下载 相关 举报
ASP整合的材料.docx_第1页
第1页 / 共44页
ASP整合的材料.docx_第2页
第2页 / 共44页
ASP整合的材料.docx_第3页
第3页 / 共44页
ASP整合的材料.docx_第4页
第4页 / 共44页
ASP整合的材料.docx_第5页
第5页 / 共44页
点击查看更多>>
下载资源
资源描述

ASP整合的材料.docx

《ASP整合的材料.docx》由会员分享,可在线阅读,更多相关《ASP整合的材料.docx(44页珍藏版)》请在冰豆网上搜索。

ASP整合的材料.docx

ASP整合的材料

作业一

1.请新建网站创建课件中的两个页面。

2.请模仿代码中的Response.Write方法在以上两个页面最下方以任何形式在页面上显示自己的学号和姓名。

{随机数:

publicpartialclassDefault2:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

intre=0;

Randomro=newRandom(unchecked((int)DateTime.Now.Ticks));

re=ro.Next(10,20);

lb_RandNumber.Text=re.ToString();

Response.Write("

 090304039陈桃香
");

}

}

}

{

三角形:

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

for(inti=0;i<4;i++)

{

Response.Write("

ASP.NET2.0

");

}

Response.Write("

 090304039陈桃香
");

 

}

}

}

Web.Config

xmlversion="1.0"?

>

--

注意:

除了手动编辑此文件以外,您还可以使用

Web管理工具来配置应用程序的设置。

可以使用VisualStudio中的

“网站”->“Asp.Net配置”选项。

设置和注释的完整列表在

ments中,该文件通常位于

\Windows\Microsoft.Net\Framework\v2.x\Config中

-->

--

设置compilationdebug="true"将调试符号插入

已编译的页面中。

但由于这会

影响性能,因此只在开发过程中将此值

设置为true。

-->

--

通过节可以配置ASP.NET使用的

安全身份验证模式,

以标识传入的用户。

-->

--

如果在执行请求的过程中出现未处理的错误,

则通过节可以配置相应的处理步骤。

具体说来,

开发人员通过该节可以配置

要显示的html错误页

以代替错误堆栈跟踪。

-->

作业二

1.新建网站如例ch2_1所示,并修改代码使得在晚上时能显示晚上好。

NavigatePage.aspx

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclassNavigatePage:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

//获取用户登陆名

this.lb_userName.Text=Request["UserName"];

//将系统时间与数据13进行比较,来获取问候语

intTime=DateTime.Now.Hour;

stringstr;

if(Time>18)

{

str="晚上!

";

}

elseif(Time>14)

{

str="下午好!

";

}

elseif(Time>12)

{

str="中午好!

";

}

elseif(Time>6)

{

str="早上好!

";

}

else

{

str="凌晨好!

";

}

this.lb_pwd.Text=str;

}

}

NavigatePage

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="NavigatePage.aspx.cs"Inherits="NavigatePage"%>

DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:

//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

//www.w3.org/1999/xhtml">

无标题页

201px;height:

118px">

LabelID="lb_userName"runat="server">

Label>

LabelID="lb_pwd"runat="server">

Label>

欢迎登陆本网站

2.新建网站如例ch2_2所示,

(1)添加ASP.NET文件夹App_Code,在该文件夹下建立一个名为DBClass的类,将GetConnection方法移到该类中,使得所有页面类中的新建数据库连接对象全部调用DBClass类中的GetConnection方法完成;

(2)将原先session中传递的UID改变为传递用户名;(3)原例中注册页面中没有判定是否己存在相同用户名,请改为需要预先判断才能插入;(4)原例注册成功没有任何提示,请修改为注册成功则转到item.aspx页面。

Default.aspx.cs

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data.SqlClient;

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

}

protectedvoidSqlDataSource1_Selecting(objectsender,SqlDataSourceSelectingEventArgse)

{

}

protectedSqlConnectionGetConnection()

{//此方法用于创建数据库连接对象实例

stringconn=ConfigurationManager.AppSettings["ConnectionString"].ToString();

//从Web.config中读取数据库连接字符串存入字符串变量conn中

//ConfigurationManager类用于提供对Webconfig的访问

//AppSettings为取节中的信息

SqlConnectionmyConn=newSqlConnection(conn);

//实例化一个SqlConnection对象

returnmyConn;

//返回数据库连接对象

}

protectedvoidLogin1_Authenticate(objectsender,AuthenticateEventArgse)

{

SqlConnectionconn=GetConnection();//获取一个数据库连接对象

stringsqlStr;

sqlStr="select*fromTb_LoginwhereUseName='"+Login1.UserName+"'andUsePassword='"+Login1.Password+"'";

//形成查询用户名密码的sql语句

SqlDataAdapteradp=newSqlDataAdapter(sqlStr,conn);

//建立一个sqlDataAdapter(sql数据适配器)对象并实例化,参数为sql语句及数据库连接对象

DataSetds=newDataSet();

//建立一个DataSet(数据集)对象ds,用于存放从数据库中查询来的数据

adp.Fill(ds);

//利用适配器adp填充数据集ds

if(ds.Tables[0].Rows.Count>0)//如果ds中第一个表格中行数大于0,即有查询结果

{

Response.Redirect("NavigatePage.aspx?

UserName="+Login1.UserName.ToString());

//重定向到NavigatePage页面,并传递用户名

}

else

Response.Write("登录失败");//输出登录失败

adp.Dispose();//释放

ds.Dispose();

}

}

Web

xmlversion="1.0"?

>

--

注意:

除了手动编辑此文件以外,您还可以使用

Web管理工具来配置应用程序的设置。

可以使用VisualStudio中的

“网站”->“Asp.Net配置”选项。

设置和注释的完整列表在

ments中,该文件通常位于

\Windows\Microsoft.Net\Framework\v2.x\Config中

-->

--

设置compilationdebug="true"将调试符号插入

已编译的页面中。

但由于这会

影响性能,因此只在开发过程中将此值

设置为true。

-->

--

通过节可以配置ASP.NET使用的

安全身份验证模式,

以标识传入的用户。

-->

--

如果在执行请求的过程中出现未处理的错误,

则通过节可以配置相应的处理步骤。

具体说来,

开发人员通过该节可以配置

要显示的html错误页

以代替错误堆栈跟踪。

-->

作业三

1.请使用response和session两种方法实现以下网站(请分别用两种方法制作出两个网站)

default.aspx

compute.aspx

要求:

(1)default.aspx中,若输入的不是数字,则在页面上显示“请输入数字”。

(2)compute.aspx中,能判断是否三角形,是则输出面积,不是则提示“这三条边长不能构成三角形,请返回重新输入”

(3)请在两个页面的下方显示客户端IP,在线人数,IP登录次数,总访问量,日访问量。

(default.aspx中)

答案:

Default.aspx:

Default.aspx.cs:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

intlastVisitCounter;

if(Request.Cookies["ipVisitCounter"]==null)

{

lastVisitCounter=0;

}

else

{

lastVisitCounter=int.Parse(Request.Cookies["ipVisitCounter"].Value);

}

lastVisitCounter++;

HttpCookieaCookie=newHttpCookie("ipVisitCounter");

aCookie.Value=lastVisitCounter.ToString();

aCookie.Expires=DateTime.MaxValue;

Response.Cookies.Add(aCookie);

this.lb_IP.Text=Request.UserHostAddress;

this.lb_Onlinecount.Text=Application["online_count"].ToString();

this.lb_ipcount.Text=Request.Cookies["ipVisitCounter"].Value.ToString();

this.lb_DayVisit.Text=Application["dayvisit"].ToString();

this.lb_TotalVisit.Text=Application["totalvisit"].ToString();

}

}

protectedvoidbtn_Compute_Click(objectsender,EventArgse)

{

doublea,b,c;

if(IsNumeric(this.tb_Oper1.Text.Trim()))

a=Convert.ToDouble(this.tb_Oper1.Text.Trim());

else{Response.Write("请输入数字");return;}

if(IsNumeric(this.tb_Oper2.Text.Trim()))

b=Convert.ToDouble(this.tb_Oper2.Text.Trim());

else{Response.Write("请输入数字");return;}

if(IsNumeric(this.tb_Oper3.Text.Trim()))

c=Convert.ToDouble(this.tb_Oper3.Text.Trim());

else{Response.Write("请输入数字");return;}

Response.Redirect("compute.aspx?

a="+a+"&b="+b+"&c="+c);

}

publicboolIsNumeric(stringa)

{

boolb=true;

foreach(charcina)

{

b=char.IsNumber(c);

if(b!

=true)

returnfalse;

}

returntrue;

}

}

Compute.aspx.cs:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclasscompute:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

doublea=Convert.ToDouble(Request["a"]);

doubleb=Convert.ToDouble(Request["b"]);

doublec=Convert.ToDouble(Request["c"]);

if(a+b>c&&a+c>b&&b+c>a)

{

doublep=(a+b+c)/2;

doublearea=Math.Sqrt(p*(p-a)*(p-c)*(p-b));

Response.Write("该三角形的面积是"+area);

}

else

Response.Write("这不是一个三角形的三个边长");

}

protectedvoidlbtn_Back_Click(objectsender,EventArgse)

{

Response.Redirect("default.aspx");

}

}

Global:

<%@ApplicationLanguage="C#"%>

<%@ImportNamespace="System"%>

<%@ImportNamespace="System.Collections"%>

<%@ImportNamespace="System.ComponentModel"%>

<%@ImportNamespace="System.Web"%>

<%@ImportNamespace="System.Web.SessionState"%>

<%@ImportNamespace="System.IO"%>

<%@ImportNamespace="System.Collections"%>

publicstaticstringtotalvisits;

publicstaticstringdayvisits;

publicstaticstringday;

voidApplication_Start(objectsender,EventArgse)

{

//在应用程序启动时运行的代码

Application["online_count"]=0;//IIS启动时在线人数为0

Application["dayvisit"]=0;

Application["day"]=DateTime.Now.ToString();//读取当前时间

intcount=0;

StreamReadersrd;

//取得文件的实际路径

stringfile_path=Server.MapPath("visit.txt");

//打开文件进行读取

srd=File.OpenT

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1