简体   繁体   English

将代码隐藏文件添加到asp.net页面

[英]Add codebehind file to asp.net page

I got a file, let's call it MyPage.aspx. 我有一个文件,我们称它为MyPage.aspx。 It has no codebehind and I need to add a codebehind file to it. 它没有代码隐藏,我需要向其添加代码隐藏文件。 Here's what I've done: 这是我所做的:

  • created MyPage.aspx.cs and included it in the corresponding namespace 创建MyPage.aspx.cs并将其包含在相应的名称空间中
  • Added the following code to the Page tag of MyPage.aspx: AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage" 将以下代码添加到MyPage.aspx的Page标记中:AutoEventWireup =“ true” CodeBehind =“ MyPage.aspx.cs” Inherits =“ MyApp.MyPage”

Still, it doesn't work. 尽管如此,它还是行不通的。 I cannot access runat=server elements from the codebehind. 我无法从背后的代码访问runat = server元素。 Strangely, I also noticed one more thing: 奇怪的是,我还注意到了另一件事:

The definition of the class in MyPage.aspx.cs is as follows: MyPage.aspx.cs中该类的定义如下:

public partial class MyPage : System.Web.UI.Page

Normally, both words MyPage and Page in this line would be green. 通常,此行中的单词MyPage和Page均为绿色。 However, only the word MyPage is green and the word Page is still black. 但是,只有单词MyPage为绿色,单词Page仍为黑色。

I'm kinda stuck with this, any help would be appreciated. 我有点坚持,任何帮助将不胜感激。

in your aspx page you need to regrence the code behind page 在aspx页面中,您需要在页面后面添加代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>

after you do that, you make a new class and name it MyPage.aspx.cs, just make sure that its in the same directory as your aspx page. 完成后,创建一个新类并将其命名为MyPage.aspx.cs,只需确保其与aspx页位于同一目录中即可。

also give the class name inside the code behind _MyPAge 还要在_MyPAge后面的代码中提供类名

it should look like this after you reference your components 引用组件后,它应该看起来像这样

public partial class _MyPage : System.Web.UI.Page

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM