CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Dec 2004
    Posts
    438

    [RESOLVED] Create Code Behind Files Automatically

    Hi. I've just started learning .net with VB and C#. I'm trying to use Code Behind files but the problem I'm having is that I can't create them automatically. I'm using Visual Studio 2008 incidentally. If I go to File > New > File.. and then enter test1.aspx.cs it just creates a HTML file with a Page directive at the top.

    How do I get it to add the Import statements at the top and set up the Partial class (if that's possible)?

    In my book and on the web it would suggest that this just happens.. But no matter what I do it won't do this for some reason.

    Edited I used File > New > File.. > Class

    instead and this seemed to do the trick. It moaned about it not being in the App_Code folder for some reason. Is this the general way of making the code behind file?

    I know that sometimes code behind files are just created for you but that doesn't seem to have happened here.
    Last edited by Nibinaear; January 13th, 2010 at 01:20 PM.

  2. #2
    Join Date
    Dec 2004
    Posts
    438

    Re: Create Code Behind Files Automatically

    I now have a new problem with code behind files. None of the controls inside my aspx file are recognised inside the code behind file.

    Code Behind

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class Test1CodeBehind : System.Web.UI.Page
    {
    	public void Click(object s, EventArgs e)
    	{
        messageLabel.Text = "test comment";
    	}
    }
    Aspx file

    Code:
    <&#37;@ Page Language="C#" CodeFile="test1.aspx.cs" Inherits="Test1CodeBehind" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Untitled Page</title>
    </head>
    <body>
    
    <form runat="server" id="form1">
    
      <asp:Label id="messageLabel" runat="server" />
      
      <asp:Button runat="server" OnClick="Click" Text="run code" />
      
    </form>
    
    </body>
    </html>
    My error is:

    Code:
    Error	2	The name 'messageLabel' does not exist in the current context	C:\asp_sites\test2\App_Code\test1.aspx.cs	17	5	http://localhost:81/
    Edited
    As it turns out, my code behind file was inside the App_Code folder as suggested by Visual studio, this prevented it from running correctly. I spent hours trying to sort this out.
    Last edited by Nibinaear; January 13th, 2010 at 05:02 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured