2017-07-27 58 views
2

我有ASP嵌套Repeater象下面這樣:ASP嵌套Repeater錯誤

<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 

這裏我怎麼bi ND第一中繼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

看起來像指向rptMenu,也在第一個查詢中選擇menuID(在Load_menu()中)。我認爲它會解決你的問題 – Aby

回答

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