代码之家  ›  专栏  ›  技术社区  ›  netseng

动态生成RadioButtonList

  •  -1
  • netseng  · 技术社区  · 17 年前

    我将生成一个 asp:CheckBoxList 使用此代码

    <%
    
    System.Collections.Generic.List<QuizzEngine.Common.Question> qList = (System.Collections.Generic.List<QuizzEngine.Common.Question>)Session[QuizzEngine.Common.SessionKeys.QuesionsList];
    
    int navigator = (int)Session[QuizzEngine.Common.SessionKeys.Navigator];
    
    if (True)
    
    {
    
    %>
    
    <'asp:CheckBoxList ID="cblistAnswers" runat="server" 
    
    <'Width="139px"style="text-align: left" AutoPostBack="False">
    
    <'asp:ListItem Value="1">Me<'/asp:ListItem>
    
    <'/asp:CheckBoxList>
    
    <%
    
    cblistAnswers.Items.Clear();
    
    foreach (QuizzEngine.Common.Answer answer in qList[navigator].QuestionAllAnswers)
    
    {
         ListItem i = new ListItem();
    
         i.Attributes.Add("runat", "server");
    
         i.Value = answer.AnswerId.ToString();
    
         i.Text = answer.AnswerText;
    
         cblistAnswers.Items.Add(i);
    
    }
    
    %>
    

    ListItem 我在标记中添加了:

    <'asp:ListItem Value="1">Me<'/asp:ListItem>
    

    出现时,我动态添加的其他项不会出现,我该怎么办?

    4 回复  |  直到 13 年前
        1
  •  1
  •   Ray Booysen    17 年前

    看看这个 MSDN Link . 它包含了创建复选框列表所需的所有详细信息和示例。

        2
  •  0
  •   netseng    17 年前

    我做到了 就像倒车一样 用于生成项目的服务器端代码 , 原因是在我的第一个例子中,在呈现控件后向控件中添加项时,这是错误的,首先向控件中添加项,然后将其标记写入呈现。

    我将我的服务器端代码与asp标记混合在一起,以便动态生成控件并显示它们,如果有其他方法,请提供。

        3
  •  0
  •   karlis    17 年前

    netseng,看看用户控件、占位符和用户控件的动态加载。我认为,使用这两个控件,您可以使页面更具动态性,并且,如果您可以重用用户控件,您将获得利润!

        4
  •  -1
  •   karlis    17 年前

    我相信你不需要这个:

    i.Attributes.Add("runat", "server");
    
    推荐文章