【问题标题】:Invalid token ';' in class, struct, or interface member declaration令牌无效 ';'在类、结构或接口成员声明中
【发布时间】:2015-10-08 21:39:09
【问题描述】:

我正在尝试使用 Web 控件在 ASP.NET 网络表单中创建用户登录表单。编译代码时,我在设计器类中收到此错误。登录表单的代码如下:

<form class="cd-form">
    <p class="fieldset">
        <label class="image-replace cd-email" for="signin-email">Username</label>
        <asp:TextBox class="full-width has-padding has-border" ID="signin-email" type="email" placeholder="E-mail" runat="server"></asp:TextBox>
        <span class="cd-error-message">Error message here!</span>
    </p>

    <p class="fieldset">
        <label class="image-replace cd-password" for="signin-password">Password</label>
        <asp:TextBox class="full-width has-padding has-border" ID="signin-password" type="text" placeholder="Password" runat="server"></asp:TextBox>
        <a href="#0" class="hide-password">Hide</a>
        <span class="cd-error-message">Error message here!</span>
    </p>

    <p class="fieldset">
        <input type="checkbox" id="remember-me" checked>
        <label for="remember-me">Remember me</label>
    </p>

    <p class="fieldset">
      <asp:Button ID="btn_login" runat="server" value="Login" OnClick="btn_login_Click1" />
    </p>
</form>

为什么会出现这个错误?

编辑:

这是设计器代码

public partial class main {

        /// <summary>
        /// signin-email control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox signin-email;

        /// <summary>
        /// signin-password control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox signin-password;

        /// <summary>
        /// btn_login control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button btn_login;
    }

【问题讨论】:

  • 能否添加带有错误和错误信息的截图?
  • 你能把designer.cs文件的相关部分也贴出来吗?
  • 从您的 ID 属性中删除 - - 它们应该是常规的 .net 标识符(不允许使用破折号)。
  • @AhmedMujtaba 您最初的问题是缺少设计器的相关代码,这使得诊断变得困难。有些用户没有耐心等待您编辑和更正问题,或者根本不相信您会这样做。这是否是合理的行为是另一回事。
  • 这个错误是否微不足道也没关系。并非每个开发人员都从内存中了解完整的语言规范。我当然不知道。这是一个完全有效的问题

标签: c# asp.net webforms


【解决方案1】:

在 C# 中不允许在变量名中使用破折号。例如,您可以通过将signin-email 指定为ID 来为您的asp:TextBox 执行此操作。自动生成的代码将在 C# 代码中使用这些值。

重命名字段以使用下划线,而不是破折号。

【讨论】:

  • 这个有趣的为什么设计生成器不能以某种方式逃避这个:-)
  • 变量名冲突。
  • 可能会发生名称冲突并且使用标准名称
  • 我不这么认为。样本? (或者也许我不太了解你)
  • 例如,当两个元素具有相同的 id 时。在第一条评论中,我的意思是设计师可以将减号字符更改为另一个符号,看起来是减号,但它是标识符的有效符号。来自特殊 unicode 符号的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
相关资源
最近更新 更多