使用转发器的多个复选框列表

时间:2012-07-03 10:32:19

标签: c# asp.net data-binding repeater

以下是我的代码,显示一个标题后跟一个checkboxlist

标记:

<asp:Repeater id="repRoles" runat="server">
       <ItemTemplate>
              <asp:literal runat="server" text=<%# DataBinder.Eval(Container.DataItem, "ItemTitle") %>></asp:literal>
              <br />
              <asp:CheckBoxList runat="server" DataSourceID=<%# FillinCbl(DataBinder.Eval(Container.DataItem,"ItemID").ToString()) %> 
                                    DataTextField="ItemName" DataValueField="ItemValue">
              </asp:CheckBoxList>
       </ItemTemplate>
</asp:Repeater>

代码背后:

private void FillinRepeater()
{
    try
    {
        List<Item> targetAudienceParent = new List<Item>();
        DataTable tempTbl = new DataTable();
        tempTbl.Columns.Add("ItemTitle", typeof(String));
        tempTbl.Columns.Add("ItemID", typeof(String));
        DataRow tempRow;
        int tempCount = 0;
        foreach (Item a in itmECM.Children)
        {
            try
            {
                tempRow = tempTbl.NewRow();
                tempRow["ItemTitle"] = a.Name.ToString();
                tempRow["ItemID"] = a.ID.ToString();
                tempTbl.Rows.Add(tempRow);
                tempCount++;
            }
            catch
            {
            }
        }
        repRoles.DataSource = tempTbl;
        repRoles.DataBind();
    }
    catch
    {
    }
}
public DataTable FillinCbl(String passedString)
{
    dtlRoles = new DataTable();
    dtlRoles.Columns.Add("Name", typeof(String));
    dtlRoles.Columns.Add("OptInID", typeof(String));
    dtlRoles.Columns.Add("OptOutID", typeof(String));
    Item tempItm = master.GetItem(passedString);
    DataTable tempTbl = new DataTable();
    tempTbl.Columns.Add("ItemName", typeof(String));
    tempTbl.Columns.Add("ItemValue", typeof(String));
    DataRow tempRow;
    try
    {
        populateUsersList(tempItm);
        Session["dtlRoles"] = dtlRoles;
        for (int a = 0; a < dtlRoles.Rows.Count; a++)
        {
            try
            {
                Response.Write(dtlRoles.Rows[a][0].ToString().Trim() + "<br/>");
                tempRow = tempTbl.NewRow();
                tempRow["ItemName"] = dtlRoles.Rows[a][0].ToString().Trim();
                tempRow["ItemValue"] = dtlRoles.Rows[a][1].ToString().Trim();
                tempTbl.Rows.Add(tempRow);
            }
            catch(Exception ex1)
            {
                Response.Write(ex1.ToString() + "<br/>");
            }
        }
    }
    catch (Exception ex2)
    {
        Response.Write(ex2.ToString() + "<br/>");
    }
    return tempTbl;
}

问题只在于文字显示。 checkboxlist未填写。

response.write() DataBind checobxlist方法{{1}}时,他们就出局了。

所以我的代码背后有效。但是有些东西不见了。但是我找不到什么。

1 个答案:

答案 0 :(得分:1)

将DataSourceID更改为DataSource

<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSource=<%# FillinCbl(DataBinder.Eval(Container.DataItem,"ItemID").ToString()) %>  DataTextField="ItemName" DataValueField="ItemValue">