1

I got a file, let's call it 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
  • Added the following code to the Page tag of MyPage.aspx: AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage"

Still, it doesn't work. I cannot access runat=server elements from the codebehind. Strangely, I also noticed one more thing:

The definition of the class in MyPage.aspx.cs is as follows:

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

Normally, both words MyPage and Page in this line would be green. However, only the word MyPage is green and the word Page is still black.

I'm kinda stuck with this, any help would be appreciated.

4
  • your project compile? Commented Apr 15, 2014 at 6:46
  • After adding Code Behind file, RightClick on .aspx -> Select "Convert To Web Application" option. Commented Apr 15, 2014 at 6:46
  • 1
    no, it doesnt compile. I don't see the Convert... button Commented Apr 15, 2014 at 6:49
  • You mentioned word Page is black. I suspect that references are not working properly. Try removing references and re-adding them. As suggestion, try to build file, close project and reopen it. It should work fine then. Commented Apr 15, 2014 at 6:50

1 Answer 1

1

in your aspx page you need to regrence the code behind page

<%@ 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.

also give the class name inside the code behind _MyPAge

it should look like this after you reference your components

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

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.