Code behind file not recognizing controls in *.ascx
Asked Answered
A

6

9

I have a QuestionControl.ascx and a QuestionControl.ascx.cs code behind file I copied to a new project. When I build the project any references in the code behind file to controls declared in the ascx gives me this error:

'QuestionControl' does not contain a definition for 'rdbtnlstQuestion1' and no extension method 'rdbtnlstQuestion1' accepting a first argument of type 'QuestionControl' could be found (are you missing a using directive or an assembly reference?)

This is at the top of my *.ascx:

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

I've also tried CodeBehind:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="QuestionControl.ascx.cs" Inherits="QuestionControl" %>

This is the top of my class in the codebehind file, it is not contained in a namespace:

public partial class QuestionControl : System.Web.UI.UserControl
{
Addlebrained answered 21/5, 2010 at 15:13 Comment(3)
If you add another blank ascx with another control on it, does that control fail to compile as well?Tyndale
Which version of the .NET framwork is this? And, just to be sure, is this MVC or WebForms?Actualize
Did you check the namespaces?Horsewoman
A
14

Try deleting your designer file. VS.NET should recreate it for you when you open the ascx file. I've had problems like this in the past where the designer gets out-of-sync for some reason, and deleting it usually fixes the problem.

Amberjack answered 21/5, 2010 at 15:20 Comment(5)
I think you are right. There is no .designer file, and I think this is the problem since it would contain declarations of the controls that are in the ascx. But what I find strange is in the project where I've used the user control before, it worked fine without a designer file. I have tried removing and readding the user control, and the designer file is not created. I have also tried manually creating the *ascx.designer.cs file, but the file doesn't get associated with the user control under it's node, and I still get the errors.Addlebrained
What version of VS are you using? And what type of project (website, webapp, etc)?Amberjack
Ok, I right clicked the *.ascx file and chose "Convert to Web Application" and it generated the designer file and associated it under the ascx file. What I am wondering now is what tells Visual Studio that the designer file is associated with a particular ascx file, because when I manually created it, it didn't want to use that file. Surely there is some markup somewhere that says to VS "hey this is my designer file, use it".Addlebrained
ASP.NET Web Application in VS 2008Addlebrained
For when your designer file does not regenerate: #45825Cutanddried
K
5

Another solution is to:

  • open your .ascx page in design view
  • right click anywhere on the page and select Refresh

(.ascx.designer.cs file may need to to be closed while doing refresh for this to work)

Kappa answered 2/2, 2012 at 11:48 Comment(0)
R
3

In VS2017 there is no option 'Convert to Web Application' in the context menu of the .ascx file. Instead you need to select to .ascx file then click on 'Project' in the upper menu and select 'Convert to Web Application' (which is all the way down in the Project menu.

React answered 21/5, 2019 at 5:40 Comment(0)
F
1

What worked form me was listed on another SO answer and I can't find it so I'm repeating it here.

Try deleting your "ReflectedSchemas" folder in

C:\Users\YOURUSENAME\AppData\Roaming\Microsoft\VisualStudio\15.0_079f391b\ReflectedSchemas

This worked for me. Sometimes I get the "unrecognized" errors, and I delete this folder again. Many thanks to the OP. Been driving me crazy for years. Now I have no "squiggles" in the HTML and no "red bars" in the code behind (aspx.cs)

Factional answered 9/2, 2018 at 15:27 Comment(0)
B
0

I had problems for example creating a dropdownlist inside a gridview. What I did is creating the ddl outside of the gv until the desinger.cs recognized it and afterwards moved the control inside the gv... hope this helps

Buenabuenaventura answered 19/12, 2013 at 17:39 Comment(0)
A
0

I was having the same issue, the code-behind didn't recognize the controls on the .aspx page. I'm using VS 2012. I right-clicked the project; clicked on Convert to Web Application; and it added all the designer files that weren't there at all before. I rebuilt everything and it's good now.

Abraxas answered 22/12, 2014 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.