iT邦幫忙

2023 iThome 鐵人賽

DAY 25
0

主要介紹 CheckBox、RadioButton、TextBox 這三個的使用方式。

CheckBox

  • 特色:適合用來多選
<h1>選擇喜歡的國家(可複選)</h1>
<p>
    <asp:CheckBox ID="CheckBox1" runat="server" Text="日本"/>
    <asp:CheckBox ID="CheckBox2" runat="server" Text="中國"/>
    <asp:CheckBox ID="CheckBox3" runat="server" Text="韓國"/>
    <asp:CheckBox ID="CheckBox4" runat="server" Text="美國"/>
    <asp:CheckBox ID="CheckBox5" runat="server" Text="英國"/>
    <asp:CheckBox ID="CheckBox6" runat="server" Text="德國"/>
    <asp:CheckBox ID="CheckBox7" runat="server" Text="荷蘭"/>
</p>
<p>
    <asp:Button ID="Button1" runat="server" Text="確定" OnClick="Button1_Click" />
</p>
<p>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</p>

https://ithelp.ithome.com.tw/upload/images/20231005/20162273af31iT4VEY.jpg

protected void Button1_Click(object sender, EventArgs e)
{
    string Country = "";

    if (CheckBox1.Checked)
        Country += "\n" + CheckBox1.Text;
    else if(CheckBox1.Checked)Country = CheckBox1.Text;

    if (CheckBox2.Checked)
        Country += "\n" + CheckBox2.Text;
    else if (CheckBox2.Checked) Country = CheckBox2.Text;

    if (CheckBox3.Checked)
        Country += "\n" + CheckBox3.Text;
    else if (CheckBox3.Checked) Country = CheckBox3.Text;

    if (CheckBox4.Checked)
        Country += "\n" + CheckBox4.Text;
    else if (CheckBox4.Checked) Country = CheckBox4.Text;

    if (CheckBox5.Checked)
        Country += "\n" + CheckBox5.Text;
    else if (CheckBox5.Checked) Country = CheckBox5.Text;

    if (CheckBox6.Checked)
        Country += "\n" + CheckBox6.Text;
    else if (CheckBox6.Checked) Country = CheckBox6.Text;

    if (CheckBox7.Checked)
        Country += "\n" + CheckBox7.Text;
    else if (CheckBox7.Checked) Country = CheckBox7.Text;

    Label1.Text = "喜歡的有:" + Country;
}

https://ithelp.ithome.com.tw/upload/images/20231005/20162273WNSHcwmXQo.jpg

執行の結果


https://ithelp.ithome.com.tw/upload/images/20231005/20162273knfB18BDuw.jpg

https://ithelp.ithome.com.tw/upload/images/20231005/20162273fyZmvmvr8g.jpg

RadioButton

  • 特色:適合用來單選
<h1>選擇喜歡的社團</h1>
<p>
    <asp:RadioButton ID="RadioButton1" runat="server" GroupName="club" Text="天文社"/>
    <asp:RadioButton ID="RadioButton2" runat="server" GroupName="club" Text="辯論社"/>
    <asp:RadioButton ID="RadioButton3" runat="server" GroupName="club" Text="吉他社"/>
    <asp:RadioButton ID="RadioButton4" runat="server" GroupName="club" Text="籃球社"/>
    <asp:RadioButton ID="RadioButton5" runat="server" GroupName="club" Text="桌遊社"/>
</p>
<p>
    <asp:Button ID="Button1" runat="server" Text="確定" OnClick="Button1_Click" />
</p>
<p>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</p>

https://ithelp.ithome.com.tw/upload/images/20231005/20162273vI3QIeKLGH.jpg

protected void Button1_Click(object sender, EventArgs e)
{
    string club = "";

    if(RadioButton1.Checked)
        club += RadioButton1.Text;

    if (RadioButton2.Checked)
        club += RadioButton2.Text;

    if (RadioButton3.Checked)
        club += RadioButton3.Text;

    if (RadioButton4.Checked)
        club += RadioButton4.Text;

    if(RadioButton5.Checked)
        club += RadioButton5.Text;

    Label1.Text = "喜歡的是:" + club;
}

https://ithelp.ithome.com.tw/upload/images/20231005/20162273rr0ppxszgw.jpg

執行の結果


https://ithelp.ithome.com.tw/upload/images/20231005/20162273hP01IbsjaF.jpg

https://ithelp.ithome.com.tw/upload/images/20231005/20162273tnlQ8hJVpO.jpg

TextBox

  • 讓使用者可以輸入資料。
<div>
    姓名:<asp:TextBox ID="TextBox1" runat="server" 
        AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</div>
<p>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</p>

💡
AutoPostBack 設為 true,當使用者輸入後按下 Enter 鍵後,就會觸發 TextChanged 事件,再來就會呼叫 OnTextChanged 方法來處理該事件。

https://ithelp.ithome.com.tw/upload/images/20231005/20162273u8qxt4PeOz.jpg

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Label1.Text = "Hi,"+ TextBox1.Text;
}

https://ithelp.ithome.com.tw/upload/images/20231005/20162273tAkKzcNgVC.jpg

執行の結果


https://ithelp.ithome.com.tw/upload/images/20231005/20162273TsTrFTJve2.jpg

https://ithelp.ithome.com.tw/upload/images/20231005/201622733fym5ztSKG.jpg
※以上資料如有錯誤請多指教

參考資料

書名:ASP.NET 4.6動態網頁程式設計技術實作:使用C#


上一篇
Day24 - 一般控制項
下一篇
Day26 - 清單控制項
系列文
連續30天學習C#和ASP.NET30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言