ASP.NET+SQL SERVER的應用開發基礎(一)

ASP.NET基本控件使用小練習

【注】:以下均使用VS 2017操作。

練習1:設置文本字體

1.1、頁面控件

1個文本框TextBoxTextBox
3個單選按鈕RadioButtonRadioButton
1個複選框CheckBoxListCheckBoxList
1個命令按鈕ButtonButton

1.2、控件排版

  1. 設置1個文本框TextBoxTextBox
    首先在工具箱中選中TextBoxTextBox,拉大文本框至適當大小和位置。然後在屬性的行爲中,設置TextModeTextModeMultilineMultiline(多行)。
  2. 設置3個單選按鈕RadioButtonRadioButton
    首先在工具箱中選中RadioButtonRadioButton,雙擊三次。然後在屬性的Font中分別設置TextText爲隸書、楷體、黑體,在行爲中設置GroupNameGroupName均爲group1group1,在佈局中設置RepeatDirectionRepeatDirectionHorizonalHorizonal
  3. 設置1個複選框CheckBoxListCheckBoxList
    首先在工具箱中選中CheckBoxListCheckBoxList,雙擊一次。然後點擊複選框右邊的小箭頭,勾選AutoPostBackAutoPostBack,點擊編輯項,再點擊3次添加,分別更改TextText爲加粗、斜體、下滑線,點擊確認。

在這裏插入圖片描述

  1. 1個命令按鈕ButtonButton
    在工具箱中選中ButtonButton,更改TextText爲提交。
  • AutoPostbackAutoPostback屬性
    • 事件發生後,立即執行相應的事件處理代碼, 需要相應控件的AutoPostbackAutoPostback屬性爲TrueTrue;否則整個頁面被提交後才統一執行事件處理代碼(減少vweb$頁面在服務器和客戶端瀏覽器之間的傳輸次數,以減輕對服務器及網絡帶寬的壓力)。
    • 可以採取單擊命令按鈕Button1Button1,提交webweb頁面。因此有時頁面添加一個ButtonButton按鈕但對應ClickClick代碼爲空。
  • 單選按鈕RadioButtonRadioButton
    • 若希望3個單選按鈕互斥,則需要將3個單選按鈕的GroupNameGroupName屬性設置爲同一個值。同樣功能可以用RadioButtonListRadioButtonList控件來實現。
  • 複選框CheckBoxListCheckBoxList
    • RepeatDirectionRepeatDirection屬性verticalverticalHorizontalHorizontal排列

1.3、實現代碼

分別點擊全部控件,實現部分代碼如下。

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        TextBox1.Font.Name = "隸書";
    }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        TextBox1.Font.Name = "楷體_GB2312";
    }

    protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
    {
        TextBox1.Font.Name = "黑體";
    }

    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CheckBoxList1.Items[0].Selected)
            TextBox1.Font.Bold = true;
        if (CheckBoxList1.Items[1].Selected)
            TextBox1.Font.Italic = true;
        if (CheckBoxList1.Items[2].Selected)
            TextBox1.Font.Underline = true;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (CheckBoxList1.Items[0].Selected == false)
            TextBox1.Font.Bold = false;
        if (CheckBoxList1.Items[1].Selected == false)
            TextBox1.Font.Italic = false;
        if (CheckBoxList1.Items[2].Selected == false)
            TextBox1.Font.Underline = false;
    }

1.4、實現效果

在這裏插入圖片描述

練習2: 圖片瀏覽

2.1 頁面控件

1個Image
1個下拉列表DropDownList

2.2 控件排版

  1. 新建文件夾image
    在這裏插入圖片描述
    在資源管理器中打開文件夾,在image文件夾中添加5張照片;
  2. 添加imageimage控件
    拉大選框,在屬性ImageUrlImageUrl中添加圖片路徑“~/image/1.JPG”,在屬性AlternateTextAlternateText添加圖片路徑“ ~/image/1.JPG”(圖片不存在時顯示的文本內容)
    在這裏插入圖片描述
  3. 添加DropDownListDropDownList控件
    啓用AutoPostBackAutoPostBack,編輯項,增加item0~item4共5項,ListItem中加入‘ ~/image/1.JPG ’到‘ ~/image/5.JPG ’
    在這裏插入圖片描述

2.3 實現代碼

點擊DropDownList控件,編寫代碼,實現DropDownList控件與image控件聯繫;

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    { 
        Image1.ImageUrl = DropDownList1.SelectedValue;
        Image1.AlternateText = DropDownList1.SelectedValue;
    }

2.4 實現效果

在這裏插入圖片描述

練習3: 選擇出行時間和目標城市

3.1 頁面控件

2個標籤Label
2個列表框ListBox
2個命令按鈕Button
1個文本框TextBox
1個日曆控件Calendar

3.2 控件排版

  1. 添加兩個Label控件
    分別設置Text屬性爲“可選城市”和“目標城市”;
    在這裏插入圖片描述
  2. 添加兩個ListBox控件
    設置ListBox1的SelectionMode屬性爲Multiple(允許選擇多項),啓用AutoPostBackAutoPostBack,編輯項,加入若干城市;
    在這裏插入圖片描述
    在這裏插入圖片描述
  3. 添加兩個Button控件
    分別設置Text屬性爲“確認”和“出發時間”;
    在這裏插入圖片描述
  4. 添加TextBox控件
    如下圖所示:
    在這裏插入圖片描述
  5. 添加Calendar控件
    設置Calendar的Visible屬性爲False,ShowDayHeader屬性爲False;
    在這裏插入圖片描述

3.3 實現代碼

	protected void Button1_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Add(ListBox1.SelectedValue);
        ListBox1.Items.Remove(ListBox1.SelectedItem);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
        Button1.Visible = false;
        Button2.Enabled = false;
        ListBox1.Visible = false;
        ListBox2.Visible = false;
        Label1.Visible = false;
        Label2.Visible = false;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox1.Text = "出發時間:" + Calendar1.SelectedDate.ToShortDateString();
        Calendar1.Visible = false;
        ListBox1.Visible = true;
        ListBox2.Visible = true;
        Button1.Visible = true;
        Button2.Enabled  = true;
        Label1.Visible = true;
        Label2.Visible = true;
    }

3.4 實現效果

在這裏插入圖片描述

練習4:用戶註冊界面設計

注:沒有包括對註冊信息的處理,只是建立一個註冊界面

4.1 頁面控件

6個Label
5個TextBox
1個Command
1個必填驗證控件RequiredFieldValidator
1個比較驗證控件CompareValidator
1個範圍驗證控件RangeValidator
1個正則表達式驗證控件RegularExpressionValidator

4.2 控件排版

  1. 添加6個Label控件
    分別設置爲如下圖所示:
    在這裏插入圖片描述
  2. 添加5個TextBox控件
    如下圖所示:
    在這裏插入圖片描述
  3. 添加一個Button控件
    設置爲確認;
    在這裏插入圖片描述
  4. 添加驗證控件
    (1)RequiredFieldValidator:
    ControlToValidate= TextBox1;
    ErrorMessage=“用戶ID不能爲空”
    (2)CompareValidator:
    ControlToCompare= TextBox2;
    ControlToValidate= TextBox3;
    ErrorMessage=“兩次輸入的密碼不相同”
    (3)RangeValidator:
    ControlToValidate= TextBox4;
    MaximumValue=120
    MinimumValue=1
    ErrorMessage=“年齡應在1-120之間”
    Type=integer
    (4)RegularExpressionValidator:
    ControlToValidate= TextBox5;
    ValidationExpression,正則表達式編輯器 選“Internet電子郵件地址”
    ErrorMessage=“輸入的信箱不正確”
    在這裏插入圖片描述

4.3 實現效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章