Asp.net

Handles 子句需要在包含類型或其基類型之一中定義的 WithEvents 變數

  • December 22, 2021

我得到錯誤

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

在下面的程式碼中..

Public Sub selCurrentManuf_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles selCurrentManuf.SelectedIndexChanged

   End Sub

與之配套的下拉列表如下…

<asp:DropDownList  
  OnSelectedIndexChanged="selCurrentManuf_SelectedIndexChanged" 
  selectedvalue='<%#Container.DataItem("c1_manufidcurrent")%>' 
   ID="selCurrentManuf" 
   Runat="Server" 
   DataTextField="c4_Desc" 
   DataValueField="c4_manufid"
   DataSource="<%# GetCurrentManuf() %>" 
   autopostback="true" 
></asp:DropDownList>

當我重命名一個欄位時,我得到了這個錯誤。出於某種原因,有時設計器會搞砸並且沒有在 form.aspx.designer.vb 文件中重命名它,如果您在其中查看並修復名稱,如果您遇到這種情況,它可以解決此問題。

因此,請仔細檢查偶數處理程序、aspx 文件和designer.aspx 文件中的欄位名稱是否相同。

我在升級舊頁面時遇到了這個問題。問題在於 Inherits 屬性 @Page 指令有錯誤的命名空間。

  1. 更正 Inherits 屬性的命名空間
  2. 移除設計器文件中的命名空間或在設計器程式碼中與之匹配

引用自:https://stackoverflow.com/questions/7674574