2013-04-04 41 views
0

我想做一个嵌套的中继器控制与复选框里面。基本上,我想是 分类喜欢的复选框,与复选框列表使用asp.net嵌套中继器

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 

我在想什么?

回答

3

您应检查哪些项目类型正在对ItemDataBound触发的事件,这可能会或可能不会成为问题,因为您没有页眉和页脚模板,但它仍然是一个好习惯。

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

EDIT 2

OK,你可以试试这个。添加datatablesdataset和转发数据源设置为: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 
+0

我尝试了你的想法,并得到错误:'DataRowView'是'数据'中的一种类型,不能用作表达式。 \t当我删除checkboxlist的数据源时,我没有收到任何错误! – Anuya 2013-04-04 09:07:26

+0

@Anuya你是否尝试过我在编辑中建议的内容? – 2013-04-04 09:18:09

+0

是的,我做了,绑定中继器之前移动关系 – Anuya 2013-04-04 09:20:50

0

确保您已导入的命名空间System.Data

尝试

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

添加到您的ASPX文件。

正如你可以导入命名空间全局,而不是只在一个页面另一种选择:

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

+0

现在我得到错误:“DataRowView的”是一个类型,不能用作表达式。 – Anuya 2013-04-04 08:53:09