UserControls in Class Library - c#

I am converting a 1.1 project to 2.0 and am having an issue with some User Controls.
In the original project, there was a class library project and the website project. In the class library project, there was a folder called UserControls and in this were several ascx files and their code behind files.
I'd like to keep the same structure so I bring in these user controls into my new solution in a separate class library project. I changed it from being a full class to a partial class and changed the CodeBehind to CodeFile of the page.
In the old user control, all the web controls in the User control were listed like follows
protected System.Web.UI.WebControls.DropDownList _findModeList;
etc.
I thought with it being .net 2 and using the CodeFile attribute instead, I may not need to do this but it doesn't compile when I omit it.
However, when I include it, it does compile, but I get "Object reference not set to an instance of an object." error when trying to use _findModeList in the code file.
Is it possible to put User Controls in a class library in .net 2.0 up?

Not really. The .ASCX file should be included in each Web Application project that uses it. To avoid duplicates you can store the UserControl in your Class Library's folder and use the "Add As Link" feature of Visual Studio.
Select "Add Existing Item" in your Web Application's context menu, select the user control and, instead of clicking the "Add" button, click the arrow in that button and select "Add As Link".

Are you working with a Web Application Project? Or a Web Site? It's confusing, I know...
If you are working with a WAP, use CodeFile and let ASP.NET generate the .designer.cs files for you. Do not declare the objects from your .acsx. You are declaring a new/different DropDownList when you explicitly declare _findModeList in the .cs file referenced by CodeFile. That is why you get the null reference... it has not been initialized. If you don't have a .designer file, right-click on the .ascx in Solution Explorer and click "Convert to Web Application".
If you are working with a Site rather than a WAP, use the CodeBehind attribute. It will continue to work like in ASP.NET 1.1 and compile on the fly.
To get a control to live entirely in a DLL... use "custom controls" rather than user controls. Again, confusing.

Actually you can try create new Web Application Project not Create new Web Site , because between these two are different project.
After you create Web Application just create your User Control (.ascx). After that you can try Rebuild your Web Application. After finished you can browse to your bin directory , there should be dll for your web application. Now you can use this dll in another website/web application project. Just add reference to this dll.

Have you tried changing/adding the Inherits attribute in the <%# Control %> tag?
<%# Control Language="C#" CodeBehind="<PathToTheFile>" Inherits="namespace.and.classname" %>

Related

Visual Studio 2013 C#: one solution consume code of another solution

I have Java background and am trying to start on C#.
I wanna create a lib in C# that will be used in other solutions. In Eclipse it's just a matter of creating a jar and adding it to classpath. I know each project in VS2013 becomes a dll, but how can I make a solution see these dll?
Also, in Eclipse, we can create a Web Fragment Project. It can have Servlets, jsp and static js and css files, it becomes a war file and can be imported into another project and its files be used in that project.
How can I do that in VS2013? I'd like to create a solution with static files, master page, some aspx stuff, C# dll, and then use them all in other solutions.
Is there any tutorial (I googled it but found nothing) teaching how to do it?
You have a few options depending on your preferences and scope
Option 1 - The Class Library
You can create Class Library, that can be referenced in your website project. The Class library is a library of classes, interfaces, and value types
You can either Add an existing/New Class Library project to your website solution and reference it directly
You can add the project to your solution by right clicking the
solution (inside VS) -> Add -> Existing project -> and navigating to said
project's .csproj file
or
You can use a new/existing Class Library Project - build it and reference the built dll in your website solution.
you can right click your website solution (inside VS) -> Add -> new project -> choose Class Library
After you've done one of the above ->
Right click the project, you want to add the reference to
Click "Add Reference"
navigate to the .dll in question.
If the dll you want to reference is part of your current solution (as in step 1) -> after you've pressed "Add Reference" - press the "Solution" Tab and it should show up
After you've added the dll.
Remember to reference it in your code files with
Using TheReferenceNamespace;
which will allow you to call the functions inside you dll like the following
FunctionInsideDll(param);
or you could fully qualify your calls instead, like the following
TheReferenceNamespace.FunctionInsideDll(param);
Option 2 - Share MasterPages
if you just want "shareable" masterpages
you can do the following - (taken from this -> MSDN article)
(for future reference - web archive link - just in case something gets moved)
Precompile the Code Used in a Master Page
If you are concerned about code in your master pages being visible to others reusing the pages, you can precompile the master pages' code into a library. In this library, you can include code-behind pages as well as user or custom controls. Compiling master pages does not remove the declarative code for the master files or any server controls used, but you can compile the master files to remove the code for controls or code-behind pages used by the master pages.
If you choose to compile the master pages into a library, you must use the "updatable" build option that allows for later modification of the markup. This option is determined by the Allow the precompiled site to be updatable check box in the Publish Web Site dialog box. For more information about precompiling pages into a library that can be reused, see Building Re-Usable ASP.NET User Control and Page Libraries with VS 2005.
Option 3 - The template
Create a template, and use that template for different projects
In Visual Studio - Press "File" -> Export Template -> follow the wizard.
After it has been exported and you've imported it (either through a checkmark in the wizard or double clicking the vsix file) -it will show up under your project templates when you create a new project.
You can include a project from solution A in solution B by right-clicking on solution B and choosing "Add existing project"
Don't be afraid to edit XML .csproj files. For instance, this works ...
<Compile Include="$(Codez)\z.Libraries\diff-match-patch\DiffMatchPatch\**\*.cs"
Exclude="NotThisOne.cs;**\NotThisFolderWith\This*.cs">
<Link>Libs\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>
...and will give you all the C# files from the source folder, and subfolders, as linked files in your destination project under a folder called \Libs\.
$(Codez) is a Windows Environment Variable I use on my PCs.
I also could have used *.* at the end instead of *.cs.
This is one of those things Visual Studio might break on you, adding a file into the folder full of wildcard-linked files may break them out to separate entries. Or not. Depends on the wind.

Updating an ASP.NET website backend code

It has been several years since I have really worked with ASP.NET C# but I have a client who needs to have a simple radio box added to a form which requires more then just editing the .ascx file.
I know that they have a custom .DLL file which I assume was compiled and then uploaded and contains the codebehind files?
The file has a code behind
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="PFA_Form.ascx.cs" Inherits="FLR.Controls.Input.PFA_Form" %>
Does this mean that if I edit the PFA_Form.ascx.cs I will have to do more then just upload that file via FTP?
As far as I know it is a Web Application Project. I have the files and solution, but Visual Studio is not connected to the web server in anyway.
If it's a ASP.net web site you can do it by just changing the code, it will be recompiled automatically. If it's an ASP.net web application you will have to compile and deploy it manually.
If you make changes to the code behind, you will have to recompile the server side code to create a new DLL.
If you only had to make changes to the ascx page, then sure, you could edit it. In this case, you need to make changes to the codebehind as well, and in ASP.NET that requires you to recompile and use the new DLL.
Depending on what you are deploying too, you need to recompile and get the new .DLL or if its a webapp you need to right click on the main project publish, and that will give you the new deploy package.

Using .dll file in asp.net application

I'm working on asp.net (web application). I have to use time picker control in my page. Hence i downloaded 'time picker.dll' file from some website. I need to integrate this 'dll' file into my project. can anyone pls guide me in the same?
You would add it as a reference in the project. Right click on VS and add reference and browse to it.
In your file include a using statement for the namespace of the DLL and then you should be able to instantiate a new object.
Copy the "time picker.dll" into the Bin folder of your web-app and add <%#Register %> directive into your web-page to use that control.
Assuming dll package the typical ASP.NET server control, you have to add reference to the dll (VS Studio Project -> References -> Right-Click & Add Reference, Browse the dll file).
Next thing to do is to add the control on tool box by right clicking on the toolbox and selecting the control. Now you can drag the control on the ASP.NET Design surface. Alternatively, you can use the object browser to check the control name and use the register directive.

Parser Error on all new aspx pages in IIS7

I have a web site where any new aspx pages throws a "Parser Error". It has existing pages, and I can change the html or the c# code in it, and the change will appear in the web page in the browser.
The error page gives a message of "Could not load type classname". I've tried adding several different pages in Visual Studio, and copying the pages to IIS, so the syntax of the page should be fine.
I;ve also made sure that the "Inherits" attribute has the correct class and the class exists. I'm completely stuck.
Have a look at the Directive at the top of the page. Does it have a CodeBehind attibute? If so change it to CodeFile, and try the page in the browser again.
If that doesn't work, compare the top part of a non-working page and a working page. Would be a little easier if you could post the top parts of the page here.
One thing that popped to mind immediately is to make sure that the target version of the .NET Framework for your web site is the same as the one specified for the application pool in IIS in which the site is running.
Right-click on the web site in Visual Studio and check the "Target framework" setting. Make sure this matches the ".Net Framework version" setting in IIS.
Are you using any usercontrols? Is your project set up as a web Application project or website?
If you are developing a web application project and you copy a new file or add a page without using the add new by right clicking you may have some page files that lack their designer files.
If that is the case then I suggest right clicking any page missing the designer file and click "Convert To Application" Then rebuild your solution

ASP.NET register a control from external dll on a page

I am banging my head against the brick wall today. I am porting a site I had developed on an old box across to a new dev env. I have not just copied all the files as I didn't have a great file structure and some parts of the code needed to be removed as I went along.
Originally I had created a website (File -> New -> Web Site). I wanted a file structure something like:
Popular folder structure for build
So I created a new blank solution so the sln file was on its own, then added projects (various DLL projects) and am ASP.NET Web Application.
This last part seems to have caused me a few issues that have given me a headache. As far as I understand (this could be a little limited), a website (as I first had) is different to the later type I created. For example the App_Code folder part didn't work as before. To solve that I created a separate DLL for the webLibrary cs files and added a reference to it.
My problem now is how I register this on a page to be able to use the controls in it. For example I have a control that inherits from TextBox, when it was in the App_Code folder I could use:
<%# Register TagPrefix="sarg" Namespace="MyNameSpace" %>
Then use
<sarg:SARGTextBox id="clienttitletxtbox" runat="server" OnTextChanged="textboxfiltering_TextChanged" AutoPostBack="true"></sarg:SARGTextBox>
Now it is in its own DLL and namespace I can not figure out how to get it to work, I just keep getting warnings saying "Cannot resolve symbol 'SARGTextBox'".
It is probably really simple but I can no longer see the wood for the trees.
Thanks
I think you have created a WebApplication instead of Website. WebApplication does not support App_Code.
Add your ClassLibrary as a reference.
Then In web.config register your control.
<pages>
<controls>
<add tagPrefix="myctl" namespace="Namespace.ClassName" assembly="AssemblyName"/>
</controls>
</pages>
Rebuild your solution.
Open the page and find your control from Toolbox and drop it on your page,
You need to specify the assembly where your control is declared :
<%# Register TagPrefix="sarg" Namespace="MyNameSpace" Assembly="MyLibrary" %>

Categories

Resources