...(你错过了使用指令或程序集引用吗?)

时间:2016-09-01 05:12:56

标签: c# asp.net

请帮我解决以下问题。我很难过。我有一个名为“one”的文件夹,里面只有2个文件。第一个是DemonstrationOne.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并单击File&gt;打开&gt; Web站点(我不从向导自动生成它)并单击运行。这给了我错误信息:

  

'ASP.demonstrationone_aspx'不包含'DemonstrateMe'的定义,也没有扩展方法'DemonstrateMe'接受a   可以找到类型'ASP.demonstrationone_aspx'的第一个参数(是   你错过了使用指令或程序集引用?)

我想请注意,我是初学者,甚至在aspx web表单中显示“Hello World”标签(没有自动生成的向导)也存在问题。我认为它只有在Visual Studio生成时才有效吗?

请帮忙。

1 个答案:

答案 0 :(得分:2)

OKAY!我花了很长时间重新阅读我做错了什么,显然是#34; web application&#34;和#34;网站&#34;是不同的东西!哇哇哇哇!所以这就是解决方案。

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

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

删除CodeBehind并在主.aspx文件中添加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!!";
        }
    }
}

那应该有用。现在,去买一个新的键盘。你的当前键盘可能有额外的按钮,因为额头上的按钮太多了。

相关问题