开发者

Aspx page doesn't compile when page is copied from another project

I often have an issue with compile errors when I copy over an aspx page and code behind file from another project, it seems to work when I create another page and the content manually but if I just copy the files it won't. I have checked to make sure namespaces and references are ok but still doesn't make any sense. The error I get is:

Error The name 'txtUser' does not exist in the current context

I just wanted to understand why.

For e.g. I copied over the following:

ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Login" %>

<!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 runat="server">
    <title>Login</title>
</head>
<body>
    <form id="form1" runat="server">
  开发者_如何学Go  <table align="center" width="300px" style="margin-top: 200px;">
        <tr>
            <td>
                    <legend>Login</legend>
                    <table style="width: 30%;" align="center">
                        <tr>
                            <td>
                                <asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                    </table>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

ASPX.cs:

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

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUser.Text = "text";
    }
}


This is probably because the designer file is missing for the page. If it is, you can create it manually by right-clicking the page in the solution explorer and selecting 'Convert to Web Application'.


Are you copying the login.aspx.designer.cs. This file contains a partial class that holds the definition of the controls on the .aspx page.

e.g. for txtUser you'd have the declaration in the .designer.cs file.


One thing to check is if your new project has the same Project References as the old one.

If something is missing there (or indeed in your web.config references), VS can skip generating the designer.cs file altogether, resulting in the error you mention.


As far I look to u r posted questions and the code u copy :

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

You get this error because in u r designer page there is no textbox control so it throws this error : "Error The name 'txtUser' does not exist in the current context"

For more help can you paste the exact code u r coping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜