How to reuse web user controls from another project in the solution? - c#

I want to use web user controls inside 'Controls' project inside the 'Web' project in the same solution. but when I reference 'Web' to 'Controls' and then drag the control into a web form in 'web' project the Register directive's src is like this:
<%# Register src="http://localhost:59388/UC/Menu/MenuManager.ascx" tagname="MenuManager" tagprefix="uc1" %>
and an error says:
http:/localhost:59388/UC/Menu/MenuManager.ascx' is not a valid virtual
path.
Some people say thay It's not possible to reuse web user controls and we have to convert them to Server controls, but I've seen somewhere some one did this.
Do you know how to do this?

You cannot use an ascx via http. Essentially you are trying to get the ASP.NET compiled and processed output of the user control if you do that.
You are better off writing a Control Library if you plan to share controls. See this question
Scott Guthrie has a write-up about creating User Control Libraries

Related

Compilation error only in server: The base class includes the field ‘MyControl_1′, but its type (MyControl) is not compatible (ASP.MyControl_ascx)

I'm desperate.
We have a little project in ASP.NET (webforms) with Framework 2.0. I've made a little maintenance over a UserControl (ASCX). I'm my development machine works perfect but when I upload the file to the server this exception is thrown:
The base class includes the field ‘MyControl_1′, but its type
(MyControl) is not compatible with the type of control
(ASP.MyControl_ascx)
I've tried almost everything:
http://chanmingman.wordpress.com/2011/09/07/the-base-class-includes-the-field-mycontrol_1-but-its-type-mycontrol-is-not-compatible-with-the-type-of-control-asp-mycontrol_ascx/
http://support.microsoft.com/kb/919284
This is only happening in production servers. I've tried to wrap the ASCX in a namespace:
CODE:
namespace MyControl {
...
}
ASCX:
Inherits="MyControl.Control"
ASPX that uses the control:
<%#Import Namespace="MyControl" %>
<%# Register Src="Controls/SomeControl.ascx" TagName="SomeControl" TagPrefix="uc2" %>
<uc2:SomeControlID="Control1" runat="server" />
The compilation mode is not using fixed assemblies naming, and I can't change it.
I have had a similar problem. I found the fix - change the user control declaration.
Your production server will probably be running a web app. Web Apps expect the declarations in a user control to be of a certain format. You may have placed a user control built elsewhere in a file based project into a web app?
With Web app user controls (by default) you get a CodeBehind="MyControl.ascx.vb" and an Inherit of AppName.MyControl.
With a file based project you get CodeFile="MyControl.ascx.vb and an Inherit of MyControl.
I changed the declaration in the user control until it worked. Looks like you may need to supply a new dll to the production server after you make the changes and recompile.
Be sure if you have a page that already contains any usercontrol will be updated to publish
so, in this case you will publish Usercontrol and Page with precompiled dlls for both
I went to the page the OP provided:
https://support.microsoft.com/en-us/kb/919284?wa=wsignin1.0
The second suggestion worked for me:
Method 2: Reorder the folders in the application
So, in the end, I put all the controls in the same subfolder, and it worked fine. It may not be an option for everyone, but it's worth trying.

Allow a Web User to Create a New .Net Page in the Project

I'm not sure if this is possible, but I'd like to allow a user to create a new .aspx page from a web interface. So, for example, say I was building a wiki site and wanted to allow a user to add an article on how helpful stackoverflow is. Is it possible to create a new .aspx page from a master page and then allow the user to add content to it? Also, it is possible to allow users to delete pages form a VS project?
Thanks,
Brent
Try content management systems, like:
WordPress
Joomla
DotNetNuke
or others.
if you install DotNetNuke (dnn), it wil be possible to have pages that look like: mydomain.com/wiki/mynewpage.aspx
But you might be better of using the DNN wiki module (or an article module) to allow the user to enter content (based on his/her user rights). These modules are espacially tailored at the task at hand.

Add general C# code to a SharePoint 2010 site page

I want to add a C# class to an existing SharePoint 2010 site page. The site page, being created either through the SharePoint UI or in SharePoint Designer, has a web part on it that I want to access and edit its properties. I could just make an invisible web part in Visual Studio, deploy it on the page, and run the code I want to run that way, but I feel like that may be bad practice (or is it)?
I'm kind of talking about how a general non-SharePoint .aspx page can reference a C# file, kind of like below:
<%# Page language="C#" Inherits="System.Web.UI.Page" CodeFile="Example.cs" %>
Would a line like above be added to the .aspx file? How is the .cs file actually deployed on the SharePoint site? Finally, is this even possible?
I'm new to SharePoint and to this site, so I appreciate all help!
By default, SharePoint does not support code behind files in regular pages for security reasons (although, as alfonso mentioned, inline code and code behind files are supported by application pages). You can override this setting in the web.config, but that is not considered to be a good practice.
You certainly could create another web part to do what you want. I have done something similar to that before. But typically, when I want to change the behavior of a custom page, I change the Inherits attribute of the Page directive:
<%# Page
language="C#"
DynamicMasterPageFile="~masterurl/default.master"
Inherits="MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
%>
Create your class, have it inherit from System.Web.UI.Page, then deploy it to the GAC. Not only is this supported, but this is how most of the out of the box SharePoint pages do it.
I think there's no "correct" way to do this with a site page. The only way would be using webparts as you said, or user controls. However, you can add code very easily to an application page. This guy explains this very well.
You should have a look at the developer modules for SharePoint on MSDN
http://www.mssharepointdeveloper.com
There are various ways how to customize SharePoint 2010 as already mentioned earlier here you could use
WebParts
ApplicationPages or LayoutsPages
Custom MasterPages
DelegateControls
There are more ways how to extend SharePoint, so it really depends on your situation or on the requirement you've to achieve.

when do you need .ascx files and how would you use them?

When building a website, when would it be a good idea to use .ascx files? What exactly is the .ascx and what is it used for? Examples would help a lot thanks!
It's an extension for the User Controls you have in your project.
A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit.
Simply, if you want to have some functionality that will be used on many pages in your project then you should create a User control or Composite control and use it in your pages. It just helps you to keep the same functionality and code in one place. And it makes it reusable.
We basically use user controls when we have to use similar functionality on different locations of an app. Like we use master pages for consistent look and feel of app, similarly to avoid repeating the same functionality and UI all over the app, we use usercontrols. There might me much more usage too, but I know this one only...
For example, let's say your site has 4 levels of users and for each user there are different pages under different directories with different access mechanisms. Say you are requesting address info for all users, then creating address fields like Street, City, State, Zip, etc on each page. That would be a repetitive job. Instead you can create it as an ascx file (ext for user control) and in this control put the necessary UI and business code for add/update/delete/select the address role wise and then simply reference it all required page.
So, thought user controls, one can avoid code repetition for each role and UI creation for each role.
Ascx-files are called User Controls and are meant for reusability and also for making complex aspx-pages less complex (lift out some part of the page). They could also be beneficial for something called donut caching, that is when you would like to cache a certain part of a page.
If you have a block of code+html that appears on several pages and is sort of independent of that page (say a block of latest news items), you could copy/paste the code to every page.
It is however better to put that code in its own block and just include that block on every page that needs it. That "block" is an ascx file.
One more use of .ascx files is, they can be used for Partial Page caching in ASP.NET pages. What we have to do is to create an ascx file and then move the controls or portion of the page we need to cache into that control. Then add the #OutputCache directive in the ascx control and it will be cached separately from the parent page. It is used when you don't want to cache the whole page but only a specific portion of the page.
ASCX files are server-side Web application framework designed for Web development to produce dynamic Web pages.They like DLL codes but you can use there's TAGS
You can write them once and use them in any places in your ASP pages.If you have a file named "Controll.ascx" then its code will named "Controll.ascx.cs".
You can embed it in a ASP page to use it:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Controll.ascx.cs"%>
When you are building a basic asp.net website using webcontrols is a good idea when you want to be able to use your controls at more then one location in your website.
Separating code from the layout ascx files will be holding the controls that are used to display the layout, the cs files that belong to the ascx files will be holding the code that fills those controls.
For some basic understanding of usercontrols you can try this website

ASP.NET Web User Control Library

We have a bunch of user controls we would like to pull out of a web application and into a separate assembly/library, and I thought it would be as simple as creating a class library and pulling the ascx and ascx.cs files into the project, and compiling a DLL to be reused among our applications.
This was not the case, however.
Our ultimate goal is to have a single distributable DLL (similar to how Telerik distributes their controls) that we can throw into any web application. The steps here: Turning an .ascx User Control into a Redistributable Custom Control were very simple to follow, however this results in many files named controlname.ascx.guid.dll, which is not the desired result. I couldn't even get these to work anyways, since we have additional classes that need to be compiled into the assembly.
Has anyone successfully created a web user control library in .NET (we're using 3.5 here)? I can't seem to find a nice step-by-step guide.
I realize this is an old topic, but if anyone is looking for a solution for creating reusable user control libraries, it turns out it's fairly simple. Here are two good step-by-step guides along with source code:
From MSDN: Turning an .ascx User Control into a Redistributable Custom Control
From Code Project: Straight way to create ASP.NET user controls library
The second link provides a solution to the multiple dlls created by the first link.
Edit- (2) Seems to be a dead link. Here's the new link
https://www.codeproject.com/Articles/30247/Straight-way-to-create-ASP-NET-user-controls-libra
If you want to share controls among project, my experience has shown that the best way is to build custom asp.net server controls instead of usercontrols. User controls are good for sharing within the same project, but not over multiple ones.
For this purpose I suggest you to build a set of custom server controls inside a class library and use that on all of your projects.
This book does quite a good job at explaining the basics of creating server controls
Edit:
I'm currently developing a .net web server control library. I actually didn't follow any step-by-step guide. I mostly considered using the book I mentioned above and the MSDN library + Reflector, which is a great tool for inspecting existing MS server controls and learning from them.
I found the tutorial Creating and Using User Control Libraries but it seems like a bit of a hack as it relies on a post-build command line event to copy the user controls from one project to another.
Somewhat late, I admit.
To create a re-usable library of user controls; create a new Web Application Project, delete all the scaffolding, add a (number of) user control(s). Create a Web Deployment Project from the Web Application Project, in the WDP properties choose the option to Merge all control output and assign a name for the library and ensure that Allow this website to be updatable is NOT checked.
Build the WDP and use Reflector to examine the generated library; you'll see that it contains an ASP namespace and the types you carefully crafted have been renamed i.e. usercontrol_ascx. In your target website(s) add references to BOTH the output dlls from your WDP, add a system.web/pages/controls node to web.config using the namespace ASP and the name of the assembly that you defined in the WDP.
Now when you use the library in a page (for example) you must use the alias that you defined in web.config and the typename as seen in Reflector i.e.
<ucl:usercontrol_ascx ... />
I found it useful to add a dependancy for the website(s) on the WDP so that the WDP is built before the websites; now I can change the user controls in the WAP without having to remember to build the WAP before building the website(s).
I hope that someone finds this useful as it cost me a few grey hairs getting to this stage and still have VS do its 'automagically' thing.

Categories