How can include a reference in a standalone ascx control? - asp.net

I have an ASP.NET ascx control. In the code behind file I need to add some reference (Sharepoint Libraries) to do some queries. How can I do that? Is a standalone control, I mean I have only the .ascx and the ascx.cs files. They are not part of any project so I don't have the tipical Property folder in the solution explorer, and they are loaded in a sharepoint page using the SmartPart control. So I'm little lost here, any help will be appreciated.

The simple answer is that you can't, In ASP.NET references are added to the web.config file of the application not the individual control. You would have to edit the SharePoint web.config file.

I found that if I put the dlls in a \bin folder at the same level of the control, I can use the code defined in it without problem. So I don't need to add a reference, by convention all dlls in that folder are available to my control. More clearly the file structure is like this:
\UserControls\bin\AnyCompiledLibrary.dll
\UserControls\AnyControl.ascx

Related

Can't find code behind files in Sitecore File Explorer

When I view an ascx file from Sitecore File Manager and it displays something like CodeBehind="Promo Downloads.ascx.cs in the header, but It doesn't seem to exist. There is an asp:Literal control I'm trying to figure out where it's coming from.
.cs files are compiled into a dll and will/shouldn't be available in the Sitecore solution using the code behind model. This only happens if you use the old code file model.
If you want to see the code for this then you'll need to view the original source code or decompile the dll.
To decompile the URL: The dll will be in the /bin folder. To find out which dll, look at the namespace in the .ascx file - the name of the dll should be the first part of the namespace.
Once you've found the dll, use dotpeek https://www.jetbrains.com/decompiler/ to examine the code.

Aspx control are not visible in cs file

I upload 2 page and downloaded same page.I added 2 more control in that file.But my aspx page control are not visible in my .cs file.When I am compling it,error occur on cs file(ddlbrand is not exist in current context).I am using vs 2005.I don't have designer file.(Only aspx and cs file).
Check if your controls are well formed, if the problem exists than I think you can't do that without the whole project and the solution file. You need to make changes in the project with visual studio. All the controls you add in the page are also added to the designer file.
Web Application Projects compile the .CS files as part of the build, and compile the ASPX's on the fly referencing your pre-built codebehind DLL - that's why you have to recompile if you change the code behind.
No designer file means you are working in ASP.Net Website project.
Check your aspx markup it must be missing some closing tag or some important parameter like runat="server"

How asp.net application works?

I am quite new to .NET development and I am just wondering how does it work?
My undermentioned points are:
While developing ASP.NET application, under the project we have files like:
pagename.aspx
pagename.aspx.cs
pagename.asp.desiger.cs
After adding certain functionality to pagename.aspx page, assuming I have the development required web application (this is not my concern, what is developed)
Now I'm going to deploy this application, I use web deployment MSI which creates the required files in the one folder called folderdelopyed.
This folder contains the files required to support this application but interesting does not contain pagename.aspx.cs and pagename.aspx.designer.cs files.
My question is if folderdelopyed does not contain .cs file, then how does it work to run the segment of code which I have written in this file called PageName.aspx.cs?
The code in your cs files gets compiled into a dll.
For Web Application projects this is one dll
For Web Site projects, this is a dll per page.
All of the code is now in the dll's in the bin folder of the website.
You can use a tool like ILSpy (http://wiki.sharpdevelop.net/ILSpy.ashx) to look inside the dll's and see your code.
In the old days, for classic ASP, the script used to be embedded in your page - a mix of code and HTML, and was interpreted at runtime.
I like the new way more :-)
ASP.NET code is compiled into Dynamic-link library files, also known as DLL files.
The code you write in your code behind, which is the files with .cs extension, is compiled and put into whole new file, with .dll extension - and that file is copied to the server, to the BIN folder of your site.
Depending on what project type you choose, it's possible to have several DLL files for the web application, changing in every build - see dash's answer for more details.
On every .aspx page you have referece to what DLL file to use, as the very first line. For example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="pagename.aspx.cs" Inherits="MyNameSpace.pagename" %>
In this example, the Inherits part determines what DLL to use. How? By the namespace, which is also the name of the DLL file.
When the above .aspx is requested by a browser, the .NET engine will go to the BIN folder, look for MyNameSpace.dll and in there look for class called pagename that inherits from the base Page class - all the rest is typical life cycle of ASP.NET page.
let me to say you something more Amazing.
you can hide your aspx file too.and put their content in to dll as same as your cs file put in dll.
you can make k aspx that just contain an address to the ddl file and no html body :D
that was greate!!! not only you can hide your cs file, you can hide you aspx file too :D

consuming classes from folder other than App_Code

I am creating a site wherein I have a folder which contains some .cs files. I want to access those classes in .aspx and .ascx files. I’ve created some properties in it, but when I create the object of the class I don’t find that property via IntelliSense.
How can I use and consume those properties from that .cs file?
The website will only compile code files that are in the App_Code folder or are codebehind files for referenced controls. There isn't a way to reference classes defined in code files outside of the App_Code folder.
If you compile those classes and put the resulting dll in your website's bin folder, then you can reference them. To do that, you'll need to add them to a Web Application project in Visual Studio. See Ian Robinson's WAP blog post for most details.
Just have those properties public and you'll see them.
If still no luck please post your code and also tell: can you create instance of the class without error? Can you access any other properties or methods?

Where can I put my source files?

I am developing a web form using Visual Web Developer
Currently I have class source files in the same directory as the web form, or in the App_Code folder. I'd like to organise the files into folders within the web form folder, but I can't find a way of adding a reference to the folders, and they don't seem to be being picked up automatically.
These are files that are under constant development.
What is the asp.net/c#'s equivalent concept to c++'s #include?
This is a Web Site rather than a Web Application
I'd suggest taking these out into a separate class library project and you can then reference this DLL in your web project. You would then add a 'using' statement at the top of your web form code to include this reference.
In a C# file (foo.cs), you would use:
using MyProjectsDefaultNamespace.Folder1.Folder2
In an aspx or ascx file, you would use:
<%# Import Namespace="MyProjectsDefaultNamespace.Folder1.Folder2" %>
Never really thought of doing this, but i guess i would do this as follows.
A folder represents a namespace. So where it says inherits="Project.PageName" in your PageName.aspx file, it should state inherits="Project.Folder.Folder.PageName". You also have to change the namespace in your PageName.aspx.designer.cs and PageName.aspx.cs files.
EDIT:
For ASP.Net website simply adjust your CodeFile attribute:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Folder/Folder/Default.aspx.cs" Inherits="_Default" %>
Its not clear if you mean the codebehind .aspx.cs file or a standalone .cs class file.
You can't have .cs files loose in your main folder. It has to be in the app_code folder (or in a class library if you were doing a WAP).
Your .aspx.cs files are paired up with your .aspx file. I wouldn't recommend trying to move these files away if thats what you are trying to do.
The top level namespace that contains _Default or any code that doesnt appear to have a namespace is ASP. This is generally hidden within Visual Studio. So its true name is ASP._Default
And the answer is, obtained from looking into the other answers thanks:
Organise the files into folders within the App_Code folder, they will automatically be included.
Right click on project in solution explore
Go to Add> Add ASP.NET Folder > App_Code
Then add your files/folder structure there
BUT, be warned of what you put in there. Please refer to:
http://vishaljoshi.blogspot.com/2009/07/appcode-folder-doesnt-work-with-web.html
In the properties of the source file(s), you might want to set the "Build Action" property to "Compile"

Resources