Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, April 20, 2016

The name 'control' does not exist in the current context

The name 'control' does not exist in the current context


I am having trouble with an error on a site I am working on that I've inherited, upon grabbing the source from the repository and building, then clearing out a few reference errors, I am still getting over 1000 instances of this error:

The name 'pnlDetails' does not exist in the current context (replace 'panelDetails' with any of my control names).

what this would seem to indicate is that the controls referenced server side are not declared on the page, or don't have runat=server in them, but in fact they do. it could also be a problem of the inherits attribute not matching, but it does. I have searched stackoverflow and seen this question before, though after trying some of the solutions mentioned, they did not help. I do not have any designer files for my pages. Below are some snippets of code from the aspx and aspx.cs pages. (some information redacted to protect client privacy)

My question is, why can't i reference my controls on the server side? 'paneldetails', 'rpAddresses' etc.?

default.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/org.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="directory_Default" %>    <%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=xxxx"      Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>  <%@ Register TagPrefix="nu" Namespace="Leap.NuCaptcha" Assembly="leapmarketing" %>          xxxx                    

 xxx 

xxx xxx.

xxx:

xxx xxx.



All Districts
All xxx
 
Search

Search Results -




New Search


<%# Eval("xxx") %>
No data was returned.

xxx

Location

Status



xxx Information


Given Name: <%# Eval("FirstName") %>
Surname: <%# Eval("LastName") %>
Gender: <%#Eval("Gender") %>
Status: <%# Eval("Status") %>
xxx: <%# Eval("xxx") %>
xxx: <%# Eval("xxx") %>


xxx


,   
Email:
Email:

To view the full email address, please type the moving characters in the box below.



Back to Search Results New Search

default.aspx.cs

using System;  using System.Data;  using System.Collections.Generic;  using System.Linq;  using System.Web;  using System.Web.UI;  using System.Web.UI.WebControls;    public partial class directory_Default : System.Web.UI.Page  {      protected void Page_Load(object sender, EventArgs e)      {          if (Page.IsPostBack && pnlDetails.Visible == true && rpAddresses.Items.Count > 0 && ((PlaceHolder)rpAddresses.Items[0].FindControl("cphCaptcha")).Visible == true)          {              Page.Validate();              if (Page.IsValid)              {                  ((PlaceHolder)rpAddresses.Items[0].FindControl("cphCaptcha")).Visible = false;                  ((PlaceHolder)rpAddresses.Items[0].FindControl("cphEmailAddress")).Visible = false;                  ((PlaceHolder)rpAddresses.Items[0].FindControl("cphEmail")).Visible = true;              }          }      }        protected void rpAddresses_DataBound(object sender, EventArgs e)      {      }        protected void lnkSearch_Click(object sender, EventArgs e)      {          //lvResults.DataBind();          DataPager1.SetPageProperties(0, DataPager1.PageSize, true);          ShowResults();      }        protected void ShowResults()      {          pnlSearchbox.Visible = false;          pnlResults.Visible = true;          pnlDetails.Visible = false;      }        protected void lnkNewSearch_Click(object sender, EventArgs e)      {          pnlSearchbox.Visible = true;          pnlResults.Visible = false;          pnlDetails.Visible = false;      }        protected void lnkDetails_Click(object sender, CommandEventArgs e)      {          dsDetails.SelectParameters["ContactID"].DefaultValue = e.CommandArgument.ToString();          dsSpecialty.SelectParameters["ContactID"].DefaultValue = e.CommandArgument.ToString();          dsAddresses.SelectParameters["ContactID"].DefaultValue = e.CommandArgument.ToString();          rpAddresses.DataBind();          if (rpAddresses.Items.Count == 0)          {              rpAddresses.Visible = false;          }          else          {              rpAddresses.Visible = true;          }          pnlSearchbox.Visible = false;          pnlResults.Visible = false;          pnlDetails.Visible = true;      }        protected void lnkDetailsBack_Click(object sender, EventArgs e)      {          ShowResults();      }        protected void dsSearchResults_Selected(object sender, SqlDataSourceStatusEventArgs e)      {          lblCount.Text = String.Format("{0} Dentists found", e.AffectedRows);      }        protected void ShowReCAPTCHA(object sender, EventArgs e)      {          ((PlaceHolder)rpAddresses.Items[0].FindControl("cphCaptcha")).Visible = true;      }  }  

Answer by scartag for The name 'control' does not exist in the current context


You may want to check the build action of the c# files in the property window in visual studio and make sure its set to Compile

Answer by Mark Fitzpatrick for The name 'control' does not exist in the current context


Check to see if this is a web application project or a website project. A website project would use the CodeFile attribute of the @Page directive, but a web application project wouldn't. If it's trying to compile as a web application project, you would receive these errors because a web application project expects a designer file in addition to the .aspx and .aspx.cs files. The designer contains all the control definitions which in turn which allows you to reference them properly in codebehind (web application uses CodeBehind and not CodeFile attribute as well).

Answer by manthan davda for The name 'control' does not exist in the current context


Right click on the page where you are getting the error and click on convert to web application... That will solve your error.

Answer by Arushi Agrawal for The name 'control' does not exist in the current context


Couple of things to be checked here: 1. Every control which you wish to access in aspx.cs should have runat="server" 2. Build your solution after adding the above tag 3. If you wish to access the control defined inside the item template of repeater you can do so only inside repeater events for by putting a loop reading all items of repeater

Answer by Brian Denomey for The name 'control' does not exist in the current context


Thanks for the answers people, I did try some of these things.

  1. Every control which you wish to access in aspx.cs should have runat="server"

they do

  1. Build your solution after adding the above tag

  2. If you wish to access the control defined inside the item template of repeater you can do so only inside repeater events for by putting a loop reading all items of repeater

they are

Right click on the page where you are getting the error and click on convert to web application... That will solve your error.

no such option in my IDE, besides I think it would be the whole project, not a page that I convert.

Check to see if this is a web application project or a website project. A website project would use the CodeFile attribute of the @Page directive, but a web application project wouldn't. If it's trying to compile as a web application project, you would receive these errors because a web application project expects a designer file in addition to the .aspx and .aspx.cs files. The designer contains all the control definitions which in turn which allows you to reference them properly in codebehind (web application uses CodeBehind and not CodeFile attribute as well).

apparently then my project is a website, not a web application, as it includes the codefile attribute. also, I don't have designer files, so this makes sense.

Turns out I am trying to edit a solution developed in VS2008, using VS2013, and a lot has changed since then. While the issue was not resolved here, the fix was to create a new VS2013 solution and import the files, it's all working as intended now, thank you.

Answer by Durgesh Pandey for The name 'control' does not exist in the current context


exclude or delete any other pages that reference the same code-behind file, for example an older page that you copied and pasted.

Answer by Gudlaugsson for The name 'control' does not exist in the current context


I had this error but had just made a silly mistake. I wanted to retain the original code while I made some big changes so I copied and pasted one of the .cs files. Well, both files had the same class name. The two identical namespaces (understandably) confused the compiler. Removing the "backup" fixed it. I could have just changed the namespace, too.

Just sharing in case someone is as bone-headed as I.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.