简体   繁体   中英

How to create new radiobuttonlist with question on submit? c# asp.net

i have:

<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>

how can i create new entry of radiobuttonlist with new question after submit?? im princip create new 2, 3, 4 etc.

here is your code

 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;
}  

here your 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>

You should place third entry between:

<%
    if (needToShowThirdEntry) {

%>

and

<%
  }

%>

So, your code:

<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>

UPDATE As you updated your question, my new answer would be:

<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>
  <%
   }        
  %>

But, as this is separate list, you should make normal radio button list (without if) and use newRBList.Visible property from code behind to hide it on first rendering (before postback)

你可以试试这个selectdYesNo.Items.Add(new ListItem("text","value"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM