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

Brian Denomey picture Brian Denomey · Oct 15, 2014 · Viewed 21.5k times · Source

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" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphHead" Runat="Server">
    <title>xxxx</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphBreadCrumb" Runat="Server">
    <div id="breadCrumbFrame"><a href="http://www.org.org" title="xxx">Home</a>  &gt; <a href="/directory" title="xxx">xxx</a></div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cphBody" Runat="Server">
    <h3 class="sectionTitle">&nbsp;xxx&nbsp;</h3>

    <!--//////////// Begin Searchbox Panel ////////////-->
    <asp:Panel ID="pnlSearchbox" Visible="true" runat="server" DefaultButton="lnkSearch">
        <div class="info">
            xxx <a href="http://www.org.org" target="_blank" style="text-decoration: underline">xxx</a>.
        </div>
        <h3 style="padding-bottom: 10px;">xxx:</h3>
        <p>xxx <a href="http://www.org.org/">xxx</a>.</p>
        <br />
        <br />
        <div class="addressLine">
            <asp:Label ID="lblFirstName" CssClass="addressLabel" runat="server" Text="First Name"></asp:Label>
            <asp:TextBox ID="txtFirstName" CssClass="addressEdit" runat="server"></asp:TextBox>
        </div>
        <div class="addressLine">
            <asp:Label ID="lblLastName" CssClass="addressLabel" runat="server" Text="Last Name"></asp:Label>
            <asp:TextBox ID="txtLastName" CssClass="addressEdit" runat="server"></asp:TextBox>
        </div>
        <div class="addressLine">
            <asp:Label ID="lblCity" CssClass="addressLabel" runat="server" Text="City"></asp:Label>
            <asp:TextBox ID="txtCity" CssClass="addressEdit" runat="server"></asp:TextBox>
        </div>
        <div class="addressLine">
            <asp:Label ID="lblDistrict" CssClass="addressLabel" runat="server" Text="District"></asp:Label>
            <asp:DropDownList ID="ddlDistrict" CssClass="addressDropDown" runat="server" DataSourceID="dsDistricts"
                DataTextField="Name" DataValueField="DistrictID"
                AppendDataBoundItems="True">
                <asp:ListItem Value="">All Districts</asp:ListItem>
            </asp:DropDownList>
        </div>
        <div class="addressLine">
            <asp:Label ID="lblSpecialty" CssClass="addressLabel" runat="server" Text="Specialty"></asp:Label>
            <asp:DropDownList ID="ddlSpecialty" CssClass="addressDropDown" runat="server"
                DataSourceID="dsSpecialties" DataTextField="xxx_certification"
                DataValueField="QualificationID" AppendDataBoundItems="True">
                <asp:ListItem Value="">All xxx</asp:ListItem>
            </asp:DropDownList>
        </div>
        <div class="addressLine">
            <div class="addressLabel">&nbsp;</div>
            <asp:LinkButton ID="lnkSearch" CssClass="navButton" runat="server"
                onclick="lnkSearch_Click">Search</asp:LinkButton>
        </div>
    </asp:Panel>
    <asp:LinqDataSource ID="dsDistricts" runat="server"
        ContextTypeName="org.Xrm.XrmDataContext" Select="new (DistrictID, Name)"
        TableName="Districts">
    </asp:LinqDataSource>
    <asp:LinqDataSource ID="dsSpecialties" runat="server"
        ContextTypeName="org.Xrm.XrmDataContext"
        Select="new (QualificationID, xxx_certification)" TableName="Qualifications"
        Where="EducationTypeID == @EducationTypeID">
        <whereparameters>
            <asp:Parameter DefaultValue="000" Name="EducationTypeID" 
                DbType="Guid" />
        </whereparameters>
    </asp:LinqDataSource>

    <!--//////////// Begin Search Results Panel ////////////-->

    <asp:Panel ID="pnlResults" Visible="false" runat="server">
        <div id="ResultsTop" style="margin-bottom: 15px;">
            <div id="ResultsLeft" style="float: left; padding-top: 5px;">
                <h2>Search Results - <span class="BlackResults">
                    <asp:Label ID="lblCount" runat="server" Text=""></asp:Label></span></h2>
            </div>
        </div>
        <br />
        <br />
        <br />
        <asp:LinkButton ID="lnkNewSearch" CssClass="navButtonLarge" runat="server" OnClick="lnkNewSearch_Click">New Search</asp:LinkButton>
        <br />
        <br />
        <div align="center">
            <asp:DataPager ID="DataPager2" runat="server" PagedControlID="lvResults"
                PageSize="25">
                <fields>
                    <asp:NextPreviousPagerField ShowFirstPageButton="True" 
                        ShowNextPageButton="False" ShowPreviousPageButton="True" />
                    <asp:NumericPagerField ButtonCount="10" />
                    <asp:NextPreviousPagerField ShowLastPageButton="True" 
                        ShowNextPageButton="True" ShowPreviousPageButton="False" />
                </fields>
            </asp:DataPager>
        </div>
        <br />
        <asp:ListView ID="lvResults" runat="server" DataSourceID="dsSearchResults">
            <itemtemplate>
                <tr style="">
                    <td class="DirectoryItems">
                        <asp:LinkButton ID="lnkDetails" CommandArgument='<%# Eval("ID") %>' OnCommand="lnkDetails_Click" runat="server"><%# Eval("xxx") %></asp:LinkButton>
                    </td>
                    <td class="DirectoryItems">
                        <asp:Label ID="LocationLabel" runat="server" Text='<%# Eval("Location") %>' />
                    </td>
                    <td class="DirectoryItems">
                        <asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
                    </td>
                </tr>
            </itemtemplate>
            <emptydatatemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </emptydatatemplate>
            <layouttemplate>
                <table runat="server" width="100%">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="border-collapse: collapse;" width="99%">
                                <tr runat="server" style="border-bottom:1px solid #828283; margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px;">
                                    <td runat="server" width="40%">
                                        <p class="DirectoryResultsHeader">xxx</p></td>
                                    <td runat="server" width="30%">
                                        <p class="DirectoryResultsHeader">Location</p></td>
                                    <td runat="server" width="30%">
                                        <p class="DirectoryResultsHeader">Status</p></td>
                                </tr>
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="">
                        </td>
                    </tr>
                </table>
            </layouttemplate>
        </asp:ListView>
        <br />
        <br />
        <div align="center">
            <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvResults"
                PageSize="25">
                <fields>
                    <asp:NextPreviousPagerField ShowFirstPageButton="True" 
                        ShowNextPageButton="False" ShowPreviousPageButton="True" />
                    <asp:NumericPagerField ButtonCount="10" />
                    <asp:NextPreviousPagerField ShowLastPageButton="True" 
                        ShowNextPageButton="True" ShowPreviousPageButton="False" />
                </fields>
            </asp:DataPager>
        </div>
        <asp:SqlDataSource ID="dsSearchResults" runat="server" OnSelected="dsSearchResults_Selected"
            ConnectionString="<%$ ConnectionStrings:MSCRM %>" CancelSelectOnNullParameter="false"
            SelectCommand="
                        SELECT * from sometable
            ">
            <selectparameters>
                    <asp:ControlParameter ControlID="txtFirstName" DbType="String" Name="FirstName" DefaultValue="" ConvertEmptyStringToNull="false" />
                    <asp:ControlParameter ControlID="txtLastName" DbType="String" Name="LastName" DefaultValue="" ConvertEmptyStringToNull="false" />
                    <asp:ControlParameter ControlID="txtCity" DbType="String" Name="City" DefaultValue="" ConvertEmptyStringToNull="false" />
                    <asp:ControlParameter ControlID="ddlDistrict" DbType="Guid" Name="District" ConvertEmptyStringToNull="true" />
                    <asp:ControlParameter ControlID="ddlSpecialty" DbType="Guid" Name="Specialty" ConvertEmptyStringToNull="true" />
                </selectparameters>
        </asp:SqlDataSource>
    </asp:Panel>

    <!--//////////// Begin Details Panel ////////////-->

    <asp:Panel ID="pnlDetails" Visible="false" runat="server">
        <h3>xxx Information</h3>
        <br />
        <asp:Repeater ID="rpDetails" runat="server" DataSourceID="dsDetails">
            <itemtemplate>
                <table border="0">
                    <tr>
                        <td width="125"><b>Given Name:</b></td>
                        <td><%# Eval("FirstName") %></td>
                    </tr>
                    <tr>
                        <td width="125"><b>Surname:</b></td>
                        <td><%# Eval("LastName") %></td>
                    </tr>
                    <tr>
                        <td width="125"><b>Gender:</b></td>
                        <td><%#Eval("Gender") %></td>
                    </tr>
                    <tr>
                        <td width="125"><b>Status:</b></td>
                        <td><%# Eval("Status") %></td>
                    </tr>
                    <asp:Panel ID="pnlSpecialty" runat="server" Visible='<%# Eval("Status").ToString() == "xxx" || Eval("Status").ToString() == "xxx" || Eval("Status").ToString() == "xxx" ? true : false %>'>
                        <tr>
                            <td width="125"><b>xxx:</b></td>
                            <td>
                                <asp:Repeater ID="xxx" runat="server" DataSourceID="xxx">
                                    <ItemTemplate>
                                        <%# Eval("xxx") %><br />
                                    </ItemTemplate>
                                </asp:Repeater>
                            </td>
                        </tr>
                    </asp:Panel>
                    <tr>
                        <td width="125"><b>xxx:</b></td>
                        <td><%# Eval("xxx") %></td>
                    </tr>
                </table>
            </itemtemplate>
        </asp:Repeater>
        <br />
        <br />
        <asp:Repeater ID="rpAddresses" runat="server" DataSourceID="dsAddresses" OnItemDataBound="rpAddresses_DataBound">
            <headertemplate>
                <h3>xxx</h3><br />
            </headertemplate>
            <itemtemplate>
                <table border="0">
                    <tr>
                        <td valign="top" width="125"><b><asp:Label ID="lblAddressType" runat="server" Text='<%# Eval("Type") %>' /></b></td>
                        <td valign="top">
                            <asp:Label ID="lblAddressStreet1" runat="server" Text='<%# Eval("Street1") + "<br />" %>' Visible='<%# Convert.IsDBNull(Eval("Street1")) ? false : true %>' />
                            <asp:Label ID="lblAddressStreet2" runat="server" Text='<%# Eval("Street2") + "<br />" %>' Visible='<%# Convert.IsDBNull(Eval("Street2")) ? false : true %>' />
                            <asp:Label ID="lblAddressStreet3" runat="server" Text='<%# Eval("Street3") + "<br />" %>' Visible='<%# Convert.IsDBNull(Eval("Street3")) ? false : true %>' />
                            <asp:Label ID="lblAddressCity" runat="server" Text='<%# Eval("City") %>' />, <asp:Label ID="lblAddressProvince" runat="server" Text='<%# Eval("Province") %>' />&nbsp;&nbsp;<asp:Label ID="lblAddressPostalCode" runat="server" Text='<%# Eval("PostalCode") %>' /><br />
                            <asp:Label ID="lblAddressCountry" runat="server" Text='<%# Eval("Country") + "<br />" %>' Visible='<%# (Eval("Country", "{0}") == "Canada") ? false : true %>' />
                            <table border="0">
                                <asp:Label ID="lblAddressPhone" runat="server" Text='<%# "<tr><td width=50>Phone:</td><td>" + Eval("Phone") + "</td></tr>" %>' Visible='<%#Convert.IsDBNull(Eval("Phone")) ? false : true %>' />
                                <asp:Label ID="lblAddressFax" runat="server" Text='<%# "<tr><td width=50>Fax:</td><td>" + Eval("Fax") + "</td></tr>" %>' Visible='<%#Convert.IsDBNull(Eval("Fax")) ? false : true %>' />
                                <asp:PlaceHolder ID="cphEmailAddress" runat="server" Visible='<%# Convert.IsDBNull(Eval("Email")) ? false : true %>'><tr><td width="50">Email:</td><td><asp:LinkButton ID="lnkEmailValidate" runat="server" Text='<%# Eval("ShortEmail") %>' OnClick="ShowReCAPTCHA" /></td></tr></asp:PlaceHolder>
                                <asp:PlaceHolder ID="cphEmail" runat="server" Visible="false"><tr><td width="50">Email:</td><td><asp:HyperLink ID="lnkEmail" runat="server" NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' Text='<%# Eval("Email") %>' /></td></tr></asp:PlaceHolder>
                             </table>
                             <asp:PlaceHolder ID="cphCaptcha" runat="server" Visible="false">
                                   <br />
                                    <div class="info">To view the full email address, please type the moving characters in the box below.</div>
                    <nu:NuCaptchaControl ID="nucaptcha" runat="server" ClientKey="LEAP|0|4|TYPE|9|CLIENTKEY|CID|4|9942|KID|4|9884|SKEY|32|bDdiOWgwdjhtNy1MdTRENG43Q1BZdyws" />

                             </asp:PlaceHolder>
                        </td>
                    </tr>
                </table>
                <br />

            </itemtemplate>
        </asp:Repeater>
        <br />
        <br />
        <asp:LinkButton ID="lnkDetailsBack" CssClass="navButtonLarge" runat="server" OnClick="lnkDetailsBack_Click">Back to Search Results</asp:LinkButton>
        <asp:LinkButton ID="lnkDetailsNewSearch" runat="server" CssClass="navButtonLarge" OnClick="lnkNewSearch_Click">New Search</asp:LinkButton>
        <asp:SqlDataSource ID="dsDetails" runat="server"
            ConnectionString="<%$ ConnectionStrings:MSCRM %>"
            SelectCommand="
                        SELECT * from some table
            ">
            <selectparameters>
                    <asp:Parameter DbType="Guid" Name="ContactID" />
                </selectparameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="dsSpecialty" runat="server"
            ConnectionString="<%$ ConnectionStrings:MSCRM %>"
            SelectCommand="
                        SELECT * from some table 
            ">
            <selectparameters>
                    <asp:Parameter DbType="Guid" Name="ContactID" />
                </selectparameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="dsAddresses" runat="server"
            ConnectionString="<%$ ConnectionStrings:xxx %>"
            SelectCommand="
                        SELECT * from some table 
            ">
            <selectparameters>
                    <asp:Parameter DbType="Guid" Name="ContactID" />
                </selectparameters>
        </asp:SqlDataSource>
    </asp:Panel>

    <!--//////////// Begin Data Sources ////////////-->

</asp:Content>

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

Brian Denomey picture Brian Denomey · Oct 15, 2014

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.