I am taking over an old ASP.NET solution that is running in the 4.7.2 framework. My last intimate familiarity with ASP.NET was some years ago, using 2.0. I find there is something I don't understand about this environment, or possibly about Visual Studion 2022. I see that in more recent versions of .NET the codebehind is a partial class rather than a parent class. The controls on the .aspx are not declared in the codebehind file, but simply referred to as if already declared. I take it that means a control's appearance in the .aspx also serves as its declaration. So far, so good. We will eventually upgrade or replace this system, but in the short term I need to add a DropDownList to a page.
I enter this on CourseReport.aspx:
<ASP:DropDownList runat="server" id="ddlCourseSession"></ASP:DropDownList>
But when I refer to ddlCourseSession in the codebehind partial class, VS complains The name 'ddlCourseSession' does not exist in the current context
. I've been digging through the Microsoft doc looking for how to connect the two in this situation, but everything I've found talking about partial classes seems to be about CS classes, rather than web pages or controls. The only thing that seems relevant is a reference to a design file, but there is no file anywhere in the solution that includes the string design
in its name.
I've triple-checked that I have the identifiers spelled and captalized correctly everywhere, and I've searched both files for any clue, without success. I did try declaring
protected DropDownList drpCourseSession;
at the top of the class just to see if it would work old school, but of course there is a null reference exception at run time. Then I tried
DropDownList ddlCourseSession = (DropDownList)Page.FindControl("ddlCourseSession");
but that, too, returns null.
So how does one get the controls from the UI file to be accessible in the codebehind?
I am taking over an old ASP.NET solution that is running in the 4.7.2 framework. My last intimate familiarity with ASP.NET was some years ago, using 2.0. I find there is something I don't understand about this environment, or possibly about Visual Studion 2022. I see that in more recent versions of .NET the codebehind is a partial class rather than a parent class. The controls on the .aspx are not declared in the codebehind file, but simply referred to as if already declared. I take it that means a control's appearance in the .aspx also serves as its declaration. So far, so good. We will eventually upgrade or replace this system, but in the short term I need to add a DropDownList to a page.
I enter this on CourseReport.aspx:
<ASP:DropDownList runat="server" id="ddlCourseSession"></ASP:DropDownList>
But when I refer to ddlCourseSession in the codebehind partial class, VS complains The name 'ddlCourseSession' does not exist in the current context
. I've been digging through the Microsoft doc looking for how to connect the two in this situation, but everything I've found talking about partial classes seems to be about CS classes, rather than web pages or controls. The only thing that seems relevant is a reference to a design file, but there is no file anywhere in the solution that includes the string design
in its name.
I've triple-checked that I have the identifiers spelled and captalized correctly everywhere, and I've searched both files for any clue, without success. I did try declaring
protected DropDownList drpCourseSession;
at the top of the class just to see if it would work old school, but of course there is a null reference exception at run time. Then I tried
DropDownList ddlCourseSession = (DropDownList)Page.FindControl("ddlCourseSession");
but that, too, returns null.
So how does one get the controls from the UI file to be accessible in the codebehind?
Share Improve this question asked Feb 4 at 16:09 Don RDon R 6235 silver badges12 bronze badges 3- It looks like I can go back to the base class codebehind as a workaround, if necessary: stackoverflow/questions/2386184/… – Don R Commented Feb 4 at 17:16
- What happens when you drag it from the Toolbox? This could help updating the code behind file. – VDWWD Commented Feb 5 at 9:20
- I did not realize that newer versions of VS have a visual design tool (it really has been a while). Dragging a new DropDownList onto the palette caused an interface ambiguity error (?), but once I deleted the new one the original error no longer occurs. While this solves my immediate problem, I still wish I understood how these are connected. – Don R Commented Feb 5 at 13:30
3 Answers
Reset to default 1In your case, it seems the designer file missed the declaration of the dropdown. I just copied the snippet :
<ASP:DropDownList runat="server" id="ddlCourseSession"></ASP:DropDownList>
into Default.aspx file and i can refer the dropdown in codebehind file (Default.aspx.cs) without any issue.
In my case i can see the designer file with the name "Default.aspx.designer.cs" in solution explorer, in which the class "_Default" contains the below declartion of the dropdown:
protected global::System.Web.UI.WebControls.DropDownList ddlCourseSession;
Try to find the designer file and add the above declaration will resolve your issue. I've recreated your issue, by deleting the above line in the designer file. Let me know if you need any further help. Hope this helps!
This may be way off base, but we also have a number of old systems still kicking around from the .NET 4.X era and have inherited solutions that "have history" ...
Most of the time we have run into this kind of problem, it was cause by a non-standard page declaration. (someone changed the code file to point elsewhere at some point in time, but didn't clean up the old files)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CourseReport.aspx.cs" Inherits="CourseReport" %>
Was change to something like:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="/code/CourseReport2001.aspx.cs" Inherits="CourseReport" %>
and we find ourselves making changes to the file CourseReport.aspx.cs ... still located in the same directory as the ASPX file, expecting to to work like all the other pages in the solution.
The solution, at least in this case, turned out to be having to place a new control on the Design screen rather than typing it in. I still have no idea what the difference is, or how the control on the UI and the control in the codebehind are connected, since there is no designer file; if I simply copy the modified .aspx and .aspx.cs files, and nothing else, to a different copy of the site, it works.