第4章 ASP-NET 3.5標(biāo)準(zhǔn)控件_第1頁
第4章 ASP-NET 3.5標(biāo)準(zhǔn)控件_第2頁
第4章 ASP-NET 3.5標(biāo)準(zhǔn)控件_第3頁
第4章 ASP-NET 3.5標(biāo)準(zhǔn)控件_第4頁
第4章 ASP-NET 3.5標(biāo)準(zhǔn)控件_第5頁
已閱讀5頁,還剩74頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

本章要求:了解HTML服務(wù)器控件熟悉ASP.NET3.5標(biāo)準(zhǔn)控件熟練掌握控件的應(yīng)用方式

第4章ASP.NET3.5標(biāo)準(zhǔn)控件1目錄4.1ASP.NET3.5頁面事件處理4.2ASP.NET3.5服務(wù)器控件概述4.3標(biāo)準(zhǔn)控件4.4小結(jié)24.1ASP.NET3.5頁面事件處理Web

窗體的事件模型Web窗體事件的觸發(fā)和響應(yīng)過程控件的三大要素:屬性、方法和事件34.1.1ASP.NET3.5事件

常用事件:

Page_Load事件:打開頁面或重載頁面時觸發(fā),

事件處理方法名與事件名相同。

(Page類中定義)

voidPage_Load(objectsender,EventArgse){}

sender

表示觸發(fā)事件的對象,e包含事件信息

4Click事件被觸發(fā)時一般會引起頁面重新加載。voidSubmitButton_Click(objectsender,EventArgse){}Change事件被觸發(fā)時,先將事件的信息暫時保存在客戶端的緩沖區(qū)中,等到下一次向服務(wù)器傳遞信息時,再和其他信息一起發(fā)送給服務(wù)器。若要讓控件的Change事件立即得到服務(wù)器的響應(yīng),就需要將該控件的屬性AutoPostBack值設(shè)為true。voidTextBox1_TextChanged(objectsender,EventArgse){…}54.1.2屬性IsPostBack

當(dāng)控件的事件被觸發(fā)時,Page_Load事件會在控件的事件之前被觸發(fā)。如果想在執(zhí)行控件的事件代碼時不執(zhí)行Page_Load事件中的代碼,可以通過判斷屬性Page.IsPostBack實現(xiàn)。屬性IsPostBack在用戶第一次瀏覽網(wǎng)頁時,會返回值false,否則返回值true。6實例4-1屬性IsPostBack應(yīng)用Page_Load事件和Click事件一起使用本實例在頁面第一次載入時顯示“頁面第一次加載!”。當(dāng)單擊按鈕時顯示“執(zhí)行Click事件代碼!”。源程序:IsPostBack.aspx(P73)程序說明:當(dāng)單擊按鈕時引起頁面往返,此時首先處理Page_Load事件中代碼,但因為“!IsPostBack”值為false,所以不執(zhí)行“Response.Write(“頁面第一次加載!”)”,然后處理Click事件中代碼,顯示“執(zhí)行Click事件代碼!”信息。7IsPostBack.aspx<body>

<formid="form1“runat="server">

<div>

<asp:ButtonID="Button1"

runat="server“OnClick="Button1_Click"

Style="font-size:x-large"Text="Button"/>

</div>

</form>

</body>8usingSystem;publicpartialclasschap4_IsPostBack:System.Web.UI.Page{

protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){Response.Write(“頁面第一次加載!”);

//先顯示}

}protectedvoidButton1_Click(objectsender,EventArgse){Response.Write("執(zhí)行Click事件代碼!");

}}IsPostBack.aspx.cs去掉if語句,觀察運行結(jié)果94.2ASP.NET3.5服務(wù)器控件概述控件是網(wǎng)頁設(shè)計的基礎(chǔ),在工具箱中,點擊工具箱中的控件,拖動到設(shè)計窗口。HTML服務(wù)器控件:常用于升級原有的ASP頁面到ASP.NET頁面(標(biāo)記變控件)。Web服務(wù)器控件:目前的ASP.NET3.5網(wǎng)站建設(shè)中,優(yōu)先考慮Web服務(wù)器控件。當(dāng)Web服務(wù)器控件無法完成特定的任務(wù)時,可考慮HTML服務(wù)器控件。104.2.1HTML服務(wù)器控件簡介實現(xiàn)了將XHTML元素到服務(wù)器控件的轉(zhuǎn)換。經(jīng)過轉(zhuǎn)換后,Web窗體頁就可訪問XHTML元素,從而實現(xiàn)在服務(wù)器端對HTML服務(wù)器控件的編程。添加屬性“runat="server"”將轉(zhuǎn)換XHTML元素到HTML服務(wù)器控件。

<inputid="Button2"type="button"value="button"/> <inputid="Button2"type="button"value="button"runat="server"/>工具箱內(nèi),HTML控件中的Table控件和div控件用于布局114.2.2Web服務(wù)器控件簡介實現(xiàn)Web系統(tǒng)的外觀、功能、操作方式等。可以從工具箱中拖到設(shè)計窗口和源碼窗口,控件名字的前面具有asp:標(biāo)記。根據(jù)功能不同分成標(biāo)準(zhǔn)控件、數(shù)據(jù)控件、驗證控件、導(dǎo)航控件、登錄控件、WebParts控件、AJAXExtensions控件和用戶自定義控件。124.3標(biāo)準(zhǔn)控件屬性名說明屬性名說明AccessKey控件的鍵盤快捷鍵Font控件的字體屬性Width控件的寬度Height控件的高度BackColor控件的背景色ID控件的編程標(biāo)識符BoderWidth控件的邊框?qū)挾萒abIndex控件的索引順序BoderStyle控件的邊框樣式Text控件上顯示的文本CssClass控件的CSS類名ToolTip當(dāng)鼠標(biāo)懸停在控件上時顯示的文本CssStyle控件的樣式Visible控件是否在Web頁上顯示Enabled是否啟用Web服務(wù)器控件公共屬性134.3.1Label控件用于在瀏覽器上顯示文本,可以在服務(wù)器端動態(tài)地修改顯示內(nèi)容。通過Text屬性指定控件顯示的內(nèi)容。定義的語法格式如下: <asp:LabelID=“Label1”runat=“server”Text=“Label顯示的內(nèi)容"></asp:Label>144.3.2TextBox控件用于輸入或顯示數(shù)據(jù)。語法格式如下:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

TextMode屬性

Text屬性值“SingleLine”表示單行文本框;值“Password”表示密碼框,將顯示特殊字符,如“*”;值“MultiLine”表示多行文本框。通過Text屬性置值和取值。AutoPostBack屬性值“true”表示當(dāng)文本框內(nèi)容改變且把焦點移出文本框時觸發(fā)TextChanged事件,引起頁面往返處理。AutoCompleteType屬性標(biāo)注能自動完成的類型,如Email表示能自動完成郵件地址列表。Focus()方法設(shè)置文本框焦點。TextChanged事件當(dāng)改變文本框中內(nèi)容且焦點離開文本框后觸發(fā)。15實例4-3控件TextBox綜合應(yīng)用當(dāng)頁面載入時,焦點自動定位在用戶名右邊的文本框中;當(dāng)輸入用戶名并把焦點移出文本框時,將觸發(fā)TextChanged事件,判斷用戶名是否可用,若可用則在lblValidate中顯示“√”,否則顯示“用戶名已占用!”;密碼右邊的文本框顯示為密碼框;E-mail右邊的文本框具有自動完成功能。源程序:TextBox.aspx

(P77)16程序說明當(dāng)頁面載入時,觸發(fā)Page_Load事件,將焦點定位在用戶名右邊的文本框中。本示例中用戶合法性判斷是與固定用戶名“jxssg”比較,實際使用需連接數(shù)據(jù)庫,與數(shù)據(jù)庫中保存的用戶名比較。要看到自動完成Email列表的效果,需先輸入E-mail并單擊確認(rèn)后再次輸入信息時才能看到效果。17TextBox.aspx<bodystyle="font-size:x-large"><formid="form1"runat="server"><div>

用戶名:<asp:TextBoxID="txtName"runat="server"

AutoPostBack="True"OnTextChanged="txtName_TextChanged"></asp:TextBox><asp:LabelID="lblValidate"runat="server"></asp:Label><br/>

碼:<asp:TextBoxID="txtPassword"runat="server"TextMode="Password"></asp:TextBox><br/>E-mail:<asp:TextBoxID="txtMail"runat="server"AutoCompleteType="Email"></asp:TextBox><br/><asp:ButtonID="btnSubmit"runat="server"Text="確認(rèn)"/></div></form></body>18TextBox.aspx.csusingSystem;publicpartialclasschap4_TextBox:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){txtName.Focus();}protectedvoidtxtName_TextChanged(objectsender,EventArgse){if(txtName.Text=="jxssg"){lblValidate.Text="用戶名已占用!";}else{lblValidate.Text="√";}}}194.3.3Button、LinkButton和ImageButton控件Button:傳統(tǒng)按鈕外觀LinkButton:超鏈接外觀ImageButton:圖形外觀,其圖像由ImageUrl屬性設(shè)置。 <asp:ButtonID="Button1"runat="server"Text="Button"/> <asp:LinkButtonID="LinkButton1"runat="server"

PostBackUrl="">

LinkButton</asp:LinkButton> <asp:ImageButtonID="ImageButton1"runat="server"ImageUrl="~/pic/map.JPG"/>20按鈕控件實用屬性和事件PostBackUrl屬性:單擊按鈕時鏈接到的URL。Click事件:當(dāng)單擊按鈕時被觸發(fā),執(zhí)行服務(wù)器端代碼。ClientClick事件:當(dāng)單擊按鈕時被觸發(fā),執(zhí)行客戶端代碼。21比較<a>與LinkButton

兩者都能呈現(xiàn)超鏈接形式設(shè)置具體的跳轉(zhuǎn)方法不同。在<a>元素中通過屬性href設(shè)置,如:

<ahref=“”>鏈接到21世紀(jì)</a>

而在LinkButton中需要設(shè)置PostBackUrl屬性或在Click事件處理代碼中,通過Response對象的重定向方法Redirect()實現(xiàn),如:

Response.Redirect("");22實例4-4利用Button控件執(zhí)行客戶端腳本要在單擊Button控件后執(zhí)行客戶端腳本,需要使用ClientClick事件和JavaScript。源程序:ClientClick.aspx(P80)程序說明:當(dāng)單擊刪除按鈕時,觸發(fā)ClientClick事件,執(zhí)行JavaScript代碼“returnconfirm('確定要刪除記錄嗎?')”,彈出確認(rèn)對話框。若單擊“確定”按鈕,觸發(fā)Click事件,執(zhí)行刪除操作(這里僅輸出信息,實際操作需連接數(shù)據(jù)庫);若單擊“取消”,將不再觸發(fā)Click事件,運行結(jié)束。

23<body><formid="form1"runat="server"><div><asp:ButtonID="Button1"runat="server"Text="刪除"OnClientClick="returnconfirm('確定要刪除記錄嗎?')"

OnClick="Button1_Click"/></div></form></body>usingSystem;//ClientClick.aspx.cspublicpartialclasschap4_ClientClick:System.Web.UI.Page{protectedvoidButton1_Click(objectsender,EventArgse){Response.Write("刪除成功!");}}實驗使用三個按鈕ClientClick.aspx向服務(wù)器發(fā)出事件處理請求244.3.4DropDownList控件允許用戶從預(yù)定義的下拉列表中選擇一項。 <asp:DropDownListID="DropDownList1"runat="server"></asp:DropDownList>25DropDownList控件實用屬性和事件表DataSource屬性使用的數(shù)據(jù)源。DataTextField屬性對應(yīng)數(shù)據(jù)源中的一個字段,該字段所有內(nèi)容將被顯示于下拉列表中。DataValueField屬性數(shù)據(jù)源中的一個字段,指定下拉列表中每個可選項的值。Items屬性列表中所有選項的集合,經(jīng)常使用Items.Add()方法添加項,Clear()方法刪除所有項。SelectedItem屬性當(dāng)前選定項。SelectedValue屬性當(dāng)前選定項的屬性Value值

。SelectedIndexChanged事件當(dāng)選擇下拉列表中一項后被觸發(fā)。DataBind()方法綁定數(shù)據(jù)源。26添加項到DropDownList中在屬性窗口中直接對屬性Items進行設(shè)置利用DropDownList對象的Items.Add()方法添加項,如: DropDownList1.Items.Add(newListItem("浙江","zhejiang"));通過屬性DataSource設(shè)置數(shù)據(jù)源,再通過DataBind()方法顯示數(shù)據(jù)。27實例4-5實現(xiàn)聯(lián)動的下拉列表聯(lián)動的下拉列表在實際工程項目中非常普遍,如要查詢某班級的課表,需要“學(xué)年—學(xué)期—學(xué)院—班級”這樣聯(lián)動的下拉列表。本實例以日期聯(lián)動為例,在默認(rèn)情況下,顯示系統(tǒng)日期,當(dāng)改變年或月時,相應(yīng)的每月天數(shù)會隨之而變。28實例4-5實現(xiàn)聯(lián)動的下拉列表(續(xù))源程序:DropDownList.aspx

(P83)程序說明:瀏覽時首先觸發(fā)Page_Load事件,綁定年、月、日到三個DropDownList控件。當(dāng)改變年或月份時,觸發(fā)相應(yīng)控件的SelectedIndexChanged事件形成頁面往返,將相應(yīng)月份的天數(shù)綁定到ddlDay。29<bodystyle="font-size:x-large"><formid="form1"runat="server"><div><asp:DropDownListID="ddlYear"runat="server"Style="font-size:large"AutoPostBack="True"

OnSelectedIndexChanged="ddlYear_SelectedIndexChanged"Width="77px"></asp:DropDownList>年<asp:DropDownListID="ddlMonth"runat="server"Style="font-size:large"Width="47px"AutoPostBack="True"OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged"></asp:DropDownList>月<asp:DropDownListID="ddlDay"runat="server"Style="font-size:large"Width="50px"></asp:DropDownList>日</div></form></body>

DropDownList.aspx.csp8330usingSystem;usingSystem.Web.UI.WebControls;publicpartialclasschap4_DropDownList:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){

if(!IsPostBack)

//頁面第一次載入,向各下拉列表填充值{BindYear();BindMonth();BindDay();}

}

DropDownList.aspx.cs31protectedvoidBindYear(){ddlYear.Items.Clear();

//清空年份下拉列表中項intstartYear=DateTime.Now.Year-10;intcurrentYear=DateTime.Now.Year;//向年份下拉列表添加項for(inti=startYear;i<=currentYear;i++){ddlYear.Items.Add(newListItem(i.ToString()));}//設(shè)置年份下拉列表默認(rèn)項ddlYear.SelectedValue=currentYear.ToString();

}protectedvoidBindMonth(){ddlMonth.Items.Clear();//向月份下拉列表添加項for(inti=1;i<=12;i++){ddlMonth.Items.Add(i.ToString());}

}32protectedvoidBindDay(){ddlDay.Items.Clear();//獲取年、月份下拉列表選中值stringyear=ddlYear.SelectedValue;stringmonth=ddlMonth.SelectedValue;//獲取相應(yīng)年、月對應(yīng)的天數(shù)intdays=DateTime.DaysInMonth(int.Parse(year),

int.Parse(month));//向日期下拉列表添加項for(inti=1;i<=days;i++){ddlDay.Items.Add(i.ToString());}

}protectedvoidddlYear_SelectedIndexChanged(objectsender,EventArgse){BindDay();}protectedvoidddlMonth_SelectedIndexChanged(objectsender,EventArgse){BindDay();}}//類結(jié)束實驗可視化設(shè)置與屬性設(shè)置334.3.5ListBox控件DropDownList和ListBox控件都允許用戶從列表中選擇項,區(qū)別在于DropDownList的列表在用戶選擇前處于隱藏狀態(tài),而ListBox的選項列表是可見的,并且可同時選擇多項。<asp:ListBoxID="ListBox1"runat="server"></asp:ListBox>SelectionMode屬性:值為Multiple表示允許選擇多項。34實例4-6實現(xiàn)數(shù)據(jù)項在ListBox控件之間的移動當(dāng)選擇左邊列表框中的項,再單擊按鈕后相應(yīng)的項將移動到右邊的列表框。源程序:ListBox.aspx

(P85)35

<asp:ListBoxID="lstLeft"runat="server"Rows="5"SelectionMode="Multiple"Style="font-size:large"Height="140px"Width="60px"><asp:ListItemValue="hunan">湖南</asp:ListItem><asp:ListItemValue="jiangxi">江西</asp:ListItem><asp:ListItemValue="beijing">北京</asp:ListItem><asp:ListItemValue="shanghai">上海</asp:ListItem><asp:ListItemValue="nanjing">南京</asp:ListItem></asp:ListBox><asp:ButtonID="btnMove"runat="server"OnClick="btnMove_Click"Style="font-size:large;position:relative;top:-55px;left:4px"Text=">"/><asp:ListBoxID="lstRight"runat="server"Rows="5"SelectionMode="Multiple"Height="140px"Width="60px"Style="position:relative;top:3px;left:12px;font-size:large;margin-right:0px"></asp:ListBox>36

usingSystem;publicpartialclasschap4_ListBox:System.Web.UI.Page{protectedvoidbtnMove_Click(objectsender,EventArgse){//遍歷左邊列表框中所有項

for(inti=0;i<lstLeft.Items.Count;i++)//Count發(fā)生變化{if(lstLeft.Items[i].Selected)

//判斷項是否選中{//向右邊列表框添加選中的一項

lstRight.Items.Add(lstLeft.Items[i]);lstLeft.Items.Remove(lstLeft.Items[i]);//調(diào)整左邊列表框中剩余項索引號

i--;}}}}374.3.6CheckBox和CheckBoxList控件多項選擇列表為用戶提供“真/假”、“是/否”或“開/關(guān)”多項選擇的方法??梢允褂枚鄠€CheckBox或單個CheckBoxList,但一般采用CheckBoxList。<asp:CheckBoxID="CheckBox1"runat="server"/> <asp:CheckBoxListID="CheckBoxList1"runat="server"></asp:CheckBoxList>

384.3.6CheckBox和CheckBoxList控件(續(xù))注意:判斷CheckBox是否選中的屬性是Checked,而CheckBoxList作為集合控件,判斷列表項是否選中的屬性是該項的Selected屬性。在實際工程項目中,一般設(shè)置CheckBoxList的屬性AutoPostBack值為false。要提交數(shù)據(jù)到服務(wù)器,不采用CheckBoxList的自身事件,而是常配合Button控件實現(xiàn)。39實例4-7CheckBoxList應(yīng)用當(dāng)選擇個人愛好并單擊提交按鈕后顯示選中項的提示信息。源程序:CheckBoxList.aspx(P87)<body><formid="form1"runat="server"><div><asp:CheckBoxListID="chklsSport"runat="server"Style="font-size:large"><asp:ListItemValue="football">足球</asp:ListItem><asp:ListItemValue="basketball">籃球</asp:ListItem><asp:ListItemValue="badminton">羽毛球</asp:ListItem><asp:ListItemValue="pingpong">乒乓球</asp:ListItem></asp:CheckBoxList></div><asp:ButtonID="btnSubmit"runat="server"OnClick="btnSubmit_Click"Style="font-size:large"Text="確認(rèn)"/><asp:LabelID="lblMsg"runat="server"Style="font-size:large"></asp:Label></form></body>40usingSystem;usingSystem.Web.UI.WebControls;publicpartialclasschap4_CheckBoxList:System.Web.UI.Page{protectedvoidbtnSubmit_Click(objectsender,EventArgse){lblMsg.Text="您選擇了:";//遍歷復(fù)選框中所有項

foreach(ListItemlistIteminchklsSport.Items){if(listItem.Selected){lblMsg.Text=lblMsg.Text+listItem.Text+" ";}}}}414.3.7RadioButton和RadioButtonList控件單項選擇列表用于在多種選擇中只能選擇一項的場合。單個的RadioButton只能提供單項選擇,可以將多個RadioButton形成一組,方法是設(shè)置每個RadioButton的屬性GroupName為同一名稱。<asp:RadioButtonID="RadioButton1"runat="server"GroupName="group"/> <asp:RadioButtonID="RadioButton2"runat="server"GroupName="group"/>42<asp:RadioButtonListID="RadioButtonList1"runat="server"><asp:ListItem>男</asp:ListItem><asp:ListItem>女</asp:ListItem></asp:RadioButtonList>注意:判斷RadioButton是否選中使用Checked屬性,而獲取RadioButtonList的選中項使用屬性SelectedItem。參照上述例題,設(shè)計應(yīng)用RadioButtonList控件的程序。434.3.8Image和ImageMap控件Image控件用于在Web窗體上顯示圖像,圖像源文件可以使用ImageUrl屬性在界面設(shè)計時確定,也可以在編程時指定。在工程實際項目中常與數(shù)據(jù)源綁定,根據(jù)數(shù)據(jù)源指定顯示圖像。<asp:ImageID="Image1"runat="server"ImageUrl="pic/map.JPG"/>注意:Image控件不包含Click事件,如果需要Click事件處理流程,可使用ImageButton控件代替Image控件。444.3.8Image和ImageMap控件(續(xù))ImageMap控件除可以用來顯示圖像外,還可以實現(xiàn)圖像的超鏈接??梢詫@示的圖像劃分為不同形狀的熱點區(qū)域,分別鏈接到不同的網(wǎng)頁。在工程實際項目中,常用于導(dǎo)航條、地圖等。熱點區(qū)域通過屬性HotSpot設(shè)置,劃分的區(qū)域有圓形CircleHotSpot、長方形RectangleHotSpot和任意多邊形PolygonHotSpot,每個區(qū)域通過屬性NavigateUrl確定要鏈接到的URL。45實例4-8利用ImageMap設(shè)計導(dǎo)航條整個導(dǎo)航條是一張圖片,當(dāng)設(shè)置好熱點區(qū)域后,點擊不同區(qū)域?qū)㈡溄拥讲煌W(wǎng)頁。源程序:ImageMap.aspx(P90)464.3.9HyperLink控件用于在網(wǎng)頁上創(chuàng)建鏈接<asp:HyperLinkID="HyperLink1"runat="server"Target="_blank">HyperLink</asp:HyperLink>屬性NavigateUrl確定要鏈接到的URL。屬性Target:值為框架名、_blank或_self??蚣苊麤Q定了在指定的框架中顯示鏈接頁,_blank決定了在一個新窗口中顯示鏈接頁,而_self決定了在原窗口中顯示鏈接頁。47ImageUrl屬性設(shè)置在圖片上建立鏈接,Text屬性設(shè)置在文本上建立鏈接。ImageUrl優(yōu)先,若找不到圖片則顯示屬性Text設(shè)置的內(nèi)容。在HyperLink中直接設(shè)置ImageUrl后顯示的圖形尺寸是不可調(diào)的,若要改變圖形尺寸,可配合使用Image控件。注意:HyperLink控件不包含Click事件,要使用Click事件可用LinkButton控件代替。48實例4-9組合使用HyperLink和Image控件本實例頁面中顯示圖片的尺寸與實際圖片的尺寸不相同。源程序:HyperLink.aspx(P91)<body><formid="form1"runat="server"><div><asp:HyperLinkID="HyperLink1"runat="server"

NavigateUrl=""><asp:ImageID="Image1"runat="server"

ImageUrl="pic/eg_mouse.jpg"Width="50"/>

</asp:HyperLink></div></form></body>494.3.10Table控件用于在Web窗體上動態(tài)地創(chuàng)建表格,是一種容器控件。Table對象由行(TableRow)對象組成,TableRow對象由單元格(TableCell)對象組成。<asp:TableID="Table1"runat="server"GridLines="Both"><asp:TableRow

runat="server"><asp:TableCell

runat="server">學(xué)號</asp:TableCell><asp:TableCell

runat="server">姓名</asp:TableCell><asp:TableCell

runat="server">成績</asp:TableCell></asp:TableRow></asp:Table>50注意:在“設(shè)計視圖”中,進入Table屬性窗口,使用屬性Rows添加行;使用Rows的屬性Cells添加單元格。在代碼中添加行是建立TableRow對象;添加單元格使用TableCell對象;添加控件使用Controls對象。實例4-10動態(tài)生成表格本實例頁面上的簡易成績錄入界面實質(zhì)是動態(tài)生成的表格。源程序:Table.aspx

(P93)51Table.aspx<asp:TableID="tblScore"runat="server"GridLines="Both"><asp:TableRowrunat="server"><asp:TableCellrunat="server">學(xué)號</asp:TableCell><asp:TableCellrunat="server">姓名</asp:TableCell><asp:TableCellrunat="server">成績</asp:TableCell></asp:TableRow></asp:Table>5253Table.aspx.csusingSystem;usingSystem.Web.UI.WebControls;publicpartialclasschap4_Table:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){//設(shè)置初使值,實際工程中來源于數(shù)據(jù)庫

string[]number={"20030201","20030202"};string[]name={"張三","李四"};

for(inti=1;i<=2;i++)

//動態(tài)生成表格{//建立一個行對象

TableRowrow=newTableRow();//建立第一個單元格對象

TableCellcellNumber=newTableCell();//建立第二個單元格對象

TableCellcellName=newTableCell();54

TableCellcellInput=newTableCell();//第三個單元格對象//設(shè)置第一個單元格的屬性TextcellNumber.Text=number[i-1];cellName.Text=name[i-1];//建立一個文本框?qū)ο?/p>

TextBoxtxtInput=newTextBox();//將文本框?qū)ο筇砑拥降谌齻€單元格中

cellInput.Controls.Add(txtInput);//添加各單元格對象到行對象

row.Cells.Add(cellNumber);row.Cells.Add(cellName);row.Cells.Add(cellInput);//添加行對象到表格對象

tblScore.Rows.Add(row);

}}

}554.3.11Panel控件

Web窗體上的容器控件,在同一個頁面上顯示不同的面板內(nèi)容。常用于分步顯示和輸入信息。每一個面板是獨立的。 <asp:PanelID="Panel1"runat="server"></asp:Panel>

56實例4-11利用Panel實現(xiàn)簡易注冊頁面源程序:Panel.aspx

(P95)程序說明:本程序用了三個Panel當(dāng)頁面載入時,首先執(zhí)行Page_Load事件代碼,將pnlStep1設(shè)置為可見,而將其它兩個Panel控件設(shè)置為不可見。判斷用戶名是否重復(fù)在實際工程項目中將與數(shù)據(jù)庫連接。信息也將保存到數(shù)據(jù)庫中。57Panel.aspx

<bodystyle="font-size:x-large"><formid="form1"runat="server"><div><asp:PanelID="pnlStep1"runat="server"Width="902px">第一步:輸入用戶名<br/>用戶名:<asp:TextBoxID="txtUser"runat="server">

</asp:TextBox><br/><asp:ButtonID="btnStep1"runat="server"

OnClick="btnStep1_Click“

Text="下一步"/></asp:Panel></div>

58<asp:PanelID="pnlStep2"runat="server">第二步:輸入用戶信息<br/>姓名:<asp:TextBoxID="txtName"runat="server"></asp:TextBox><br/>電話:<asp:TextBoxID="txtTelephone"runat="server"></asp:TextBox><br/><asp:ButtonID="btnStep2"runat="server"OnClick="btnStep2_Click"

Text="下一步"/></asp:Panel><asp:PanelID="pnlStep3"runat="server">第三步:請確認(rèn)您的輸入信息<br/><asp:LabelID="lblMsg"runat="server"></asp:Label><br/><asp:ButtonID="btnStep3"runat="server"Text="確定"

OnClick="btnStep3_Click"/></asp:Panel></form></body>

59publicpartialclasschap4_Panel:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){pnlStep1.Visible=true;pnlStep2.Visible=false;pnlStep3.Visible=false;}}protectedvoidbtnStep1_Click(objectsender,EventArgse){pnlStep1.Visible=false;pnlStep2.Visible=true;pnlStep3.Visible=false;

}protectedvoidbtnStep2_Click(objectsender,EventArgse){pnlStep1.Visible=false;pnlStep2.Visible=false;pnlStep3.Visible=true;//輸出用戶信息lblMsg.Text="用戶名:"+txtUser.Text+"<br/>姓名:"+

txtName.Text+"<br/>電話:"+txtTelephone.Text;}protectedvoidbtnStep3_Click(objectsender,EventArgse){//TODO:將用戶信息保存到數(shù)據(jù)庫}}604.3.12MultiView和View控件提供了一種多視圖切換顯示信息的方式,可以容易地實現(xiàn)分頁多步驟功能。在使用時,MultiView作為View的容器控件,View作為其它控件的容器控件。<asp:MultiViewID="MultiView1"runat="server"><asp:ViewID="View1"runat="server"></asp:View><asp:ViewID="View2"runat="server"></asp:View></asp:MultiView>ActiveViewIndex屬性:決定了當(dāng)前顯示哪個視圖,默認(rèn)值為-1,值0表示MultiView中包含的第一個View。6162View中Button控件類型屬性表CommandNameCommandArgument說明NextView不需要設(shè)置顯示下一個ViewPrevView不需要設(shè)置顯示上一個ViewSwitchViewByID要切換到的View控件ID切換到指定ID的ViewViewByIndex要切換到的View控件索引號切換到指定索引號的View63實例4-13利用MultiView和View實現(xiàn)用戶編程習(xí)慣調(diào)查源程序:MultiView.aspx

(P101)64源程序:MultiView.aspx

<asp:MultiViewID="mvSurvey"runat="server">

<asp:ViewID="View1"runat="server">

<spanclass="style1">1、您從事的是哪種應(yīng)用程序的編程?</span>

<asp:RadioButtonListID="rdoltView1"runat="server">

<asp:ListItemValue="webapp">Web應(yīng)用程序</asp:ListItem>

<asp:ListItemValue="winapp">Windows應(yīng)用程序</asp:ListItem>

</asp:RadioButtonList>

<spanclass="style1">

<asp:ButtonID="btnView1Next"runat="server"

CommandName="NextView"Text="下一個"/>

</span>

</asp:View>在行內(nèi)定義一個區(qū)域65

<asp:ViewID="View2"runat="server">

2、您最常用的語言是哪一種?<br/>

<asp:RadioButtonListID="rdoltView2"runat="server">

<asp:ListItemValue="cshap">C#語言</asp:ListItem>

<asp:ListItem>Java</asp:ListItem>

</asp:RadioButtonList>

<asp:ButtonID="btnView2Prew"runat="server"

CommandName="PrevView"Text="上一個"/>

<asp:ButtonID="btnView2Next"runat="server"

CommandName="NextView"Text="下一個"

OnClick="btnView2Next_Click"/>

</asp:View>

<asp:ViewID="View3"runat="server">

謝謝您的參與!<br/>

<asp:LabelID="lblDisplay"runat="server"></asp:Label><br/>

<asp:ButtonID="btnSave"runat="server"Text="保存"

OnClick="btnSave_Click"/>

</asp:View>

</asp:MultiView>66publicpartialclasschap4_MultiView:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){

mvSurvey.ActiveViewIndex=0;

//設(shè)置第一個活動視圖}}protectedvoidbtnView2Next_Click(objectsender,Eve

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論