【问题标题】:... (are you missing a using directive or an assembly reference?)...(您是否缺少 using 指令或程序集引用?)
【发布时间】:2016-09-01 05:12:56
【问题描述】:

请帮助我解决以下问题。我很难过。我有一个名为“one”的文件夹,里面只有 2 个文件。第一个是DemostrationOne.aspx:

<%@ Page Language="C#" Title="Demo" CodeBehind="DemonstrationOne.aspx.cs" AutoEventWireup="true" Async="true" %>
<head runat="server">
<title><%: Title %></title>
</head>
<body>
<form id="sayHello" runat="server">
<div>
<asp:Button ID="doSomething" OnClick="DemonstrateMe" Text="Show Label" runat="server"></asp:Button>
<asp:PlaceHolder runat="server" ID="showLiteral" Visible="false">
<p><asp:Literal ID="HelloWorldLabel" runat="server"></asp:Literal></p></asp:PlaceHolder>
</div>
</form>
</body>

第二个当然是DemonstrationOne.aspx.cs:

using System;
using System.Web.UI;
namespace OneDemonstration
{
    public partial class DemonstrateMe
    {
        protected global::System.Web.UI.WebControls.Literal HelloWorldLabel;
        protected global::System.Web.UI.WebControls.PlaceHolder showLiteral;
    }
    public partial class DemonstrateMe : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {  }
        public void sHowHelloWorldLabel(object sender, EventArgs e)
        {
            showLiteral.Visible = true;
            HelloWorldLabel.Text = "Hello World!";
        }
    }
}

我去visual studio并点击文件>打开>网站(我不从向导自动生成它)然后点击运行。这给了我错误信息:

“ASP.demonstrationone_aspx”不包含“DemonstrateMe”的定义,并且没有扩展方法“DemonstrateMe”接受 可以找到“ASP.demonstrationone_aspx”类型的第一个参数(是 您缺少 using 指令或程序集引用?)

我想指出,我是一个初学者,甚至在 aspx Web 表单中显示“Hello World”标签时遇到问题(没有自动生成的向导)。我认为它只有由 Visual Studio 生成才有效?

请帮忙。

【问题讨论】:

  • DemonstrateMe 是一个类,而不是一个方法。 OnClick 需要指向一个方法。
  • 旁白:让你的文件与你的类名匹配会更清楚。并且您的处理程序目前指向一个类而不是该类中的方法,因此存在问题。
  • 嗨@ManoDestra 我有文件名匹配类名但我很困惑所以我改变了它以确保像“demo”这样的词不是c#语言的一部分。
  • 您可以改为重构类,这会自动为您更改文件名和类名。而且“demo”不是C#的保留字。你可以将它用于测试类,没问题:)
  • @Rookie:您可能应该从学习教程开始。

标签: c# asp.net


【解决方案1】:

好的!我花了很长时间重新阅读我做错了什么,显然“网络应用程序”和“网站”是不同的东西!谁 - 达 thunk !所以这是解决方案。

首先添加一个 Web.config 文件,其中包含以下代码:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
  </system.web>
</configuration>

在主.aspx文件中去掉CodeBehind,添加CodeFileInherits,分别填写。就我而言:

<%@ Page Language="C#" Title="Demo" CodeFile="DemonstrationOne.aspx.cs" Inherits="DemonstrationOne.Demo" AutoEventWireup="true" Async="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><%: Title %></title>
</head>
<body>
<form id="sayHello" runat="server">
<asp:Button runat="server" OnClick="ShowLabel" Text="Show Label" /><br/>
<asp:PlaceHolder runat="server" ID="showLiteral" Visible="false">
<p><asp:Literal ID="HelloWorldLabel" runat="server"></asp:Literal></p>
</asp:PlaceHolder>
</form>
</body>
</html>

最后但并非最不重要的一点是添加一些 using 指令并删除多余的变量声明。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace DemonstrationOne
{
    public partial class Demo : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { }

        protected void ShowLabel(object sender, EventArgs e)
        {
            showLiteral.Visible = true;
            HelloWorldLabel.Text = "Work damnitt aarrgghh!!";
        }
    }
}

应该可以。现在,去买一个新键盘。您当前的键盘可能因前额按钮过多而出现粘键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多