Asp.net controls are not accessible in code behind

后端 未结 26 1075
慢半拍i
慢半拍i 2020-12-15 16:25

I have a project that was previously built by someone else. When I try to add a new control to the page, I can\'t access it in the code behind. When I try to access it there

相关标签:
26条回答
  • 2020-12-15 17:19

    This happened to me in a website project in VS2010, but as I'm part of a larger team, I don't have the option to convert to Web Application as Domenic suggested.

    It turns out it was because I had switched my default editor for ASPX files to "HTML Editor" instead of "Web Form Editor". I did this because intellisense was killing my computer whenever I opened an ASPX file. When I right clicked on this page to "Open With..." the "Web Form Editor", and added the controls again, it worked perfectly and they're now available in the code behind file.

    0 讨论(0)
  • 2020-12-15 17:19

    I also had a similar problem in the past. As you have mentioned that the project was previously built by someone else, So it must be coded in VB based coding and you can check that in web.config compilation details. Try copying the aspx page content onto your newly created C# aspx page and paste the code as well it will work.

    0 讨论(0)
  • 2020-12-15 17:22

    Have you renamed your project file? If you have, then remember to rename the Inherits at the top of your ASPX page.

    0 讨论(0)
  • 2020-12-15 17:22

    Faced a similar problem because the controls were either within the: "LoginView" or "AnonymousTemplate" and Viewstatemode was Disabled

    Resolved by moving the Asp controls outside these tag elements

    0 讨论(0)
  • 2020-12-15 17:23

    If your trying to access the tags from your own built in method on the back-end make sure that you don't make this method static - otherwise you wont have access to these tags...

    <p #id="myTag" runat="server">...</p>
    
    private static void myMethod(){
        myTag.InnerText = "Hello" //You will not have access to this tag
    }
    
    private void myMethod(){
        myTag.InnerText = "Hello"; //You have access to tags
    }
    
    0 讨论(0)
  • 2020-12-15 17:27

    I have the same problem. I found the solution by removing "PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>" from aspx page and it works normal now.

    0 讨论(0)
提交回复
热议问题