使用asp.net的带有Checkbox列表的嵌套转发器

时间:2013-04-04 08:43:44

标签: asp.net .net vb.net repeater

我正在尝试使用内部的复选框执行嵌套转发器控件。基本上,我想要的是 对复选框进行分类,例如,

Group 1
   Item 1
   Item 2

Group 2
   Item 3
   Item 4

Group 3
   Item 5
   Item 6

我面临的问题是,我收到错误:

错误1:未声明'DataRowView'。由于其防护等级,它可能无法访问 错误2:未声明名称“DataRowView”。

ASPX:

  <asp:Repeater ID="rp_Groups" runat="server" OnItemDataBound="rp_Groups_ItemDataBound" >
        <ItemTemplate>
            <ul>
                <asp:CheckBox runat="server" ID="chk_Group" Text='<%# Eval("category_type") %>' Value='<%# Eval("service_type_category_id") %>' onclick="OnGroupClick" />
                <p class="nested">
                 <asp:CheckBoxList runat="server" ID="chk_Items" DataValueField="ServiceTypeID" DataTextField="Name"
                 DataSource='<%# ((DataRowView)Container.DataItem).CreateChildView("FK_esnServiceType_Service_Type_Categorization") %>' ></asp:CheckBoxList>

                </p>
            </ul>
        </ItemTemplate>
    </asp:Repeater>

代码隐藏:

Public Sub Fill()  
    Dim dtServiceCategory As DataTable = ServiceTypeModel.GetService_Categories()
    Dim dtServiceType As DataTable = ServiceTypeModel.Search("", True)

    rp_Groups.DataSource = dtServiceCategory
    rp_Groups.DataBind()

    Dim ds As New DataSet()
    ds.Tables.Add(dtServiceCategory)
    ds.Tables.Add(dtServiceType)

    Dim relation As New DataRelation("FK_esnServiceType_Service_Type_Categorization", ds.Tables("dtServiceCategory").Columns("service_type_category_id"), ds.Tables("dtServiceType").Columns("CategorizationID"), False)
    ds.Relations.Add(relation)
    relation.Nested = True
End Sub

Protected Sub rp_Groups_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rp_Groups.ItemDataBound
    Dim chklist As CheckBoxList = DirectCast(e.Item.FindControl("chk_Items"), CheckBoxList)
    If chklist IsNot Nothing Then
        chklist.DataSource = DirectCast(e.Item.DataItem, DataRowView).CreateChildView("FK_esnServiceType_Service_Type_Categorization")
        chklist.DataBind()
    End If
End Sub

我缺少什么?

2 个答案:

答案 0 :(得分:3)

您应该检查ItemDataBound事件中触发了哪个itemtype,这可能是也可能不是问题,因为您没有页眉和页脚模板,但这仍然是一个好习惯。

Protected Sub rp_Groups_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rp_Groups.ItemDataBound
  If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
    Dim chklist As CheckBoxList = DirectCast(e.Item.FindControl("chk_Items"), CheckBoxList)
    If chklist IsNot Nothing Then
      chklist.DataSource = DirectCast(e.Item.DataItem, DataRowView).CreateChildView("FK_esnServiceType_Service_Type_Categorization")
      chklist.DataBind()
    End If
  End If
End Sub

修改

我刚刚注意到,在您已经绑定了转发器的数据源之后,您正在添加关系。在添加关系后尝试移动.databind

编辑2

好的,你可以尝试一下。将datatables添加到dataset并将转发器数据源设置为:ds.Tables(0)

Public Sub Fill() 
    Dim ds As New DataSet() 
    Dim dtServiceCategory As DataTable = ServiceTypeModel.GetService_Categories()
    Dim dtServiceType As DataTable = ServiceTypeModel.Search("", True)
    ds.Tables.Add(dtServiceCategory)
    ds.Tables.Add(dtServiceType)
    Dim relation As New DataRelation("FK_esnServiceType_Service_Type_Categorization", ds.Tables("dtServiceCategory").Columns("service_type_category_id"), ds.Tables("dtServiceType").Columns("CategorizationID"), False)
    relation.Nested = True
    ds.Relations.Add(relation)

    rp_Groups.DataSource = ds.Tables(0)
    rp_Groups.DataBind()
End Sub

答案 1 :(得分:0)

确保已导入名称空间System.Data

尝试添加

<%@ Import Namespace="System.Data" %>

到您的ASPX文件。

作为另一种选择,您可以全局导入名称空间,而不仅仅是在一个页面中:

http://msmvps.com/blogs/simpleman/archive/2006/01/11/80804.aspx