绑定asp:asp:datalist中的checkboxlist c#

时间:2012-02-10 18:24:22

标签: c# asp.net data-binding datalist checkboxlist

我从表中asp:datalist绑定此表包含两列,其中一列string,另一列是list<string>

我绑定了<div>中的字符串值,我想在asp中绑定list<string>:CheckBoxList如何在html或.cs中执行此操作?

这是html部分

<asp:DataList ID="feedbacklist" RepeatDirection="Vertical" runat="server" Width="100%">
        <ItemTemplate>
            <div class="rowstyle2" style="width: auto">
                    <%# Eval("Question")%>
            </div>
            <div class="news_des">
             <asp:CheckBoxList ID="answerslist" runat="server" RepeatDirection="Horizontal" Width="100px">
             </asp:CheckBoxList>
            </div>
        </ItemTemplate>
    </asp:DataList>

这是页面加载中的c#部分

List<string> boollist = new List<string>();
        List<string> stringlist = new List<string>();
        boollist.Add("Yes");
        boollist.Add("No");
        stringlist.Add("1");
        stringlist.Add("2");
        stringlist.Add("3");
        DataTable test = new DataTable();
        test.Columns.Add("Question", typeof(string));
        test.Columns.Add("Answers", typeof(List<string>));
        test.Rows.Add("What do you think of our new website?", boollist);
        test.Rows.Add("What do you think so?", stringlist);
        feedbacklist.DataSource = test;
        feedbacklist.DataBind();

2 个答案:

答案 0 :(得分:2)

您可能希望看到三件事:

  1. 使用BindingList
  2. 名称属性
  3. 中添加BindableAttribute
  4. 绑定到CheckListBox Binding DropDownList, ListBox and CheckBoxList Control the ADO.NET way

答案 1 :(得分:0)

<asp:DataList ID="feedbacklist" RepeatDirection="Vertical" runat="server" Width="100%">
                <ItemTemplate>
                    <div class="rowstyle2" style="width: auto">
                            <%# Eval("Question")%>
                    </div>
                    <div class="news_des">
                     <asp:RadioButtonList ID="answerslist" runat="server" RepeatDirection="Horizontal" DataSource='<%# Eval("Answers")%>'>
                     </asp:RadioButtonList>
                    </div>
                </ItemTemplate>
            </asp:DataList>