WEB系統(tǒng)與技術課件_第1頁
WEB系統(tǒng)與技術課件_第2頁
WEB系統(tǒng)與技術課件_第3頁
WEB系統(tǒng)與技術課件_第4頁
WEB系統(tǒng)與技術課件_第5頁
已閱讀5頁,還剩226頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

WEB系統(tǒng)與技術(基于.net)主講人:張勇敢通信工程上課要求1:上要請準時到教室2:上課不準講話3:準時提交作業(yè)及實驗報告4:有問題到天空教室進行提問5:課后要多進行練習及自學相關材料最終成績總評:平時考勤+實驗成績+期末成績。課程設計成績總評:課程設計報告+答辯參考資料1:程序開發(fā)范例寶典2:內(nèi)容目錄1:HTML2:C#(自學)3:ASP.net控件(重點)4:ASP.net內(nèi)置對象(重點)5:數(shù)據(jù)庫知識6:數(shù)據(jù)庫編程(重點)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:復合內(nèi)容(基于選擇內(nèi)容)<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;}作業(yè)設計一張網(wǎng)站主頁(內(nèi)容自定)完成時間(2周)第三部分:控件部分1:常用WEB服務器控件2:特殊控件3:自定義控件4:驗證控件常用WEB服務器控件1:Label/TextBox2:Button,LinkButton,ImageButton3:DropDownList/ListBox4:CheckBox/CheckBoxList5:RadioButton/RadioButtonList6:Image/HyperLinkTextBox控件內(nèi)容獲?。篢extBox1.Text控件主要用于用戶輸入信息或者將信息從數(shù)據(jù)庫中顯示出來,常見登錄、注冊等。要掌握以下屬性: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”O(jiān)nClick="LinkButton1_Click">

查看詳細情況</asp:LinkButton>protectedvoidLinkButton1_Click(objectsender,EventArgse){

//通常轉(zhuǎn)到另外網(wǎng)頁

Response.Redirect(“”);}思考:LinkButton的鏈接效果如何設置?<asp:ImageButtonID="ImageButton1"runat="server"ImageUrl="~/image/001.jpg“OnClick="ImageButton1_Click"/>protectedvoidImageButton1_Click(objectsender,ImageClickEventArgse){;//一般轉(zhuǎn)到其它頁面}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>文本獲?。簊tringLabel1.Text=DropDownList1.SelectedValue;strsex=DropDownList1.SelectedItem.text思考:利用DropDownList實現(xiàn)省市聯(lián)動ListBox

<asp:ListBoxID="ListBox1"runat="server"BackColor="CornflowerBlue"Font-Bold="True"ForeColor="White"><asp:ListItemValue="0">C++</asp:ListItem><asp:ListItemValue=“1">WEB系統(tǒng)與技術</asp:ListItem><asp:ListItemValue="2">Java程序設計</asp:ListItem><asp:ListItemValue="3">操作系統(tǒng)</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系統(tǒng)與技術</asp:ListItem><asp:ListItemValue="2">Java程序設計</asp:ListItem><asp:ListItemValue="3">操作系統(tǒng)</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>作業(yè)2設計一個用戶注冊表單實例1:利用DropDownList實現(xiàn)省市聯(lián)動

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="處理發(fā)生錯誤!原因:"+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="供求網(wǎng)站導航"url="~/TreeViewPage.aspx><siteMapNodetitle="農(nóng)副食品加工業(yè)"><siteMapNodetitle="食品制造業(yè)"url="~/TestPage0.aspx"/><siteMapNodetitle="飲料制造業(yè)"url="~/TestPage1.aspx"/><siteMapNodetitle="煙草制造業(yè)"url="~/TestPage2.aspx"/></siteMapNode><siteMapNodetitle="金融業(yè)"><siteMapNodetitle="銀行業(yè)"url="~/TestPage3.aspx"/><siteMapNodetitle="證券業(yè)"url="~/TestPage4.aspx"/></siteMapNode></siteMapNode></siteMap>自定義控件具體實例見:EX6-11有以下好處:(1)對于一些常用的、比較復雜的功能,可以它們設計成為用戶控件,然后在多個頁面中重復使用;(2)如果要改變網(wǎng)頁內(nèi)容,只須修改用戶控件中的內(nèi)容,其他添加使用用戶控件的網(wǎng)頁會隨之改變,提高網(wǎng)頁開發(fā)效率作業(yè)3利用TreeView和SiteMapPath,結(jié)合框架設計網(wǎng)頁驗證控件控件描述RequireFieldValidator驗證是否為空CompareValidator對兩個控件的值進行比較RangeValidator對輸入的值進行控制,使其值在一定的范圍內(nèi)RegularExpressionValidator把用戶輸入的字符與自定義的表達式進行比較CustomValidator自定義驗證方式ValidationSummary在一個頁面中顯示總的驗證錯誤RequiredFieldValidator控件RequiredFieldValidator控件用于驗證目標控件輸入的數(shù)據(jù)是否等于InitialValue(初始值)如果不等于,IsValid為true.否則為false一般用來檢測文本框是否有數(shù)據(jù)輸入。重點屬性ControlValidator:表示驗證的對象ErrorMessage:錯誤信息Text:文本信息Display:顯示方式InitialValue(初始值)CompareValidator控件用于比較兩個控件的屬性值或?qū)⒁粋€控件的值與某個特定的數(shù)據(jù)比較。重點屬性ControlToValidate:作用的對象ControlToCompare:比較的對象Operator:比較類型Type:設置數(shù)據(jù)類型ValueToCompare與值比較一般需要結(jié)合RequiredFieldValidator控件一起用RangeValidator控件用于驗證目標控件的值是否在指定的MinimunValue與MaximumValue屬性范圍之間。重點屬性MinimumValue:最小值MaximumValue:最大值ControlToValidate:作用對象Type:設置數(shù)據(jù)類型一般需要結(jié)合RequiredFieldValidator控件一起用RegularExpressionValidator控件用來檢測用戶輸入的值是否和自定義的正則表達式一致。比如電子郵件、電話號碼等合法性。正則表達式是由普通字符以及特殊字符組成的文字模式。重點屬性ControlToValidate:設置作用對象ValidationExpression:設置正則表達式一般需要結(jié)合RequiredFieldValidator控件一起用。CustomValidator控件對用戶輸入的值進行自定義的檢驗方式。重點屬性ControlToValidate:設置作用對象ServerValidate事件奇偶數(shù)檢測長度檢測ValidationSummary控件不執(zhí)行任何驗證,只是將其他驗證控件的驗證錯誤(ErrorMessage值)集中顯示。主要屬性HeadTestDisplayModeWEB驗證控件小結(jié)所有Validation控件都包含相似的屬性。至少每個控件都應該指定兩個屬性。首先它必須包括ControlToValidata屬性,該屬性指定了要監(jiān)視的服務器控件的名稱。其次每個控件必須有ErrorMessage屬性,該屬性告訴ASP.NET:有效性驗證失敗時,應向用戶顯示什么消息。可使兩個或多個Validation控件監(jiān)視同一個服務器控件,只有所有條件都滿足時,輸入才算有效。作業(yè)4將第2次作業(yè)注冊頁面進行驗證處理。第四部分:內(nèi)置對象Page對象:用于操作整個頁面Response對象:用于向瀏覽器輸出信息。Request對象:用于獲取來自瀏覽器的信息。Server對象:提供服務器端的一些屬性和方法。Application對象:用于共享多個會話和請求之間的全局信息。Session對象:用于存儲特定用戶的會話信息。Cookie對象:用于設置或獲取Cookie信息。Page常用的屬性和方法:IsPostBack屬性:獲取一個布而值,該值表示頁是否正為響應客戶端回發(fā)而加載。如果該值為true,則表示當前頁是為響應客戶端回發(fā)而加載,否則表示當前頁是首次加載和訪問。IsValid屬性:獲取一個布而值,該值表示頁面是否通過驗證。如果頁驗證成功,則為true,否則為false。Load事件:當前服務器控件加載到Page對象中時發(fā)生。Response1:Redirect方法用于將客戶端重定向到資源的新位置。

Response.Redirect("NavigatePage.aspx?UserName="+Login1.UserName.ToString());Response.Redirect("NavigatePage.aspx”)2:Write/WriteFile直接輸出顯示W(wǎng)rite方法:向客戶端發(fā)送字符串信息。WriteFile方法:讀取一個文件,并且寫入客戶端輸出流

Response.Write("現(xiàn)在的時間為:"+DateTime.Now.ToString());Response.Write("<br>");Response.Write("專業(yè)名稱列表:");Response.Write("<ul>");Response.Write("<li>計算機網(wǎng)絡技術");Response.Write("<li>計算機信息管理");Response.Write("<li>軟件技術與開發(fā)");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("瀏覽器的相關規(guī)格與信息:");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數(shù)據(jù)庫或其他文件操作時就必須使用物理路徑。物理路徑可以直接寫出,但不利于網(wǎng)站的移植,而利用Server.MapPath方法可以將虛擬路徑轉(zhuǎn)換為物理路徑,方便了網(wǎng)站的移植,又滿足了程序的需要。

Response.Write(“當前目錄物理路徑:”+Server.MapPath(“./"));Response.Write("<br>");Response.Write("上級目錄物理路徑:"+Server.MapPath("../"));Response.Write("<br>");Response.Write("文件物理路徑:"+Server.MapPath("Default.aspx"));Application如果想在整個應用程序范圍內(nèi)存儲一些所有用戶共享的信息,Application是最佳的選擇,利用Application存儲的變量和對象在整個應用程序內(nèi)執(zhí)行的所有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”];如果直接使用一個根本不存在的應用程序變量時,會引發(fā)“未將對象引用設置到對象實例”的異常,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模塊引發(fā)的應用程序級別事件的代碼。Global.asax文件提供了5個事件,其中5個應用于Application對象。

事件名稱說明Application_Start在應用程序啟動時激發(fā)Application_Error在發(fā)生錯誤時激發(fā)Application_End在應用程序結(jié)束時激發(fā)應用程序狀態(tài)同步

有可能存在多個用戶同時存取同一個Application對象的情況。這樣就有可能出現(xiàn)多個用戶修改同一個Application命名對象,造成數(shù)據(jù)不一致的問題。

通過Lock和Unlock兩種方法,以解決對Application對象的訪問同步問題,一次只允許一個線程訪問應用程序狀態(tài)變量。關于鎖定與解鎖鎖定: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="";}發(fā)送按鈕事件

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對象的內(nèi)容是完全一樣的;而不同用戶會話訪問到的Session對象的內(nèi)容則各不相同。3:Session對象的生命周期是有限的,默認值為20分鐘,可以通過TimeOut屬性設置會話狀態(tài)的過期時間。如果用戶在該時間內(nèi)不刷新頁面或請求站點內(nèi)的其他文件,則該Session就會自動過期,而Session對象存儲的數(shù)據(jù)信息也將永遠丟失。(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”];如果直接使用一個根本不存在的應用程序變量時,會引發(fā)“未將對象引用設置到對象實例”的異常,If(Application[“var1”]!=null){intintvar=(int)Application[“var1”];}(3)刪除會話對象中的變量語法格式為:Session.Remove(“鍵名”);(4)清除會話對象中的所有變量語法格式為: Session.RemoveAll();(5)取消當前會話語法格式為: Session.Abandon();(6)設置會話對象的超時期限,以分鐘為單位。語法格式為: Session.TimeOut=數(shù)值;Web.config里進行Session配制<configuration><system.web><sessionStatemode=“Inproc”cookless=“false”timeout=“20”></sessionState></system.web></configuration>Mode有四種可能:(1)Off:禁用會話狀態(tài)(2)Inproc:指示使用進程內(nèi)會話狀態(tài)模式,在服務器本地存儲會話狀態(tài)數(shù)據(jù)(3)StateServer:指示使用狀態(tài)服務器模式,在運行狀態(tài)服務的機器上存儲會話狀態(tài)數(shù)據(jù)。(4)SQLServer:指示使用SQL模式,在SQLServer上存儲會話狀態(tài)數(shù)據(jù)。Session事件Global.asax文件中有2個事件應用于Session對象事件名稱說明Session_Start在會話啟動時激發(fā)Session_End在會話結(jié)束時激發(fā)用戶權限檢查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事件實例:統(tǒng)計在線人數(shù)

voidApplication_Start(objectsender,EventArgse){Application["counter"]=0;}voidSession_Start(objectsender,EventArgse){//在新會話啟動時運行的代碼

//對Application加鎖以防并行性

Application.Lock();//增加一個在線人數(shù)

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

Application.UnLock();}

voidSession_End(objectsender,EventArgse){//在會話結(jié)束時運行的代碼。

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

//InProc時,才會引發(fā)Session_End事件。如果會話模式設置為//StateServer或SQLServer,則不會引發(fā)該事件。

//對Application加鎖以防并行性

Application.Lock();//減少一個在線人數(shù)

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內(nèi)容例:讀取名字為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”];}作業(yè)5利用所學的知識,將注冊的信息在另一張網(wǎng)頁上顯示出來。第五部分:數(shù)據(jù)庫知識1:sqlserver20002:sql語句3:存儲過程4:觸發(fā)器(自學)sqlserver20001:用戶的創(chuàng)建及授權2:數(shù)據(jù)表的創(chuàng)建及相關操作3:數(shù)據(jù)庫的創(chuàng)建及相關操作4:數(shù)據(jù)庫的附加與分離sql語句1:數(shù)據(jù)查詢2:數(shù)據(jù)插入3:數(shù)據(jù)更新4:數(shù)據(jù)刪除5:數(shù)據(jù)備份與恢復數(shù)據(jù)查詢selectSELECT[ALL|DISTINCT|DISTINCTROW|TOP]{*|table.*|[table.]field1[ASalias1][,[table.]field2[ASalias2][,…]]}FROMtableexpression[,…][INexternaldatabase][WHERE…][GROUPBY…][HAVING…][ORDERBY…][WITHOWNERACCESSOPTION]

StudentInfo表:學生信息表列名數(shù)據(jù)類型長度是否允許為空StuIDchar10chineseint4√mathint4√englishint4√sumint4√Student_score:學生成績表實例4:查詢姓名為“張三”的學生記錄實例1:查詢所有學生的信息記錄Select*fromstudentInfo實例2:查詢所有學生的stuID,stuName并以學號,姓名為列名顯示SelectstuIDas學號,stuNameas姓名fromstudentInfo實例3:查詢stuID前10個學生記錄Selecttop10*fromstudentInfoorderbystuIDSelect*fromstudentInfowhereStuName=‘張三’5:統(tǒng)計學生的人數(shù)Selectcount(*)as人數(shù)fromstudentInfo6:模式查詢:查詢合某種匹配格式的所有記錄查詢所有姓“林”的學生Select*fromstudentInfowherestuNamelike‘林%’7:多表查詢(1)查詢所有學生的信息及成績(2)查詢姓名“張三”的學生信息及成績(3)查詢所有數(shù)學成績不及格的學生的信息及成績Insert/update/delete實例5:插入一條記錄,學生編號為”S0006”,姓名為張三,年齡20,地址莆田學院InsertintostuInfo(StuID,StuName,StuAge,StuAddress)values(“S006”,“張三”,20,“莆田學院“)實例6:將StuID號為“S0001”的姓名更改為”李四”UpdateStuInfosetStuName=“李四”whereStuID=“S001”實例七:刪除StuID為”S0001”的學生記錄DeletefromStuInfowherestuID=“K001”數(shù)據(jù)庫備份:backupdatabasetodisk='E:\dataBak\bak_t_full.bak'數(shù)據(jù)庫恢復:restoredatabase數(shù)據(jù)庫名fromdisk='E:\dataBak\bak_t_full.bak''存儲過程使用存儲過程的優(yōu)點如下:1:存儲過程在服務端運行,執(zhí)行速度快2:存儲過程執(zhí)行一次后就駐留在高速緩沖存儲器,在以后的操中,只需從高速度緩沖存儲器中調(diào)用已經(jīng)編譯好的二進制代碼,提高了系統(tǒng)性能。3:確保數(shù)據(jù)安全存儲過程CREATEprocgetAllEmployeeasselect*from員工信息表GOCREATEPROCEDUREgetEmployeeOrName (@員工姓名 [VarChar](50))ASselect*from員工信息表where員工姓名=@員工姓名GOCREATEPROCEDUREprocInsertEmployee (@員工編號 [VarChar](50), @員工姓名 [VarChar](50), @身份證號 [VarChar](50), @聯(lián)系電話 [VarChar](50))ASINSERTINTO員工信息表( [員工編號], [員工姓名], [身份證號], [聯(lián)系電話])VALUES( @員工編號, @員工姓名, @身份證號, @聯(lián)系電話)GOCREATEPROCEDURE[procUpdateEmployee] (@員工編號 [varchar](50), @員工姓名 [varchar](50), @身份證號 [varchar](50), @聯(lián)系電話 [varchar](50))ASUPDATE員工信息表SET [員工編號] =@員工編號, [員工姓名] =@員工姓名, [身份證號] =@身份證號, [聯(lián)系電話] =@聯(lián)系電話WHERE ([員工編號] =@員工編號)GOCREATEPROCEDURE[procDeleteEmployee] (@員工編號 [varchar](50))ASDELETE員工信息表WHERE([員工編號]=@員工編號)GO觸發(fā)器觸發(fā)器是一類特殊的存儲過程,觸發(fā)器與表的關系密切,用于保護表中的數(shù)據(jù)。當有操作影響觸發(fā)器保護的數(shù)據(jù)時,觸發(fā)器自動執(zhí)行。例如:通過觸發(fā)器實現(xiàn)多個表間數(shù)據(jù)的一致性。觸發(fā)器createTRIGGER管理員日志ONdbo.系統(tǒng)管理員表FORUPDATEASinsertinto系統(tǒng)日志表(備注)select'用戶名:'+inserted.用戶名稱+'—密碼:'+inserted.密碼frominsertedREATETRIGGER員工工資觸發(fā)器ON[dbo].[員工信息表]FORINSERTASinsertinto員工工資表(員工編號,員工姓名,基本工資)selectinserted.員工編號,inserted.員工姓名,1000frominserted事務處理在大部分數(shù)據(jù)庫應用和網(wǎng)站中,往往存在一組需要保證其原子性的數(shù)據(jù)庫操作,也就是將這一組操作看作成一個整體,如果其中一個操作不成功,那么前面所有操作的結(jié)果都沒有意義,而且會造成數(shù)據(jù)的錯誤,所以必須將數(shù)據(jù)恢復到這一組操作之前。為了確保數(shù)據(jù)庫最終結(jié)果的確定性和數(shù)據(jù)的一致性,引入了事務機制。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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論