Asp.net controls are not accessible in code behind

后端 未结 26 1076
慢半拍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:13

    Make sure the codeBehind attribute in page directive refer to the cs file where you call the control

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

    I had the same issue when 2 ASP Pages were using the same Code Behind file (It was ASP.NET Web Site project in Visual Studio 2013, not an ASP.NET Web Application). I have solved the problem by changing it to use different Code Behind files for each page.

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

    I had this problem when I moved a website into a main solution by creating an asp.net application project in the solution and copying aspx & aspx.cs files over. I solved it by right clicking on the project and converting to a web application. This created the necessary designer.cs files which contain the code for the asp objects on the aspx page.

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

    Very old question, but I ran into this issue today, and ended up figuring out a solution that I didn't see mentioned here. So, I'll share it.

    There are three files in a page: the ASPX, the ASPX.CS, and the ASPX.Designer.CS

    When you edit the HTML/Markup for the ASPX directly (like I prefer to do), it doesn't automatically update the designer. That's why it's not showing up in intellisense (at least for me). To get the IDE to update the designer, you have the ASPX page open in Visual Studio (and have it be the focused file); click on the "view" menu, and select "designer" (or hit Shift+F7); the IDE will show you the design view; right-click in the design area and select "refresh" from the context menu; save the file; select "markup" from the "view" menu (or hit Shift+F7 again); and check the designer file to see if the controls you added now exist in the designer.

    There might be a simpler way, but this worked for me to be able to access a control from the code behind that I had added to the HTML manually.

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

    Check to ensure the designer.cs file for the page is included in the project. Right click it and see if you get the option to "Include in project."

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

    Here are a list of troubleshooting I go through when I get similar problems.

    1. Make sure the IDs you are tying to use in the code behind are actually declared in the markup
    2. Make sure these elements have 'runat="server"'
    3. If you are having problems with a specific control, try to create one near the top of the page, if that doesn't work either, it's a code generation problem

    If 3. is your issue, temporarily remove the mark-up of the controls and any reference you had to them in the code behind, ie make it so you can successfully build. Close the designer.cs file if you have it open and do a rebuild. After which try to re-add a basic control (following step 1 and 2) and see if it shows up (it should). Then re-add your mark up as needed and you should find the code generation is working again.

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