asp:Panel runat=“server” not available in C# .cs c

2019-07-21 21:25发布

问题:

My ascx page:

<%@ Control Language="c#"  Inherits="FSB.Layouts.Short_Home_Loan_Application" CodeBehind="Short Home Loan Application.ascx.cs" %>    
<div class="form">
<form action="" method="post" rel="shortForm">
    <div class="section">        
         <asp:panel id="shortForm_wrapper" runat="server">
            this is the first panel
         </asp:panel>
         <asp:Panel runat="server" ID="thankYouWrapper" >this is the second panel</asp:Panel>
    </div>     
</form>
</div>

<sc:sublayout runat="server" path="~/layouts/FSB/Document Checklist Lightbox.ascx" id="checklist" />

My CS Page:

using System;
using Sitecore.Data.Items;
using Sitecore.Links;
using Sitecore.Data.Fields;
using Sitecore.Web.UI.WebControls;
using System.Net;
using FSB.Helpers;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace FSB.Layouts {

    public partial class Short_Home_Loan_Application : System.Web.UI.UserControl {    

        protected void Page_Load(object sender, EventArgs e) {
            thankYouWrapper // not accessible here
        }
    }
}

NOTE: This is a CMS project and uses Sitecore. I don't actually know what it is may be this helps. I am totally new to C# and have been thrown into bunch of C# websites.

Thanks in advance

回答1:

I assume you have a Web Application Project instead of a WebSite Project. As such, in the Solution Explorer, navigate to your *.ascx file, expand it to see the C# code-behind and you will also see a *.ascx.designer.cs file. Delete that "designer" file. Next, right-click on your *.ascx file and select Convert to Web Application which will re-generate the designer file. Now try to access the control via C#.



回答2:

use "CodeFile" instead of "CodeBehind"

<%@ Control Language="C#" Inherits="FSB.Layouts.Short_Home_Loan_Application" CodeFile="Short Home Loan Application.ascx.cs" %>


回答3:

You can adjust theses elements :

1 CodeFile

CodeFile="Short_Home_Loan_Application.ascx.cs" %> //AddCodeFile, and set the same name

2 Runat

Add runat="server" to your forms

3 And replace your Panel with Div Container



回答4:

In your ascx page header, modify your Inherits directive to include the Namespace prefix in your code behind.

Revision

I have taken your original markup as posted and placed it in a test web application, and it compiles cleanly with the proper references available in the codebehind using VS2010.

Is there any possibility that this control was originally authored under a different name, in a different namespace, or copied from a different project and pasted into a shell source file in this project? The problem you are experiencing seems directly tied to the absence of the intermediate files ASP.NET generates in the background to establish protected declarations for markup-based entities that can be referenced in the codebehind of the control's source. If, for some reason, VS2010 cannot detect the need for these intermediate files to be regenerated, the markup and source will become out of sync and fail in a manner similar to what you are experiencing. I obviously cannot guarantee that is the cause of your particular problem, but I would at least consider it a strong suspect.

I would respectfully suggest that you create an entirely new control within VS2010, via the "Add New Item->" option in Solution Explorer, and add just the markup from this control into the ascx file (not the directive line), and then see if the code-behind for that new control can reference the server-side elements.

Good luck.