Jaggu
Jaggu

Reputation: 6428

The name 'xxx' does not exist in current context user control

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 :)

Upvotes: 2

Views: 18144

Answers (10)

Rodney
Rodney

Reputation: 219

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.

Upvotes: 0

Joseph Garrone
Joseph Garrone

Reputation: 1762

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
}

Upvotes: 0

Anand
Anand

Reputation: 31

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

Upvotes: 0

Barbic
Barbic

Reputation: 1

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.

Upvotes: 0

Salman
Salman

Reputation: 1380

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

Upvotes: 0

Dek Dekku
Dek Dekku

Reputation: 1461

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.

Upvotes: 1

Suvendu Shekhar Giri
Suvendu Shekhar Giri

Reputation: 1384

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.

Upvotes: 0

Chris Halcrow
Chris Halcrow

Reputation: 31940

  • 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!

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38200

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

Upvotes: 8

FiveTools
FiveTools

Reputation: 6030

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.

Upvotes: 1

Related Questions