gpt4 book ai didi

c# - aspx 文件不包含函数定义,定义在连接的 aspx.cs 文件中

转载 作者:行者123 更新时间:2023-11-30 20:57:54 27 4
gpt4 key购买 nike

我正在尝试点击按钮,但我得到了

'ASP.views_home_index_aspx' does not contain a definition for'btnCreateOrder_Click' and no extension method 'btnCreateOrder_Click'accepting a first argument of type 'ASP.views_home_index_aspx' couldbe found (are you missing a using directive or an assembly reference?)

我的代码:

索引.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' must derive from ViewPage,ViewPage, ViewUserControl, or ViewUserControl.

所以...是的,除非我应该将我的函数放在其他地方,比如在 Controller 文件中(我是 Visual Studio 的新手)不知道该怎么做。

我已经看到更改继承可以修复它,但如果我将它设置为正确的文件指令,它会破坏它(MyProject.Views.Home 会正确)它返回

"Could not load type 'MyProject.Views.Home'."

所以不确定那里发生了什么。除此之外,是的,它们在同一个文件夹中,是的,它们是附加的,显然它已设置为其代码隐藏。

最佳答案

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

Inherits="MyProject.Views.Home.Index"

关于c# - aspx 文件不包含函数定义,定义在连接的 aspx.cs 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16443714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com