我有ASP嵌套转发器,如下所示:
<asp:Repeater ID="rptMenu" runat="server">
<ItemTemplate>
<div class="box">
<div class="box-header">
<h3 class="box-title"><asp:Label ID="lblHeader" runat="server"></asp:Label></h3>
</div>
<div class="box-body no-padding">
<table class="table table-striped">
<tr>
<th style="width: 10px">#</th>
<th>ID</th>
<th>Nama</th>
</tr>
<tr>
<asp:Repeater ID="rptDetail" runat="server">
<ItemTemplate>
<td><asp:CheckBoxList ID="cblChild" runat="server"></asp:CheckBoxList></td>
<td><asp:Label ID="LblID" runat="server"></asp:Label></td>
<td><asp:Label ID="LblName" runat="server"></asp:Label></td>
</ItemTemplate>
</asp:Repeater>
</tr>
</table>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
这里有什么代码:
加载页面时,调用Load_menu()
:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not Page.IsPostBack Then
Call Load_menu()
End If
Catch ex As Exception
Response.Write("Page_Load Exception :<br>" & ex.ToString)
End Try
End Sub
这里绑定到转发器的Load_menu()
函数:
Private Sub Load_menu()
Try
sqlstring = "SELECT DISTINCT a.parent, b.name FROM masterMenu a join masterMenu b " & _
"ON a.parent = b.menuID JOIN rolesDetail c " & _
"ON a.menuID = c.menuID " & _
"WHERE b.[status] = 1 "
DS = SQLExecuteQuery(sqlstring)
DT = DS.Tables(0)
rptMenu.DataSource = DT
rptMenu.DataBind()
Catch ex As Exception
Response.Write("Load_Menu Exception :<br>" & ex.ToString)
End Try
End Sub
在这里我如何绑定第一个转发器rptMenu
:
Private Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
Try
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rDS As New DataSet
Dim rDT As New DataTable
Dim lblHeader As Label = e.Item.FindControl("lblHeader")
Dim dr As DataRowView = e.Item.DataItem
lblHeader.Text = dr.Item("name").ToString
sqlstring = "SELECT a.menuID, a.name, b.[status] " & _
"FROM masterMenu a LEFT JOIN rolesDetail b ON b.menuID=a.menuID " & _
"WHERE a.[status] = 1 and a.parent= '" & dr.Item("parent") & "' and b.RoleID = '" & roleID & "' " & _
"ORDER BY a.menuID "
rDS = SQLExecuteQuery(sqlstring)
rDT = rDS.Tables(0)
rptDetail.DataSource = rDT
rptDetail.DataBind()
End If
Catch ex As Exception
Throw New Exception("<b>Error RptMenu Data bound :</b>" & ex.ToString)
End Try
End Sub
它将绑定第二个转发器rptDetail
:
Private Sub rptDetail_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
Try
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lblID As Label = e.Item.FindControl("LblID")
Dim lblName As Label = e.Item.FindControl("LblName")
Dim cblChild As CheckBoxList = e.Item.FindControl("cblChild")
Dim dr As DataRowView = e.Item.DataItem
lblID.Text = dr.Item("menuID").ToString
lblName.Text = dr.Item("name").ToString
End If
Catch ex As Exception
Throw New Exception("<b>Error RptDetail Data bound :</b>" & ex.ToString)
End Try
End Sub
我确保所有查询都有效且有效。我收到以下错误:
Load_Menu Exception :
System.Exception: Error RptDetail Data bound :System.ArgumentException: menuID is neither a DataColumn nor a DataRelation for table Table.
at System.Data.DataRowView.get_Item(String property)
at LMS.role_detail.rptDetail_ItemDataBound(Object sender, RepeaterItemEventArgs e) in D:\Others\Ekspedisi\LMS\konfigurasi\role-detail.aspx.vb:line 165
at LMS.role_detail.rptDetail_ItemDataBound(Object sender, RepeaterItemEventArgs e) in D:\Others\Ekspedisi\LMS\konfigurasi\role-detail.aspx.vb:line 178
at System.Web.UI.WebControls.RepeaterItemEventHandler.Invoke(Object sender, RepeaterItemEventArgs e)
at System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem)
at System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) at System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e)
at LMS.role_detail.Load_menu() in D:\Others\Ekspedisi\LMS\konfigurasi\role-detail.aspx.vb:line 120
一直在努力解决这个错误而没有结果。我不理解手段neither datacolumn
。我查看了表格,该列中应该存在menuID
。
我正在使用Visual Studio 2012和SQL Server 2014。
此处masterMenu
表和示例:
CREATE TABLE [dbo].[masterMenu](
[menuID] [varchar](255) NOT NULL,
[name] [varchar](255) NULL,
[order] [int] NULL,
[level] [int] NULL,
[parent] [varchar](255) NOT NULL,
[icon] [varchar](255) NULL,
[cssClass] [varchar](255) NULL,
[url] [varchar](255) NULL,
[target] [varchar](255) NOT NULL,
[lastModified] [datetime] NULL CONSTRAINT [DF__MasterMen__lastm__15502E78] DEFAULT (NULL),
[status] [smallint] NULL CONSTRAINT [DF__MasterMen__statu__164452B1] DEFAULT ((1)),
[timestamp] [datetime] NULL CONSTRAINT [DF__MasterMen__times__173876EA] DEFAULT (getdate()),
CONSTRAINT [PK__MasterMe__3214EC271367E606] PRIMARY KEY CLUSTERED
(
[menuID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'A0000', N'Kategori Berat', 100000000, 1, N'A0000', N'fa fa-balance-scale', NULL, N'/master/category.aspx', N'content', NULL, 1, CAST(N'2010-03-05 11:51:47.293' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'B0000', N'Transaksi Kontainer', 200000000, 1, N'B0000', N'fa fa-truck', N'', N'/warehouse/transaksi.aspx', N'content', NULL, 1, CAST(N'2010-01-25 15:02:58.000' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'C0000', N'Report', 300000000, 1, N'C0000', N'fa fa-file-text', N'fa fa-chevron-down', N'#', N'content', NULL, 1, CAST(N'2010-03-08 11:41:12.457' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'C0001', N'Daftar Transaksi Kontainer', 301000000, 2, N'C0000', N'fa fa-circle', N'', N'/reports/listContainerTransaction.aspx', N'content', NULL, 1, CAST(N'2010-03-08 11:41:12.457' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'C0002', N'Rencana Muat Kapal', 301000000, 2, N'C0000', N'fa fa-circle', N'', N'/reports/loadingPlan.aspx', N'content', NULL, 1, CAST(N'2010-03-08 11:41:12.457' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'E0000', N'Konfigurasi', 500000000, 1, N'E0000', N'fa fa-gears', N'fa fa-chevron-down', N'#', N'content', NULL, 1, CAST(N'2010-01-25 15:02:58.000' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'E0001', N'Master Company', 501000000, 2, N'E0000', N'fa fa-building', NULL, N'/master/company.aspx', N'content', NULL, 1, CAST(N'2010-03-05 11:51:47.293' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'E0003', N'Pendaftaran User', 503000000, 2, N'E0000', N'fa fa-user', NULL, N'/konfigurasi/user.aspx', N'content', NULL, 1, CAST(N'2010-01-25 15:02:58.000' AS DateTime))
INSERT [dbo].[masterMenu] ([menuID], [name], [order], [level], [parent], [icon], [cssClass], [url], [target], [lastModified], [status], [timestamp]) VALUES (N'E0004', N'Roles', 504000000, 2, N'E0000', N'fa fa-unlock', NULL, N'/konfigurasi/roles.aspx', N'content', NULL, 1, CAST(N'2010-01-25 15:02:58.000' AS DateTime))
答案 0 :(得分:1)
扩展:
<Extension()>
Public Function GetControl(Of T As Control)(par As Control, cntrl As String) As T
Return CType(par.FindControl(cntrl), T)
End Function
<Extension()>
Public Function GetRepeater(cont As Control, rptr As String) As Repeater
Return cont.GetControl(Of Repeater)(rptr)
End Function
现在修改您的事件处理程序
Private Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
Try
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rDS As New DataSet
Dim rDT As New DataTable
Dim lblHeader As Label = e.Item.FindControl("lblHeader")
Dim dr As DataRowView = e.Item.DataItem
lblHeader.Text = dr.Item("name").ToString
sqlstring = "SELECT a.menuID, a.name, b.[status] " & _
"FROM masterMenu a LEFT JOIN rolesDetail b ON b.menuID=a.menuID " & _
"WHERE a.[status] = 1 and a.parent= '" & dr.Item("parent") & "' and b.RoleID = '" & roleID & "' " & _
"ORDER BY a.menuID "
rDS = SQLExecuteQuery(sqlstring)
rDT = rDS.Tables(0)
' Add this to get reference to the Nested Repeater
Dim rptr As Repeater = e.Item.GetRepeater("rptDetail")
AddHandler rptr.ItemDataBound, AddressOf rptDetail_ItemDataBound
rptr.DataSource = rDT
rptr.DataBind()
End If
Catch ex As Exception
Throw New Exception("<b>Error RptMenu Data bound :</b>" & ex.ToString)
End Try
End Sub
此外,您需要手动添加ItemDataBound
处理程序,您将无法访问嵌套的Repeater。事件处理程序需要像普通的Sub