Where Controls Declarations in a website project are put - c#

I have website i cant access the controls in the code-behind
i drag controls and add them to ASPX file and when i try to access them i cant.
i tried opening the website using:
Visual Studio 2012
Visual Studio 2010
when i even double click on a button it creates its event in the code behind but i still cant access the control!
there is no designer.cs file since this is a web site any idea how to fix it ?

If memory serves, the markup is the designer for a page that lacks a designer file. You can observe this behavior in classic ASP pages that have been ported to .NET.
The compiler simply parses the page, looking for a tag that includes runat="server" and generates a server-side control for it. If there's no ID, one is generated for it. Further, if AutoEventWireup="true" is set at the page level, the events will be automatically connected to these controls, as if by magic. It's all just compiler voodoo, if you ask me, but rather convenient compiler voodoo.
But that's an educated guess, based on experience, and I'm sure someone with far more experience with ASP.NET internals can provide more riveting details. (Jon Skeet, et al.)

Make sure your .aspx file specifies its codebehind file:
<%# Page Title="" Language="C#" CodeFile="YourPage.aspx.cs" Inherits="Content_YourPage" %>

If it is a website then websites don't have designer.
But if you need the designer's then try to convert it to web application

Related

In VS 2019 - How to regenerate code-behind and designer files from an existing aspx page

I imported in my project Web c# an aspx page, for example, mypage.aspx, with HTML code. This page is very simple.
All I want is to generate the 2 files (in IDE) aspx.cs and designer.cs (mypage.aspx.cs and mypage.aspx.designer.cs).
I tried "Convert to web project" functionality but it does not work.
Any idea, please?
Of course, I read several solutions on this site but none worked (with vs 2019).
If there is no actual code in the original code-behind file then it's pretty easy to do. Create a new page of the same name. Then just copy and paste the markup from the original, taking care to leave the newly-created page directive as is.
If there was logic in your original code-behind then you're out of luck. You might try decompiling the original DLL and try to re-create the original code that way.

Cannot access controls from code behind & cannot convert to web application ASP.NET C#?

I've had an ongoing problem in my asp.net website, where I cannot access controls from the .cs file. It gives me the error: control not in context or whatever it normally does.
What's weird is that I have no designer.cs files or the option to convert to web application.
I've tried right clicking my project name, my file names, and everything else, but I cannot find "Convert To Web Application." I'm using Visual Studio 2013 Ultimate Edition and the latest versions of ASP.NET and stuff. For example, I have the following code in my page:
<asp:TextBox ID="AssignScreenName" runat="server" CssClass="form-control" Placeholder="Screen name"></asp:TextBox> (It's for renaming yourself on your profile)
When in the Page_Load class of my code behind, AssignScreenName.Text will give me that error. I have to use Page.FindControl("AssignScreenName"), which doesn't let me convert to text and assign it to the profile.
Is there a way around this, or a way to fix it? (If you need anymore information post it in the comments, I'll try to check back here daily)
Thanks!

GridView to Excel ASPX in SharePoint

I have an ASPX file which I am running in SharePoint that has a GridView which I am attempting to export into an excel spreadsheet. I know the code I have for the export is correct; however, I am getting this error:
RegisterForEventValidation can only be called during Render();
I have done some research and have found a solution, which might work with normal ASPX pages created in VS with a CS code behind class, which is setting the EnableEventValidation to false, and I am getting a Parser error when I attempt to use this solution on my ASPX page.
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"%>
Is there any other way to allow this, or any other workaround to my problem using just the ASPX page without the code behind? All of my C# code is within the head and has to be in order for me to run it in my SharePoint environment. 12 hive storage of the CS class is not an option for me. Also, I am not wanting to change anything in my web.config folder if possible.
Thanks
Try creating it as a Web Part - this is the more common approach for this type of scenario in SharePoint.
In SharePoint, all customized pages are run through the Safe Mode Parser which prohibits inline code. Also, this parser does not allow adding controls to pages which have not been marked as "safe".
To get around your issue, you might want to look at "Application Pages" which are added to the \LAYOUTS directory. They reference the master page and can have inline code, but they can't be customized. They are compiled into a single assembly DLL:
Creating an Application Page in Windows SharePoint Services 3.0

Broken link between aspx and aspx.cs files

I've had the same problem a couple of times with different ASPX pages after renaming them and I am surprised that I can't find someone else with the same problem on stackoverflow.
When I run my ASP.NET C# project, the debugger gives me a message like this one.
Error 5 The name 'txtTitle' does not exist in the current context
It seems that the aspx and aspx.cs files at no longer bound. The only fix I have found for this is to recreate the page and copy/paste my code.
Any idea how to fix this without recreating the whole thing?
Thanks
The code file contains a partial class that is referenced in the ASPX header declaration. Both file name and the actual class in the ASPX header have to match for this to work.
<%# Page Title="TestPage" Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
In your case probably the class name does not match. Check if the class name in the codebehind .cs file matches the name after Inherits.
The concept of partial classes used in ASP.NET is detailed here.
I am surprised at this occurring I regularly rename Silverlight user controls with no harmful side-effects.
Are all of the references to classes being renamed in the aspx page and code behind ?
You might try using this util to relink them.
This can also be caused by the whatever.aspx.designer.vb file being dependent upon whatever.aspx.vb instead of whatever.aspx. If this is the case, it should show incorrectly in the file tree when you expand the group of files in Visual Studio. To fix it, simply exclude the affected files from the project and then re-include them - the faulty project config file should be regenerated and it should now work correctly.
(Same theory for C#.)
I've had the same problem and noticed that sometimes, but not always, the CodeFile setting in the .aspx page is not updated when the class is changed. If this is the same problem you have, you can change it manually:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MyClass.aspx.cs" Inherits="MyClass" %>
^^^^^^^

Using a User Control from Another Project

I have built a user control in a project that I want to re-use in a separate project. I had seen similar attempts successfully accomplished by adding a reference to the original project and then registering the control via:
<%# Register Assembly="AssemblyName" Namespace="AssemblyName.Namespace" TagPrefix="xxx" %>
I did something similar, compiling the original project and referencing it in the subsequent one . The control has an asp:Repeater control within it. I then registered my control as above and placed the control on my page like so (the control has a string property named prop):
<xxx:ControlName ID="ControlId" runat="server" prop="21" />
However when I launch the control in the subsequent project, I get a null reference exception and the application chokes. Am I doing this the wrong way? Is it mandatory that I have an .ascx file in the subsequent project?
Any and all help is appreciated.
Thanks,
pbr
Just as a follow up to this:
I did some further research and learned that the mark-up language (materials on the .ascx page) do not get compiled into the assembly. Therefore you must have the .ascx file within the project that calls it if there is any markup associated with the control.
In the properties dialogue I set up a post-build event that copies the .ascx file over to the subsequent project Controls file. I reference it as I would any normal User Control and everything works fine.
Hope this helps someone out there with a similar issue,
pbr

Categories

Resources