WEB系统与技术课件_第1页
WEB系统与技术课件_第2页
WEB系统与技术课件_第3页
WEB系统与技术课件_第4页
WEB系统与技术课件_第5页
已阅读5页,还剩226页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

WEB系统与技术(基于.net)主讲人:张勇敢通信工程上课要求1:上要请准时到教室2:上课不准讲话3:准时提交作业及实验报告4:有问题到天空教室进行提问5:课后要多进行练习及自学相关材料最终成绩总评:平时考勤+实验成绩+期末成绩。课程设计成绩总评:课程设计报告+答辩参考资料1:程序开发范例宝典2:内容目录1:HTML2:C#(自学)3:ASP.net控件(重点)4:ASP.net内置对象(重点)5:数据库知识6:数据库编程(重点)7:安全设置第一部分:HTML1:表格的应用2:超链接3:框架使用4:移动字幕5:CSS应用6:图片/flash7:Javascript表格<tablewidth="600"border="1"cellpadding="0"cellspacing="0"><tr><tdheight="100"colspan="3"> </td></tr><tr><tdwidth="100"height="200"> </td><tdwidth="400"> </td><td> </td></tr><tr><tdheight="100"colspan="3"> </td></tr></table>超链接<ahref="">莆田学院</a><ahref="table.html">表格</a><ahref="#ptu">ptu</a>a:linka:visiteda:hovera:active框架<framesetrows="100,*,113"cols="*"frameborder="yes"border="1"framespacing="1">

<framesrc=“top.aspx"name=“topFrame"scrolling="NO"noresize><framesetrows="*"cols="153,*"framespacing="1"frameborder="yes"border="1"><framesrc="left.aspx"name="leftFrame"scrolling="NO"noresize><framesrc="right.aspx"name="mainFrame"></frameset><framesrc="down.aspx"name="bottomFrame"scrolling="NO"noresize></frameset>CSS样式表1:类(可用于任何HTML元素)2:ID(仅应用于一个HTML元素)3:标签(重新定义HTML元素)4:复合内容(基于选择内容)<styletype="text/css">.font01{ font-size:40px;}#header{ font-size:20px;}body{ font-size:13px;}</style><pid="header">这个是测试文字</p><p>这个测试文字</p><pclass="font01">这个是测试文字01</p><pclass="font01">这个是测试文字02</p>CSS方框模型——盒子模型整体划分<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="/1999/xhtml"><head><title></title><linkhref="StyleSheet.css"rel="stylesheet"type="text/css"/></head><body><divid="contenter"><divid="banner"></div><divid="content"><divid="left"></div><divid="middle"></div><divid="right"></div></div><divid="linker"></div><divid="footer"></div></div></body></html>#contenter{width:800px;margin-left:auto;margin-right:auto;}#banner{background-color:#B9EEFF;height:100px;}#content{}#linker{background-color:#FF00FF;height:100px;clear:both;}#footer{background-color:#0000FF;height:100px;}#content#left{background-color:#FFFF00;width:200px;height:400px;float:left;}#content#middle{background-color:#008000;width:400px;height:400px;float:left;}#content#right{background-color:#00FFFF;width:200px;height:400px;float:left;}作业设计一张网站主页(内容自定)完成时间(2周)第三部分:控件部分1:常用WEB服务器控件2:特殊控件3:自定义控件4:验证控件常用WEB服务器控件1:Label/TextBox2:Button,LinkButton,ImageButton3:DropDownList/ListBox4:CheckBox/CheckBoxList5:RadioButton/RadioButtonList6:Image/HyperLinkTextBox控件内容获取:TextBox1.Text控件主要用于用户输入信息或者将信息从数据库中显示出来,常见登录、注册等。要掌握以下属性:1:ID2:Text3:ReadOnly4:TextMode:SingleLine,MultiLine,Password

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox></td>

<asp:TextBoxID="TextBox2"runat="server"TextMode="Password"Width="149px“></asp:TextBox>Button/LinkButton/ImageButton

<asp:ButtonID="Button1"runat="server"Text="登录"OnClick="Button1_Click"/>

protectedvoidButton1_Click(objectsender,EventArgse){

Lbl_Message.Text="你的用户名为:"+Txt_name.Text.ToString().Trim()+"密码为:"+Txt_password.Text.ToString().Trim();}控件主要是提供一个按钮,通过点击按钮完成某个事件。<asp:LinkButtonID=“LinkButton1”runat=“server”OnClick="LinkButton1_Click">

查看详细情况</asp:LinkButton>protectedvoidLinkButton1_Click(objectsender,EventArgse){

//通常转到另外网页

Response.Redirect(“”);}思考:LinkButton的链接效果如何设置?<asp:ImageButtonID="ImageButton1"runat="server"ImageUrl="~/image/001.jpg“OnClick="ImageButton1_Click"/>protectedvoidImageButton1_Click(objectsender,ImageClickEventArgse){;//一般转到其它页面}DropDownList通常用于从多个选择项中选择其中一个重点掌握的属性:1:ID2:Items请选择年级:<asp:DropDownListID="DropDownList1"runat="server"Width="158px"><asp:ListItemSelected="True"Value="0">大一</asp:ListItem><asp:ListItemValue="1">大二</asp:ListItem><asp:ListItemValue="2">大三</asp:ListItem><asp:ListItemValue="3">大四</asp:ListItem></asp:DropDownList>文本获取:stringLabel1.Text=DropDownList1.SelectedValue;strsex=DropDownList1.SelectedItem.text思考:利用DropDownList实现省市联动ListBox

<asp:ListBoxID="ListBox1"runat="server"BackColor="CornflowerBlue"Font-Bold="True"ForeColor="White"><asp:ListItemValue="0">C++</asp:ListItem><asp:ListItemValue=“1">WEB系统与技术</asp:ListItem><asp:ListItemValue="2">Java程序设计</asp:ListItem><asp:ListItemValue="3">操作系统</asp:ListItem></asp:ListBox>stringstrclass=ListBox1.SelectedItem.Text;//单选

<asp:ListBoxID="ListBox1"runat="server"BackColor="CornflowerBlue"Font-Bold="True"ForeColor="White"SelectionMode="Multiple"><asp:ListItemValue="0">C++</asp:ListItem><asp:ListItemValue=“1">WEB系统与技术</asp:ListItem><asp:ListItemValue="2">Java程序设计</asp:ListItem><asp:ListItemValue="3">操作系统</asp:ListItem></asp:ListBox>

foreach(ListItemiteminListBox1.Items){if(item.Selected)strclass=strclass+item.Text;}CheckBox/CheckBoxList兴趣爱好:<br/><asp:CheckBoxID="CheckBox1"runat="server"Text="看电影"/><br/><asp:CheckBoxID="CheckBox2"runat="server"Text="听歌曲"/><br/><asp:CheckBoxID="CheckBox3"runat="server"Text="做运动"/><br/>

if(CheckBox1.Checked)stringhobby=CheckBox1.Text;

<asp:CheckBoxListID="CheckBoxList1"runat="server"RepeatDirection="Horizontal"><asp:ListItemValue="0">看电影</asp:ListItem><asp:ListItemValue="1">听歌曲</asp:ListItem><asp:ListItemValue="2">做运动</asp:ListItem></asp:CheckBoxList>

foreach(ListItemiteminCheckBoxList1.Items){if(item.Selected)strclass=strclass+item.Text;}RadioButton/RadioButtonList请选择所在年级:<br/><asp:RadioButtonID="RadioButton1"runat="server"GroupName="level"Text="大一"/><br/><asp:RadioButtonID="RadioButton2"runat="server"GroupName="level"Text="大二"/><br/><asp:RadioButtonID="RadioButton3"runat="server"GroupName="level"Text="大三"/><br/><asp:RadioButtonID="RadioButton4"runat="server"GroupName="level"Text="大四"/><br/>

if(RadioButton1.Checked)strclass=RadioButton1.Text;<asp:RadioButtonListID="RadioButtonList1"runat="server"Width="77px"><asp:ListItemSelected="True"Value="0">大一</asp:ListItem><asp:ListItemValue="1">大二</asp:ListItem><asp:ListItemValue="2">大三</asp:ListItem><asp:ListItemValue="3">大四</asp:ListItem></asp:RadioButtonList>

foreach(ListItemiteminRadioButtonList1.Items){if(item.Selected)strclass=item.Text;}Image/HyperLink

<asp:ImageID="Image1"runat="server"ImageUrl="~/image/001.jpg"/><asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl="">莆田学院</asp:HyperLink>作业2设计一个用户注册表单实例1:利用DropDownList实现省市联动

protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){DropDownList1.Items.Add(newListItem("福州","福州"));DropDownList1.Items.Add(newListItem("江西","江西"));DropDownList2.Items.Add(newListItem("莆田","莆田"));DropDownList2.Items.Add(newListItem("福州","福州"));}}protectedvoidDropDownList1_SelectedIndexChanged1(objectsender,EventArgse){inti=DropDownList1.SelectedIndex;DropDownList2.Items.Clear();switch(i){case0:DropDownList2.Items.Add(newListItem("莆田","莆田"));DropDownList2.Items.Add(newListItem("福州","福州"));break;case1:DropDownList2.Items.Add(newListItem("南昌","南昌"));DropDownList2.Items.Add(newListItem("宜春","宜春"));break;}}

protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){ListBox1.Items.Add(newListItem("张三"));ListBox1.Items.Add(newListItem("李四"));ListBox1.Items.Add(newListItem("王五"));ListBox1.Items.Add(newListItem("成名"));}}

protectedvoidLinkButton1_Click(objectsender,EventArgse){foreach(ListItemiteminListBox1.Items){if(item.Selected){ListBox2.Items.Add(item);}}foreach(ListItemiteminListBox2.Items){ListBox1.Items.Remove(item);

}

}

protectedvoidLinkButton2_Click(objectsender,EventArgse){foreach(ListItemiteminListBox1.Items){ListBox2.Items.Add(item);}ListBox1.Items.Clear();}特殊控件1:Calendar2:FileUpload3:TreeView4:SiteMapPathCalendar

<asp:CalendarID="Calendar1"runat="server"></asp:Calendar>具体见实例:EX6-08,以下是获取选择的时间方法

protectedvoidCalendar1_SelectionChanged(objectsender,EventArgse){DateTimedateValue;dateValue=Convert.ToDateTime(Calendar1.SelectedDate);//Label1.Text=dateValue.ToLongDateString();(2013年1月8日)

Label1.Text=dateValue.ToShortDateString();(2013-1-8)}FileUpLoad<asp:FileUploadID="FileUpload1"runat="server"Font-Size="9pt"Width="217px"/>protectedvoidButton1_Click(objectsender,EventArgse){try{if(FileUpload1.PostedFile.FileName==""){Label1.Text="要上传的文件不允许为空!";return;}else{stringfilepath=FileUpload1.PostedFile.FileName;stringfilename=filepath.Substring(filepath.LastIndexOf("\\")+1);stringserverpath=Server.MapPath("File/")+filename;FileUpload1.PostedFile.SaveAs(serverpath);Label1.Text="上传成功!";}}catch(Exceptionerror){Label1.Text="处理发生错误!原因:"+error.ToString();}}文件上传Ex12_09图片上传见:Ex12_11

protectedvoidButton1_Click(objectsender,EventArgse){if(FileUpload1.PostedFile.FileName==""){Label1.Text="您还没有选择图片!";return;}else{stringfilepath=FileUpload1.PostedFile.FileName;stringfilename=filepath.Substring(filepath.LastIndexOf("\\")+1);stringfileEx=filepath.Substring(filepath.LastIndexOf(".")+1);stringserverpath=Server.MapPath("File/")+filename;if(fileEx=="jpg"||fileEx=="bmp"||fileEx=="gif"){FileUpload1.PostedFile.SaveAs(serverpath);Image1.ImageUrl="File/"+filename;Label1.Text="上传成功!";}else{Label1.Text="上传的图片扩展名错误!";}}}TreeView具体见实例:Ex6-05SiteMapPath

<asp:SiteMapPathID="SiteMapPath1“runat="server"></asp:SiteMapPath>在实际使用中与Web.sitemap一起搭配使用EX06_04<?xmlversion="1.0"encoding="utf-8"?><siteMapxmlns="/AspNet/SiteMap-File-1.0"><siteMapNodetitle="供求网站导航"url="~/TreeViewPage.aspx><siteMapNodetitle="农副食品加工业"><siteMapNodetitle="食品制造业"url="~/TestPage0.aspx"/><siteMapNodetitle="饮料制造业"url="~/TestPage1.aspx"/><siteMapNodetitle="烟草制造业"url="~/TestPage2.aspx"/></siteMapNode><siteMapNodetitle="金融业"><siteMapNodetitle="银行业"url="~/TestPage3.aspx"/><siteMapNodetitle="证券业"url="~/TestPage4.aspx"/></siteMapNode></siteMapNode></siteMap>自定义控件具体实例见:EX6-11有以下好处:(1)对于一些常用的、比较复杂的功能,可以它们设计成为用户控件,然后在多个页面中重复使用;(2)如果要改变网页内容,只须修改用户控件中的内容,其他添加使用用户控件的网页会随之改变,提高网页开发效率作业3利用TreeView和SiteMapPath,结合框架设计网页验证控件控件描述RequireFieldValidator验证是否为空CompareValidator对两个控件的值进行比较RangeValidator对输入的值进行控制,使其值在一定的范围内RegularExpressionValidator把用户输入的字符与自定义的表达式进行比较CustomValidator自定义验证方式ValidationSummary在一个页面中显示总的验证错误RequiredFieldValidator控件RequiredFieldValidator控件用于验证目标控件输入的数据是否等于InitialValue(初始值)如果不等于,IsValid为true.否则为false一般用来检测文本框是否有数据输入。重点属性ControlValidator:表示验证的对象ErrorMessage:错误信息Text:文本信息Display:显示方式InitialValue(初始值)CompareValidator控件用于比较两个控件的属性值或将一个控件的值与某个特定的数据比较。重点属性ControlToValidate:作用的对象ControlToCompare:比较的对象Operator:比较类型Type:设置数据类型ValueToCompare与值比较一般需要结合RequiredFieldValidator控件一起用RangeValidator控件用于验证目标控件的值是否在指定的MinimunValue与MaximumValue属性范围之间。重点属性MinimumValue:最小值MaximumValue:最大值ControlToValidate:作用对象Type:设置数据类型一般需要结合RequiredFieldValidator控件一起用RegularExpressionValidator控件用来检测用户输入的值是否和自定义的正则表达式一致。比如电子邮件、电话号码等合法性。正则表达式是由普通字符以及特殊字符组成的文字模式。重点属性ControlToValidate:设置作用对象ValidationExpression:设置正则表达式一般需要结合RequiredFieldValidator控件一起用。CustomValidator控件对用户输入的值进行自定义的检验方式。重点属性ControlToValidate:设置作用对象ServerValidate事件奇偶数检测长度检测ValidationSummary控件不执行任何验证,只是将其他验证控件的验证错误(ErrorMessage值)集中显示。主要属性HeadTestDisplayModeWEB验证控件小结所有Validation控件都包含相似的属性。至少每个控件都应该指定两个属性。首先它必须包括ControlToValidata属性,该属性指定了要监视的服务器控件的名称。其次每个控件必须有ErrorMessage属性,该属性告诉ASP.NET:有效性验证失败时,应向用户显示什么消息。可使两个或多个Validation控件监视同一个服务器控件,只有所有条件都满足时,输入才算有效。作业4将第2次作业注册页面进行验证处理。第四部分:内置对象Page对象:用于操作整个页面Response对象:用于向浏览器输出信息。Request对象:用于获取来自浏览器的信息。Server对象:提供服务器端的一些属性和方法。Application对象:用于共享多个会话和请求之间的全局信息。Session对象:用于存储特定用户的会话信息。Cookie对象:用于设置或获取Cookie信息。Page常用的属性和方法:IsPostBack属性:获取一个布而值,该值表示页是否正为响应客户端回发而加载。如果该值为true,则表示当前页是为响应客户端回发而加载,否则表示当前页是首次加载和访问。IsValid属性:获取一个布而值,该值表示页面是否通过验证。如果页验证成功,则为true,否则为false。Load事件:当前服务器控件加载到Page对象中时发生。Response1:Redirect方法用于将客户端重定向到资源的新位置。

Response.Redirect("NavigatePage.aspx?UserName="+Login1.UserName.ToString());Response.Redirect("NavigatePage.aspx”)2:Write/WriteFile直接输出显示Write方法:向客户端发送字符串信息。WriteFile方法:读取一个文件,并且写入客户端输出流

Response.Write("现在的时间为:"+DateTime.Now.ToString());Response.Write("<br>");Response.Write("专业名称列表:");Response.Write("<ul>");Response.Write("<li>计算机网络技术");Response.Write("<li>计算机信息管理");Response.Write("<li>软件技术与开发");Response.Write("<ul>");

<tdstyle="width:166px;height:24px;"><%Response.Write("你好,很高兴见到你");%></td>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="/1999/xhtml"><head><title>无标题页</title></head><body><tablestyle="width:645px;height:257px"><tr><tdcolspan="3"style="height:18px">top部分</td></tr><tr><tdstyle="width:100px">left部分</td><tdstyle="width:100px">middle部分</td><tdstyle="width:100px">right部分</td></tr><tr><tdcolspan="3"rowspan="2"style="height:20px">bottom部分</td></tr><tr></tr></table></body></html>

protectedvoidPage_Load(objectsender,EventArgse){Response.WriteFile("1.htm");}Request1:获取页面间传递的信息Label1.Text=Request.QueryString[“UserName“]2:获取客户端浏览器的类型3:获取客户端和服务端的IP地址

HttpBrowserCapabilitiesbc=Request.Browser;Response.Write("浏览器的相关规格与信息:");Response.Write("<hr>");Response.Write("类型:"+bc.Type+"<br>");Response.Write("名称:"+bc.Browser+"<br>");Response.Write("版本:"+bc.Version+"<br>");Response.Write("操作平台:"+bc.Platform+"<br>");Response.Write("是否支持框架:"+bc.Frames+"<br>");Response.Write("是否支持表格:"+bc.Tables+"<br>");Response.Write("是否支持Cookies:"+bc.Cookies+"<br>");Response.Write("<hr>");Response.Write("用户IP地址:"+Request.UserHostAddress);Response.Write("服务器IP地址:"+Request.ServerVariables["LOCAL_ADDR"]);Server方法名称说明HtmlEncode对要在浏览器中显示的字符串进行HTML编码并返回已编码的字符串。HtmlDecode对HTML编码的字符串进行解码,并返回已解码的字符串。MapPath返回与Web服务器上的指定虚拟路径相对应的物理文件路径。

stringstr1,str2;str1="<h2>大家好!</h2>";str2=Server.HtmlEncode(str1);Response.Write(str1);Response.Write(str2);MapPath在页面中,一般使用的虚拟路径,但是在连接Access数据库或其他文件操作时就必须使用物理路径。物理路径可以直接写出,但不利于网站的移植,而利用Server.MapPath方法可以将虚拟路径转换为物理路径,方便了网站的移植,又满足了程序的需要。

Response.Write(“当前目录物理路径:”+Server.MapPath(“./"));Response.Write("<br>");Response.Write("上级目录物理路径:"+Server.MapPath("../"));Response.Write("<br>");Response.Write("文件物理路径:"+Server.MapPath("Default.aspx"));Application如果想在整个应用程序范围内存储一些所有用户共享的信息,Application是最佳的选择,利用Application存储的变量和对象在整个应用程序内执行的所有ASP.NET页面中都是可用的,并且值也是相同的。1:使用Application对象保存信息Application[“对象名”]=值;Application.Add(“对象名”,值)例如:stringstr1=“这是一个字符串”;intint1=34;Application.Add(“var1”,str1);Application[“var2”]=int1;2:获取Application对象信息

object对象名=Application[“对象名”];例:intintvar=(int)Application[“var1”];Stringstrvar=(string)Application[“var2”];如果直接使用一个根本不存在的应用程序变量时,会引发“未将对象引用设置到对象实例”的异常,If(Application[“var1”]!=null){intintvar=(int)Application[“var1”];}3:删除一个对象

Application.Remove(“对象名”);4:删除所有对象Application.RemoveAll();Application.Clear();Application事件在ASP.NET应用程序中可以包含一个特殊的可选文件——Global.asax文件,也称作ASP.NET应用程序文件,它包含用于响应ASP.NET或HTTP模块引发的应用程序级别事件的代码。Global.asax文件提供了5个事件,其中5个应用于Application对象。

事件名称说明Application_Start在应用程序启动时激发Application_Error在发生错误时激发Application_End在应用程序结束时激发应用程序状态同步

有可能存在多个用户同时存取同一个Application对象的情况。这样就有可能出现多个用户修改同一个Application命名对象,造成数据不一致的问题。

通过Lock和Unlock两种方法,以解决对Application对象的访问同步问题,一次只允许一个线程访问应用程序状态变量。关于锁定与解锁锁定:Application.Lock()

访问:Application[“对象名”]=值

解锁:Application.Unlock()

注意:Lock方法和UnLock方法应该成对使用。具体实例:聊天室send.aspxmain.aspx<framesetrows="*,80"><framesrc="main.aspx"/><framesrc="send.aspx"/></frameset>chat.html

voidApplication_Start(objectsender,EventArgse){Application["chatcontent"]="<h2>欢迎来到幸福聊天室。。。。</h2>";}Global.asax

protectedvoidButton1_Click(objectsender,EventArgse){stringmessage;message="<fontcolor='blue'>"+this.sender.Text+"</font>说";message+=this.content.Text;message+="(<i>"+DateTime.Now.ToString()+"</i>)";message+="<br>";Application.Lock();Application["chatcontent"]=(string)Application["chatcontent"]+message;Application.UnLock();this.content.Text="";}发送按钮事件

protectedvoidPage_Load(objectsender,EventArgse){this.chatmessage.Text=(string)Application["chatcontent"];}Main.aspx里的Page_Load事件<title>无标题页</title><metahttp-equiv="refresh"content="4;"/>在源码部分加上下面部分功能,完成刷新功能Session1:Session对象用于存储从一个用户开始访问某个特定的aspx的页面起,到用户离开为止,特定的用户会话所需要的信息。用户在应用程序的页面切换时,Session对象的变量不会被清除。2:对于一个Web应用程序而言,所有用户访问到的Application对象的内容是完全一样的;而不同用户会话访问到的Session对象的内容则各不相同。3:Session对象的生命周期是有限的,默认值为20分钟,可以通过TimeOut属性设置会话状态的过期时间。如果用户在该时间内不刷新页面或请求站点内的其他文件,则该Session就会自动过期,而Session对象存储的数据信息也将永远丢失。(1)使用Session对象保存信息语法格式为:Session[“变量名“]=值;实例:stringstr1=“这是一个字符串”;intint1=34;Session[“var1”]=str1;Session[“var2]=int1;Session.Add(“var1”,str1);Session.Add(“var2”,str2);(2)获取会话变量中的值语法格式为:object对象=Session["变量名“]object对象名=Session[“对象名”];例:intintvar=(int)Session[“var1”];Stringstrvar=(string)Session[“var2”];如果直接使用一个根本不存在的应用程序变量时,会引发“未将对象引用设置到对象实例”的异常,If(Application[“var1”]!=null){intintvar=(int)Application[“var1”];}(3)删除会话对象中的变量语法格式为:Session.Remove(“键名”);(4)清除会话对象中的所有变量语法格式为: Session.RemoveAll();(5)取消当前会话语法格式为: Session.Abandon();(6)设置会话对象的超时期限,以分钟为单位。语法格式为: Session.TimeOut=数值;Web.config里进行Session配制<configuration><system.web><sessionStatemode=“Inproc”cookless=“false”timeout=“20”></sessionState></system.web></configuration>Mode有四种可能:(1)Off:禁用会话状态(2)Inproc:指示使用进程内会话状态模式,在服务器本地存储会话状态数据(3)StateServer:指示使用状态服务器模式,在运行状态服务的机器上存储会话状态数据。(4)SQLServer:指示使用SQL模式,在SQLServer上存储会话状态数据。Session事件Global.asax文件中有2个事件应用于Session对象事件名称说明Session_Start在会话启动时激发Session_End在会话结束时激发用户权限检查index.aspxLogin.aspxadmin.aspx

protectedvoidButton1_Click(objectsender,EventArgse){stringstrno="<script>alert('用户名或密码不正确');<"+"/script>";if(username.Text=="admin"&&userpassword.Text=="123456"){//登录成功

Session["username"]=username.Text;Session["userpass"]=userpassword.Text;Response.Redirect("admin.aspx");}else{Page.RegisterClientScriptBlock("loginno",strno);}}Login.aspx中的登录事件

protectedvoidPage_Load(objectsender,EventArgse){if(Session["username"]!=null&&Session["userpass"]!=null){stringusername=Session["username"].ToString();stringuserpassword=Session["userpass"].ToString();if(username!="admin"||userpassword!="123456"){Response.Redirect("login.aspx",true);}}else{Response.Redirect("login.aspx",true);}}Admin.aspx的Page_Load事件实例:统计在线人数

voidApplication_Start(objectsender,EventArgse){Application["counter"]=0;}voidSession_Start(objectsender,EventArgse){//在新会话启动时运行的代码

//对Application加锁以防并行性

Application.Lock();//增加一个在线人数

Application["counter"]=(int)Application["counter"]+1;//解锁

Application.UnLock();}

voidSession_End(objectsender,EventArgse){//在会话结束时运行的代码。

//注意:只有在Web.config文件中的sessionstate模式设置为

//InProc时,才会引发Session_End事件。如果会话模式设置为//StateServer或SQLServer,则不会引发该事件。

//对Application加锁以防并行性

Application.Lock();//减少一个在线人数

Application["counter"]=(int)Application["counter"]-1;//解锁

Application.UnLock();}历史访问量思考CookieCookie就是Web服务器保存在用户硬盘上的一段文本信息,伴随着用户清求和页面在WEB服务器和浏览器之间传递,用户访问某个站点时,WEB程序都可以读取Cookie所包含的信息Cookie对象常用的属性有:(1)Expires:获取或设置Cookie的过期日期和时间。(2)Value:获取或设置单个Cookie值。Stringstrname=“张三”;Response.Cookies[“username”].Value=strnameResponse.Cookies[“username”].Expires=DateTime.Now.AddDays(3);也可以:

//定义新的Cookie对象

HttpCookienewCookie=newHttpCookie(“username");newCookie.Expires=DateTime.MaxValue;newCookie.Value=strname;Response.AppendCookie(newCookie);一个Cookie对象也可以存储多个值,称为多值Cookie如:Response.Cookies[“user”][“username”]=“张三”;Response.Cookies[“user”[“userage”]=23.toString();Response.Cookies[“user”].Expires=DateTime.Now.AddDays(3);也可以:HttpCookienewCookie=newHttpCookie("user");newCookie.Expires=DateTime.Now.AddDays(3);newCookie.Values[“username”]=“张三”;newCookie.Values[“userage”]=23.toString();Response.AppendCookie(newCookie);2:读取Cookie内容例:读取名字为username的Cookie,并将值显示在Label1控件中If(Request.Cookies[“username”]!=null){Label1.Text=Request.Cookies[“username”].Value;}也可以用另外一种方法If(Request.Cookies[“username”]!=null){HttpCookiemyCookie=Request.Cookies[“username”];Label1.Text=myCookie.Value;}如果读取多值Cookie,需要使用子键来获得值。If(Request.Cookies[“user”]!=null){Label1.Text=Request.Cookies[“user”][“username”];Label2.Text=Request.Cookies[“user”][“userage”];}作业5利用所学的知识,将注册的信息在另一张网页上显示出来。第五部分:数据库知识1:sqlserver20002:sql语句3:存储过程4:触发器(自学)sqlserver20001:用户的创建及授权2:数据表的创建及相关操作3:数据库的创建及相关操作4:数据库的附加与分离sql语句1:数据查询2:数据插入3:数据更新4:数据删除5:数据备份与恢复数据查询selectSELECT[ALL|DISTINCT|DISTINCTROW|TOP]{*|table.*|[table.]field1[ASalias1][,[table.]field2[ASalias2][,…]]}FROMtableexpression[,…][INexternaldatabase][WHERE…][GROUPBY…][HAVING…][ORDERBY…][WITHOWNERACCESSOPTION]

StudentInfo表:学生信息表列名数据类型长度是否允许为空StuIDchar10chineseint4√mathint4√englishint4√sumint4√Student_score:学生成绩表实例4:查询姓名为“张三”的学生记录实例1:查询所有学生的信息记录Select*fromstudentInfo实例2:查询所有学生的stuID,stuName并以学号,姓名为列名显示SelectstuIDas学号,stuNameas姓名fromstudentInfo实例3:查询stuID前10个学生记录Selecttop10*fromstudentInfoorderbystuIDSelect*fromstudentInfowhereStuName=‘张三’5:统计学生的人数Selectcount(*)as人数fromstudentInfo6:模式查询:查询合某种匹配格式的所有记录查询所有姓“林”的学生Select*fromstudentInfowherestuNamelike‘林%’7:多表查询(1)查询所有学生的信息及成绩(2)查询姓名“张三”的学生信息及成绩(3)查询所有数学成绩不及格的学生的信息及成绩Insert/update/delete实例5:插入一条记录,学生编号为”S0006”,姓名为张三,年龄20,地址莆田学院InsertintostuInfo(StuID,StuName,StuAge,StuAddress)values(“S006”,“张三”,20,“莆田学院“)实例6:将StuID号为“S0001”的姓名更改为”李四”UpdateStuInfosetStuName=“李四”whereStuID=“S001”实例七:删除StuID为”S0001”的学生记录DeletefromStuInfowherestuID=“K001”数据库备份:backupdatabasetodisk='E:\dataBak\bak_t_full.bak'数据库恢复:restoredatabase数据库名fromdisk='E:\dataBak\bak_t_full.bak''存储过程使用存储过程的优点如下:1:存储过程在服务端运行,执行速度快2:存储过程执行一次后就驻留在高速缓冲存储器,在以后的操中,只需从高速度缓冲存储器中调用已经编译好的二进制代码,提高了系统性能。3:确保数据安全存储过程CREATEprocgetAllEmployeeasselect*from员工信息表GOCREATEPROCEDUREgetEmployeeOrName (@员工姓名 [VarChar](50))ASselect*from员工信息表where员工姓名=@员工姓名GOCREATEPROCEDUREprocInsertEmployee (@员工编号 [VarChar](50), @员工姓名 [VarChar](50), @身份证号 [VarChar](50), @联系电话 [VarChar](50))ASINSERTINTO员工信息表( [员工编号], [员工姓名], [身份证号], [联系电话])VALUES( @员工编号, @员工姓名, @身份证号, @联系电话)GOCREATEPROCEDURE[procUpdateEmployee] (@员工编号 [varchar](50), @员工姓名 [varchar](50), @身份证号 [varchar](50), @联系电话 [varchar](50))ASUPDATE员工信息表SET [员工编号] =@员工编号, [员工姓名] =@员工姓名, [身份证号] =@身份证号, [联系电话] =@联系电话WHERE ([员工编号] =@员工编号)GOCREATEPROCEDURE[procDeleteEmployee] (@员工编号 [varchar](50))ASDELETE员工信息表WHERE([员工编号]=@员工编号)GO触发器触发器是一类特殊的存储过程,触发器与表的关系密切,用于保护表中的数据。当有操作影响触发器保护的数据时,触发器自动执行。例如:通过触发器实现多个表间数据的一致性。触发器createTRIGGER管理员日志ONdbo.系统管理员表FORUPDATEASinsertinto系统日志表(备注)select'用户名:'+inserted.用户名称+'—密码:'+inserted.密码frominsertedREATETRIGGER员工工资触发器ON[dbo].[员工信息表]FORINSERTASinsertinto员工工资表(员工编号,员工姓名,基本工资)selectinserted.员工编号,inserted.员工姓名,1000frominserted事务处理在大部分数据库应用和网站中,往往存在一组需要保证其原子性的数据库操作,也就是将这一组操作看作成一个整体,如果其中一个操作不成功,那么前面所有操作的结果都没有意义,而且会造成数据的错误,所以必须将数据恢复到这一组操作之前。为了确保数据库最终结果的确定性和数据的一致性,引入了事务机制。SqlConnectioncon=newSqlConnection("server=(local);DataBase=db_07;UserID=sa;PWD=");con.Open();

SqlTransactionst=con.BeginTr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论