【问题标题】:aspx file does not contain a definition for function, definition in connected aspx.cs fileaspx 文件不包含函数定义,连接的 aspx.cs 文件中的定义
【发布时间】:2013-05-08 23:11:34
【问题描述】:

Andrei 给出的答案(在他的 cmets 中为我解决了这个问题的部分)

试图点击按钮,但我得到了

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

我的代码:

索引.aspx:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" AutoEventWireup="true" CodeBehind="Index.aspx.cs" %>
*THERE IS A COMMENT ABOUT THE INHERIT NEAR THE END OF MY POST*
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewData["Message"] %></h2>
    <table id="productTable">

    </table>
    <asp:Label ID="testMessage" runat="server" Text="fail"></asp:Label>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>

    <asp:Button ID="btnCreateOrder" runat="server" Text="Create Order" onclick="btnCreateOrder_Click" />

</asp:Content>

索引.aspx.cs:

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

namespace MyProject.Views.Home
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
    {


        System.Diagnostics.Debug.Write("Testing");
        XmlTextReader reader = new XmlTextReader("data/products.xml");

        while (reader.Read())
        {

            System.Diagnostics.Debug.Write(reader.Name);

        }


        int numRows = 21;
        int numCells = 4;
        for (int rowNum = 0; rowNum < numRows; rowNum++)
        {
            TableCell cel = new TableCell();

        }
    }

    public void btnCreateOrder_Click(object sender, EventArgs e)
    {
       System.Diagnostics.Debug.Write("Testing");
    }

}

在一个半相关的说明中,我也无法让 Page_Load 运行......好吧,页面加载。 (因此调试“测试”)所以如果你知道那里出了什么问题,那就太棒了:)

继承评论 有人建议我将继承更改为 MyProject.Views.Home.Index 但是我需要 System.Web.Mvc.ViewPage 来处理 MVC 布局,没有它我得到: System.InvalidOperationException: The view at '~/ Views/Home/Index.aspx' 必须派生自 ViewPage、ViewPage、ViewUserControl 或 ViewUserControl。 所以......是的,除非我应该把我的函数放在其他地方,比如控制器文件中(我是 Visual Studio 的新手),不知道该怎么做:/

我已经看到更改继承可以修复它,但如果我将它设置为正确的文件指令,它会破坏它(MyProject.Views.Home 会正确)它返回“无法加载类型'MyProject.Views.Home ’。”所以不确定那里发生了什么。除此之外,是的,它们在同一个文件夹中,是的,它们已附加,显然它已设置为其代码隐藏。

提前致谢!

PS 如果某些括号是错误的,那只是因为我搞砸了格式,不要在这里发帖,永远,对不起!

【问题讨论】:

  • MVC 和 Web 表单是完全不同的鱼缸。 MVC 在页面后面使用控制器和 Web 窗体用户代码。我会选择其中一个

标签: c# asp.net onclick code-behind


【解决方案1】:

页面的Inherits 属性中的值有误。实际的代码隐藏类应该在那里:

Inherits="MyProject.Views.Home.Index"

【讨论】:

  • 这里的问题是我必须从 System.Web.Mvc.ViewPage 继承,因为 MVC 布局,从我的研究来看,没有办法从两个来源继承,我会改为做我的 .cs其他地方的操作(比如在 HomeController.cs 文件中,System.Web.Mvc.ViewPage 用于访问)?
  • @user2172534,您可以尝试继承 Index 页面的代码隐藏类,而不是从 System.Web.UI.Page 而是从 System.Web.Mvc.ViewPage。作为页面属性Inherits的含义略有不同,见the description
  • @user2172534,顺便说一下,Inherits 的正确值不是您在编辑中建议的命名空间。它是一个类,所以值应该是MyProject.Views.Home.Index
  • 我得去学习所有这些继承的东西哈哈,谢谢你的指导和帮助!
【解决方案2】:

您的 Index.aspx 页面似乎没有连接到使用您的 CodeBehind 文件中使用的相同类。

尝试将 Index.aspx 中的 Inherits 属性从 System.Web.Mvc.ViewPage 更改为 MyProject.Views.Home.Index

【讨论】:

  • 谢谢,但不幸的是这不起作用,因为这是 MVC 格式,所以也许我应该把函数放在其他地方?请参阅我的帖子中标记为 INHERITANCE COMMENT 的部分,了解更多信息:)
【解决方案3】:

您的页面在继承 System.Web.Mvc.ViewPage 时应该是 MyProject.Views.Home.Index

将 .aspx 页面上的整个顶行更改为

<%@ Page Language="C#" 
MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="MyProject.Views.Home.Index" 
AutoEventWireup="true" 
CodeBehind="Index.aspx.cs" %>

【讨论】:

    猜你喜欢
    • 2020-11-23
    • 1970-01-01
    • 2012-04-21
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多