首页 > 解决方案 > 如何更改下拉列表中的信息

问题描述

因此,如果您选择不同的单选按钮,我想制作一个显示新信息的下拉列表。我正在苦苦挣扎。例如,如果选择单选按钮 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.netvisual-studio

解决方案


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

Page-Cycle 会是这样的

  • Page_Load
  • RadioButton2_Changed
  • Page_LoadComplete

因此,请确保在 RadioButton2_Changed 或 Page_LoadComplete 中设置新内容。如果您尝试在 Page_Load 中执行此操作,您将拥有单选按钮的旧值,并且效果不佳...


推荐阅读