The name 'xxx' does not exist in current c

2020-07-27 04:21发布

问题:

I have this statemen at the beginning:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucCreditCard.ascx.cs"
    Inherits="UserControls_Common_ucCreditCard" %>

in ucCreditCard.ascx.cs I have this:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
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 UserControls_User_ucCreditCard : System.Web.UI.UserControl
{
//
}

When I try to reference any of the simple control like Textbox etc, I always get control 'xxx' does not exist in current content. What can be the problem?

Thanks in advance :)

回答1:

Hope You are missing the designer file, as these errors mostly occur only in such cases, Right click and convert to web application. should do the trick.

Also do check that its the same as the partial class defined in your .cs file



回答2:

You must have changed the name of your file but did not update the name of your class:

You have:

public partial class UserControls_User_ucCreditCard : System.Web.UI.UserControl
{
//
}

should be

public partial class UserControls_Common_ucCreditCard : System.Web.UI.UserControl
{
//
}

as indicated in the .aspx portion of your code.



回答3:

Same problem here, but with a User Control. Now solved.

In my case, the issue was the designer file being generated in a different namespace than the main one rather than just a missing file. Don't ask me how it happened.



回答4:

  • This issue for me was caused by another error in the application
  • I tried rebuilding the solution and was getting only this error and no others
  • So I rebuilt just the project with the problem
  • Another error appeared
  • I fixed this - turns out it was blocking correct compilation of the user control
  • Now works fine!


回答5:

There is another situation when you get this error. It is when multiple aspx pages refers to the same code page. This generally happens when you have made a copy of the page and then changed the page name but forgot to change the inherits tag.

To find out this just search the text inside the inherits tag, in the whole project and if there is more than one occurrence of the text in aspx pages then just do the modification accordingly.



回答6:

Using different form and user control name will solve your problem.



回答7:

Here the solution. You missing to declare the project.

using System.Xml.Linq;
namespace YouProjectName
{
public partial class UserControls_Common_ucCreditCard : System.Web.UI.UserControl
{
//
}
} // end of namespace.


回答8:

select "Convert to web application" it will create new .aspx.designer.cs file. Then Build would resolve the problem



回答9:

To illustrate what V4Vendetta sayed ther is an exemple inyour case with a dropDownList.

UserControls_Common_ucCreditCard.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucCreditCard.ascx.cs"
    Inherits="UserControls_Common_ucCreditCard" %>

//Exemple with a Drop down list
<asp:DropDownList ID="drpMain" runat="server" Width="200px" Visible="true">

ucCreditCard.ascx.designer.cs

    //Namespace must be the same in ucCreditCard.ascx.designer.cs and in ucCreditCard.ascx.cs
namespace NameOfYourProject {
                        public partial class UserControls_User_ucCreditCard {

                            protected global::System.Web.UI.WebControls.DropDownList drpMain;
                    }
}

ucCreditCard.ascx.cs

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
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;


namespace NameOfYourProject
{
public partial class UserControls_User_ucCreditCard : System.Web.UI.UserControl
{
// You can refer drpMain
}


回答10:

I just ran into this after I copied/pasted an ascx user control for which I'm going to make a breaking change and it needed to be "versioned". I had adjusted the class names but the references to page controls on the original ascx code behind still barfed until I made sure the full namespace.classname was being used in the ascx page "Inherits" property.



标签: c# asp.net