vb.net 错误:“已被声明为受保护的 WithEvents”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15464167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:50:28  来源:igfitidea点击:

Error: "is already declared as protected WithEvents"

asp.netvb.net

提问by Kyo Yamagata

Web Controls

网页控制

<asp:TextBox id="txtUsername" runat="server" CssClass="input-block-level" placeholder="Username" type="text" required></asp:TextBox>
<asp:TextBox id="txtPassword" runat="server" CssClass="input-block-level" placeholder="Password" type="password" required></asp:TextBox>

Code Behind

背后的代码

Inherits System.Web.UI.MasterPage
Protected WithEvents txtUsername As System.Web.UI.WebControls.Literal
Protected WithEvents txtPassword As System.Web.UI.WebControls.Literal

Error:

错误:

Parse Error: 'txtPassword' is already declared as 'Protected WithEvents txtPassword As System.Web.UI.Webcontrols.TextBox in' this class
'txtUsername' is already declared as 'Protected WithEvents txtUsername As System.Web.UI.Webcontrols.TextBox in' this class

解析错误:“txtPassword”已声明为“Protected WithEvents txtPassword As System.Web.UI.Webcontrols.TextBox in”此类
“txtUsername”已声明为“Protected WithEvents txtUsername As System.Web.UI.Webcontrols.TextBox in” ' 这节课

What could be the cause of the error?

错误的原因可能是什么?

回答by chakeda

I know this is 3 years late, but I've just solved this problem and I'll share my solution to help out anyone stumbling here.

我知道这已经晚了 3 年,但我刚刚解决了这个问题,我将分享我的解决方案来帮助任何在这里绊倒的人。

I got this error when I commented out a control, worked on other parts of the project, then uncommented it.

当我注释掉一个控件,处理项目的其他部分,然后取消注释时,我得到了这个错误。

Simply change the ID of your control in your aspx and aspx.vb and the error will be gone.

只需在 aspx 和 aspx.vb 中更改控件的 ID,错误就会消失。

<!-- changed from txt to text -->
<asp:TextBox id="textUsername" runat="server" CssClass="input-block-level" placeholder="Username" type="text" required></asp:TextBox>
<asp:TextBox id="textPassword" runat="server" CssClass="input-block-level" placeholder="Password" type="password" required></asp:TextBox>

回答by Theophilus

You may have two or more Web Forms with the same class name.

您可能有两个或多个具有相同类名的 Web 窗体。

Contents of somepage.aspxand anotherpage.aspx:

内容SomePage.aspx页面anotherpage.aspx

<%@ Page ... Inherits="somepage" %>
...

Contents of somepage.aspx.vband anotherpage.aspx.vb:

内容somepage.aspx.vbanotherpage.aspx.vb

Partial Class somepage
    ...
End Class

In the sample code above, changing the Inheritsattribute and class name in the anotherpage.*files to anotherpagefixes the problem. The code-behind looks a little different if you are using C#, but it's the same idea.

在上面的示例代码中,更改anotherpage.*文件中的Inherits属性和类名以解决问题。如果您使用 C#,代码隐藏看起来有点不同,但它的想法是相同的。anotherpage

回答by OneFineDay

When you add controls to the aspx page your controls are added to the partial class of the same name as your code behind file. If you add then again to the code file you will get the error.

当您将控件添加到 aspx 页面时,您的控件将添加到与代码隐藏文件同名的部分类中。如果您再次将 then 添加到代码文件中,您将收到错误消息。