如何在提交时创建有问题的新单选按钮列表?c#asp.net

本文关键字:单选按钮 列表 net c#asp 有问题 提交 创建 | 更新日期: 2023-09-27 17:59:23

我有:

<div id="question">
        <div style="float: left; width: 250px;">
           <asp:Label ID="question" runat="server"></asp:Label></div>
        <div>
            <asp:RadioButtonList ID="selectdYesNo" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
                <asp:ListItem Text="No" Value="0"></asp:ListItem>
            </asp:RadioButtonList>
        </div>
    </div>
    <div id="btCreate" style="margin-left: 200px; margin-top: 10px;">
        <asp:Button runat="server" Text="Categorize" ID="btCategorize" />
    </div>

如何在提交后创建带有新问题的单选按钮列表的新条目??原则上创建新的2、3、4等

如何在提交时创建有问题的新单选按钮列表?c#asp.net

这是您的代码

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        PanelFirstQuestionBlock.Visible = true;
    }
}
protected void FirstQuestionGotAnswered(object sender, EventArgs e)
{
    PanelFirstQuestionBlock.Visible = false;
    PanelSecondQuestionBlock.Visible = true;
}  

这是您的ASP:HTML

<asp:Panel ID="PanelFirstQuestionBlock" runat="server" Visible="false">
        <h1>My first Question</h1>
        <asp:RadioButtonList ID="RadioButtonListAnswer1" runat="server" 
            OnSelectedIndexChanged="FirstQuestionGotAnswered">
            <asp:ListItem>yes</asp:ListItem>
            <asp:ListItem>no</asp:ListItem>
        </asp:RadioButtonList>
    </asp:Panel>
    <asp:Panel ID="PanelSecondQuestionBlock" runat="server" Visible="false">
    <h1>My second Question</h1>
        <asp:RadioButtonList ID="RadioButtonListAnswer2" runat="server">
            <asp:ListItem>yes</asp:ListItem>
            <asp:ListItem>no</asp:ListItem>
        </asp:RadioButtonList>
    </asp:Panel>

您可以这样尝试selectdYesNo.Items.Add(new ListItem("text","value"));

您应该将第三个条目放在:之间

<%
    if (needToShowThirdEntry) {
%>

<%
  }
%>

所以,你的代码:

<asp:RadioButtonList ID="selectdYesNo" runat="server" RepeatDirection="Horizontal">
     <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
     <asp:ListItem Text="No" Value="0"></asp:ListItem>
  <%
    if (needToShowThirdEntry) {     
  %>
     <asp:ListItem Text="Maybe" Value="2"></asp:ListItem>
  <%
   }        
  %>
</asp:RadioButtonList>

更新当你更新你的问题时,我的新答案是:

<asp:RadioButtonList ID="selectdYesNo" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
            <asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:RadioButtonList>

  <%
    if (needToShowSecondList) {     
  %>
<asp:RadioButtonList ID="newRBList" runat="server" RepeatDirection="Horizontal">
     <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
     <asp:ListItem Text="No" Value="0"></asp:ListItem>
     <asp:ListItem Text="Maybe" Value="2"></asp:ListItem>
</asp:RadioButtonList>
  <%
   }        
  %>

但是,由于这是一个单独的列表,您应该制作普通的单选按钮列表(没有if),并使用代码后面的newRBList.Visible属性在第一次渲染(回发之前)时将其隐藏