【问题标题】:How to make the information in a dropdownlist change如何使下拉列表中的信息发生变化
【发布时间】:2020-09-23 11:02:07
【问题描述】:

因此,如果您选择不同的单选按钮,我想制作一个显示新信息的下拉列表。 我正在苦苦挣扎。例如,如果选择单选按钮 1,则保管箱中的信息必须更改为爆米花和糖果。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            margin-left: 18px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Day" />
        </div>
        <asp:RadioButton ID="RadioButton2" runat="server" Text="Night" />
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>Cream Soda</asp:ListItem>
            <asp:ListItem>coke</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style1" Width="139px"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Seat No:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" Width="140px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Height="20px" Text="Add Choice" />
        <br />
        <asp:Label ID="Label3" runat="server"></asp:Label>
    </form>
</body>
</html>

【问题讨论】:

  • 有更新吗?如果您的问题已经解决,您可以点击“✔”将相应的回复标记为答案。

标签: c# asp.net visual-studio


【解决方案1】:

您必须在 RadioButton 上定义 autopostback="true",然后您需要在代码隐藏中处理选定的值,并使用新的项目列表重新填充下拉列表。

Page-Cycle 会是这样的

  • Page_Load
  • RadioButton2_Changed
  • Page_LoadComplete

因此,请确保在 RadioButton2_Changed 或 Page_LoadComplete 中设置新内容。如果您尝试在 Page_Load 中执行此操作,您将拥有单选按钮的旧值,这将无法正常工作...

【讨论】:

  • 谢谢,有人告诉我的值我需要 2 个数组,但似乎这不是必需的,或者我应该在哪里存储 vlaues。
【解决方案2】:

您可以尝试以下步骤在单击单选按钮时显示相应的信息。

首先,您需要将您的 html 文件更改为以下内容:

<body>
   <form id="form1" runat="server">
        <div>
            <asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Day" GroupName="Ra"  AutoPostBack="true" />
        </div>
        <asp:RadioButton ID="RadioButton2" runat="server" Text="Night" OnCheckedChanged="RadioButton2_CheckedChanged" GroupName="Ra"  AutoPostBack="true"/>
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style1" Width="139px"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Seat No:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" Width="140px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Height="20px" Text="Add Choice" />
        <br />
        <asp:Label ID="Label3" runat="server"></asp:Label>
    </form>
</body>

其次,您需要调用 RadioButton1_CheckedChanged 事件和 RadioButton2_CheckedChanged 事件来添加列表项。

ListItem list1 = new ListItem("popcorn");
ListItem list2 = new ListItem("sweets");
ListItem list3 = new ListItem("Cream Soda");
ListItem list4 = new ListItem("coke");
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    if(RadioButton1.Checked==true)
    {
        if (!DropDownList1.Items.Contains(list1) && !DropDownList1.Items.Contains(list2))
        {
            DropDownList1.Items.Clear();
            DropDownList1.Items.Add(list1);
            DropDownList1.Items.Add(list2);
        }  
    }
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
    if (RadioButton2.Checked == true)
    {

        if (!DropDownList1.Items.Contains(list3) && !DropDownList1.Items.Contains(list4))
        {
            DropDownList1.Items.Clear();
            DropDownList1.Items.Add(list3);
            DropDownList1.Items.Add(list4);
        }
        
    }
}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-11
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    相关资源
    最近更新 更多