RadioButtonList變動時,控制其他控制項顯示或不顯示(asp.Net C#)

摘要:RadioButtonList變動時,控制其他控制項顯示或不顯示

本人是新手,看到小鋪上有人問相關的問題。因此我做了一個小測試。

當選擇1時,TextBox顯示,選擇其他則不顯示。以下為程式碼。

<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server"  
            onselectedindexchanged="RadioButtonList1_SelectedIndexChanged"  
            AutoPostBack="True">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </asp:RadioButtonList>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
    </div>
    </form>
</body>

以上是aspx,加入一個RadioButtonList控制項與一個TextBox,包含三個Item。重點在AutoPostBack

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue == "1")
        {
            TextBox1.Visible = true;
        }

        else
        {
            TextBox1.Visible = false;
        }

    }

}

以上是cs,撰寫一個RadioButtonList的SelectedIndexChanged事件

判斷RadioButtonList的SelectValue為1時,TextBoxa顯示,否則不顯示。

希望這篇文章對跟我一樣是新手的人有些幫助。