Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP.NET 3.5 Ch5 進階伺服器控制項.

Similar presentations


Presentation on theme: "ASP.NET 3.5 Ch5 進階伺服器控制項."— Presentation transcript:

1 ASP.NET 3.5 Ch5 進階伺服器控制項

2 CheckBox-多選選項 CheckBox1.Checked屬性:True(勾選) CheckBox1.Text屬性

3 CheckBox範例-ch05-01 Dim Orders As String = ""
'如果 CheckBox1 被勾選, 則使用 CheckBox1.Text 與 'TextBox1.Text 屬性讀取第 1 本書的書名與數量 If CheckBox1.Checked = True Then Orders = CheckBox1.Text & ", 共" & TextBox1.Text & "本<br />" End If '如果 CheckBox2 被勾選, 則讀取第 2 本書的書名與數量 If CheckBox2.Checked = True Then Orders &= CheckBox2.Text & ", 共" & TextBox2.Text & "本<br />" '如果 CheckBox3 被勾選, 則讀取第 3 本書的書名與數量 If CheckBox3.Checked = True Then Orders &= CheckBox3.Text & ", 共" & TextBox3.Text & "本<br />" '若 Orders 變數不是空字串, 表示使用者訂購了書籍 If Orders <> "" Then '使用 Label 控制項顯示使用者訂購的書籍 Label1.Text = "您已經訂購了以下書籍:<br />" & Orders

4 RadioButton控制項-單選項 RadioButton1.checked屬性:判斷是否點選
RadioButton1.Text屬性:顯示文字 RadioButton.Group屬性:分組依據

5 RadioButton範例-ch05-02 Dim Score As Integer = 0, Number As Integer = 0
'如果選擇了 RadioButton2, 表示答對問題 A If RadioButton2.Checked Then Score += 2 '問題 A 得 2 分 Number += 1 End If '如果選擇了 RadioButton8, 表示答對問題 B If RadioButton8.Checked Then Score += 3 '問題 B 得 3 分 '使用 Label1 控制項顯示答對題數與得分 Label1.Text = "您答對了 " & Number & _ " 題, 得分為 " & Score

6 CheckBoxList-多選項目清單 CheckBoxList1.items.Count屬性:項目數量
CheckBoxList1.item(index).Selected: 項目是否被勾選 CheckBoxList1.item(index).Text:項目的文字 CheckBoxList1.item(index).Value:項目的值

7 CheckBoxList範例-ch05-03 Dim Orders As String = ""
'使用迴圈逐一讀取各項目的 Selected 屬性, '若為 True, 表示使用者勾選了該項目 For Index As Integer = 0 To CheckBoxList1.Items.Count - 1 If CheckBoxList1.Items(Index).Selected Then '使用各項目的 Text 屬性讀取書名 Orders += CheckBoxList1.Items(Index).Text & "<br />" End If Next '若 Orders 變數不是空字串, 表示使用者訂購了書籍 If Orders <> "" Then Label1.Text = "您已經訂購了以下書籍:<br />" & Orders

8 CheckBoxList控制項方法 方法名稱 用途 清除所有項目 新增一個項目 刪除index所指的項目
CheckBoxList1.items.Clear() 清除所有項目 CheckBoxList1.items.Add(“字串”) 新增一個項目 CheckBoxList1.items.RemoveAt(index) 刪除index所指的項目

9 動態增加資料項目-ch05-04 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) '以 TextBox1.Text 與 TextBox2.Text 做為項目的Text 與 Value 屬性,然後將其加入 CheckboxList1 CheckBoxList1.Items.Add(New ListItem(TextBox1.Text & ", $" & TextBox2.Text, TextBox2.Text)) '將 Textbox1、Textbox2 與 Label1 欄位清空 TextBox1.Text = "“ : TextBox2.Text = "“ : Label1.Text = "" End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) '用迴圈從 CheckBoxList1 由後至前逐一讀取各項目屬性 For Index As Integer = CheckBoxList1.Items.Count - 1 To 0 Step -1 '如果 Selected 屬性為 True,表示使用者已勾選 If CheckBoxList1.Items(Index).Selected = True Then CheckBoxList1.Items.RemoveAt(Index) '刪除該項目 End If Next Label1.Text = "“ '清空 Label1 欄位 Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim Total As Integer = 0 '用迴圈從 CheckBoxList1 由前至後逐一讀取各項目 Value 屬性 For Index As Integer = 0 To CheckBoxList1.Items.Count - 1 If CheckBoxList1.Items(Index).Selected Then Total += CheckBoxList1.Items(Index).Value '相加計算總價 Label1.Text = "總價格為 " & Total '將總價顯示於 Label1 控制項

10 RadioButtonList單選項目清單
RadioButtonList1.SelectedIndex: 被選擇項目的索引編號,若小於零表示未選 RadioButtonList1.SelectedItem.Text: 被選擇的項目文字 RadioButtonList1.SelectedItem.Value: 被選擇項目的值

11 RadioButtonList範例-ch05-05
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim Score As Integer = 0, Number As Integer = 0 '如果在問題 A 選擇了第 2 個項目便答對 If RadioButtonList1.SelectedIndex = 1 Then Score += 2 '問題 A 得 2 分 Number += 1 End If '如果在問題 B 選擇了第 4 個項目便答對 If RadioButtonList2.SelectedIndex = 3 Then Score += 3 '問題 B 得 3 分 '使用 Label1 控制項顯示答對題數與得分 Label1.Text = "問題 A 的答案為 2, 而您選擇 " & _ RadioButtonList1.SelectedItem.Text & _ "<br />問題 B 的答案為 12, 而您選擇 " & _ RadioButtonList2.SelectedItem.Text & _ "<br />所以您答對了" & Number & "題, 得分為 " & Score End Sub

12 DropDownList下拉式清單 DropDownList1.SelectedValue屬性: 選取到的值
DropDownList1.SelectedItem.Value 選取到的值 DropDownLlist1.SelectedItem.Text 選取到的標題文字 DropDownList1.SelectedIndex 選取項目的索引值

13 DropDownList下拉式選單 DropDownLlist1.SelectedValue ‘AutoPostBack必須要設為True
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) ‘如果使用者選擇的不是第一個項目 If DropDownList1.SelectedItem.Value <> "none" Then ‘依照所選項目的Value屬性轉往其他網站 Response.Redirect(DropDownList1.SelectedItem.Value) End If End Sub

14 DropDownList下拉式選單 DropDownList1.Items.Add:新增項目
DropDownList1.Items.Clear:清除所有項目

15 ListBox-選項列表 SelectionMode=“Multiple” AutoPostBack=“True”
Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) '清空 Label1 的文字 Label1.Text = "" '用迴圈從 ListBox1 由前至後逐一讀取各項目 For Index As Integer = 0 To ListBox1.Items.Count - 1 '若 Selected 屬性為 True,表示使用者已選擇此項 If ListBox1.Items(Index).Selected Then Label1.Text += ListBox1.Items(Index).Text & " " End If Next End Sub

16 作業 請建立asp.net網頁來計算購買多份早餐(主餐+飲料)的總價,主餐有三明治一個30元,漢堡一個40元,飲料部分豆漿一杯20元,奶茶一杯25元,在表單選擇主餐和飲料種類,然後使用textbox分別輸入購買數量後,在label顯示計算結果的金額。

17 作業 試寫一個飲料訂購程式,上面有紅茶、綠茶、奶茶、烏龍茶、與珍珠奶茶,每一種都可以選擇大杯或小杯,數量與是否去冰。當使用者按結帳鈕時,網頁會顯示所有選購的飲料與總價格。


Download ppt "ASP.NET 3.5 Ch5 進階伺服器控制項."

Similar presentations


Ads by Google