SharePoint Webpart and ASP.NET User Control - asp.net

I am developing SharePoint Web Parts for MOSS 2007 on Visual Studio 2008. Up until now, I've been adding all my controls by hand to the code behind... but an earlier post suggested I could use the designer to create an ASP.NET User Control, then add it to the webpart, and everything is happy... See figure 5 for an example.
However, I can't seem to add a new ASP.NET User Control to my MOSS WebPart project, the template just doesn't appear. If I create a WebApplication and make my User Control in there, I can't see any SharePoint templates to add to the project. Finally, I tried copying a simple aspx file and its code behind to my webpart directly, and adding them as an "existing component"... but now the designer won't recognize the aspx file. Next, I'd probably try adding two projects to my solution, and just referencing any dll's from the ASP.NET application...
So how do I get an ASP control into my SharePoint WebPart project so that I can use the Visual Studio designer?
Edit: So here is a partial solution where the ProjectTypeGuids needs to be modified. When I try this, it gives me an error saying "The project type is not supported by this installation". I am using Visual Studio 2008.

Try reading some of the following tutorials:
http://www.google.com/search?q=create+sharepoint+web+part+as+feature
What type of functionality and/or display are you trying to create? You may not even need a user control. For example, create a very simple "proof of concept" HTML table as a custom Web control (Web Part). Create it as a Feature and use the Solution Framework to deploy it:
Create a Web Part with VSeWSS 1.3 - Part I
Create a Web Part with VSeWSS 1.3 - Part II
Scott Guthrie offers up resources here on Web Parts and User Controls:
Writing Custom Web Parts for SharePoint 2007

Related

Where is data manipulation code in ASP.NET Web site Template in Visual Studio

I am rather new in ASP.NET so I decided to use ASP.NET Web Site template in Visual Studio 10. I have succesfully changed database from express edition to my online server but I fail to see where is the code that drives logic behind login and membership roles in that site (It is there by default). There is no code in .cs files so there is zero possibilies for me to change the code since I can't even find it.... I see that most of the logic is in stored procedures but they still need to be called from somewhere?
The code you're looking for is inside of the Login control etc. You can't modify the provider that way.

How to add Web Pages 2 with razor syntax in an empty web application project in Visual Studio 2012?

I want to create simple Web Pages 2 applications.
I want to use the File->New->Project->Empty Web Application and not the File->New-Web Site.
After i create a new empty web application i cant add a .cshtml file..
The only way that i could do it is:
Nuget -> Install Web Pages 2 with razor libraries
Add->New->Razor MVC View
I am NOT putting the .cshtml file in Views directory. Instead i leave it in root and name it Default.cshtml
Is THIS really the way to do it?
Actually, you can do this. I've done it this way.
Open VS 2013
From the main menu: File > New > Project
Under Installed Templates
Select
Templates
Visual C# (or VB)
Web
(do not select VS 2012)
Select ASP.NET Web Application from the list of projects on the right of screen
Next Screen - Select Empty Project, Check Webforms
Run once, get error for no default page
Only files in project
Global.asax
Web.config
Select "Project" in Solution Explorer
(do not select the Solution)
Select Manage NuGet Packages
Microsoft ASP.NET Web Pages
Microsoft ASP.NET Web Pages Data (Important! Includes WebMatrix dlls)
Add Layout and View pages
Notice the code is recognized as an error
If you run, you get a Server Error that ViewBag does not exist
Change ViewBag to Page
Run again. You should be able to develop like standard web pages but with Razor and no viewstate.
That should be it. I've been doing this since VS 2012 and love the results.
I contacted Scott H. at MS about adding this as a project type. Didn't get
far. He didn't see the point in doing a project this way. IMHO, I disagree.
Web Pages 2 applications are Web Sites, not Web Applications, so you should do the following:
File » New Web Site » ASP.NET Web Site (Razor 2)

Best practice for integrating a silverlight control in a Asp.Net Solution

I need some advice regarding the handling of a Silverlight control that I need to integrate into an existing Website, coded with Asp.Net.
I'm using SVN, Visual Studio 2010, Framework 4.0. I need to create a simple silverlight control and I need to have it embedded in one of my asp.net pages.
My question is basically, what is the best practice on where to put this Silverlight control?
1) Creating a new project for my Silverlight control and then refer to it through an external tools within my website? (importing only the .xap?)
+ Reuse
- Seems lot of extra work for a small control
2) Create a new project withing my website solution, compile it next to my website and create a post build event to copy the .xap generated into a ClientBin folders in my website?
- Lack of reusability
+ Easy to make small modifications in my Silverlight component.
3) Another solution I don't know yet??
Thanks for your advices
Do not worry about number of projects. Reuse is far more important (we have solutions with over 100 projects).
Create a new Silverlight application, then add a Silverlight relationship between the website and your new Silverlight application:
Right-click on your website project and select Properties.
Choose the Silverlight Applications tab on the left side
Press Add...
Leave Use an existing Silverlight project in the solution checked and select the project you want to add from the Project dropdown.
Leave Add a test page that references the control checked if you want a separate test page for your application.
Press Add and you are done.
The website will now build your Silverlight application and include the output Xap file in its ClientBin folder.
If you check "add a test page", that will give you sample HTML/ASPX pages and the JavaScript you need to run the Silverlight component anywhere in your ASP.Net app.

Custom Server Control that can be dragged into design view in Visual Studio

How do I setup a custom server control, so that it can be dragged into a design view in Visual Studio?
I have create a separate ASP.NET Server Control project and a custom MailLink control, basically by copying the code from this MSDN article: How to: Use Custom ASP.NET Server Controls in Visual Studio. The control works fine, if I manually register the library and insert the control in my web page with these two lines:
<%# Register Namespace="Samples.AspNet" TagPrefix="Sample" %>
<Sample:MailLink runat="server" />
But how do I register / configure the control, so that it can be dragged into a design view i Visual Studio? If I drag my control from the solution explorer all I get is the path to the control. If add the dll from the server control project to my VS toolbox I get a clipboard FORMATETC error when I drag the control onto the design view.
Here's a how to that I found more useful than the MSDN one.
http://www.osnews.com/story/6802/Visual_Studio_NET_Design_View_and_Custom_Web_Controls
This shows a step by step process of how to handle it http://www.newmobilecomputing.com/story/6802/Visual_Studio_NET_Design_View_and_Custom_Web_Controls/page4/
Just to make sure we're on the same page:
There are more than one type of control you can build in ASP.Net. Unfortunately, depending on what kind of control you build you can normally only get designer support for a control from Visual Studio at either the time when you are developing the control, or the time you are developing the page where the control will be used, but not both. Controls that are not supported will just show up as grey boxes.
IMO this was a major failure of Visual Studio/ASP.Net, but I digress. I have seen this limit overcome by using an ascx control with no code-behind.
Now that that's out of the way, let's move on.
It sounds like in this case you're working with a Server Control. You're in luck, because Server controls should get designer support from Visual Studio when you're building the page where the control is used. However, you need to make the control available to the project.
Server controls are normally compiled down to a dll assembly. You must include a reference for this file in your solution. At this point you should be able to right click in your toolbox and select Choose Items.... This will present you with a dialog box that will allow you to select your assembly. At this point your controls should show up in the toolbox.
Unfortunately, Visual Studio can at time be quirky about adding these controls. You may need to restart Visual Studio for them to be available. Note that I still use Visual Studio 2005, so hopefully things have improved in this respect for 2008.

Is it possible to add a web user control to a class library?

I'm looking at building some web user controls with an eye toward re-use, but I can't seem to add a Web User Control in my class library in VS2008. Is there a way to work around this problem, or is there a better approach to creating reusable controls?
You can create either Web User Controls or Web Custom Controls that encapsulate the functionality you need. The main difference between the two controls lies in ease of creation vs. ease of use at design time.
You should maybe consider creating a Web Custom Control library. There is a walkthrough for creating a web custom control using the Web Control Library template.
According to the MSDN article "Recommendations for Web User Controls vs. Web Custom Controls" these are the differences between the two types of controls:
Web user controls are easy to make,
but they can be less convenient to use
in advanced scenarios. You develop Web
user controls almost exactly the same
way that you develop Web Forms pages.
Like Web Forms, user controls can be
created in the visual designer, they
can be written with code separated
from the HTML, and they can handle
execution events.
However, because Web
user controls are compiled dynamically
at run time they cannot be added to
the Toolbox, and they are represented
by a simple placeholder glyph when
added to a page. This makes Web user
controls harder to use if you are
accustomed to full Visual Studio .NET
design-time support, including the
Properties window and Design view
previews.
Also, the only way to share
the user control between applications
is to put a separate copy in each
application, which takes more
maintenance if you make changes to the
control.
Web custom controls are compiled code,
which makes them easier to use but
more difficult to create; Web custom
controls must be authored in code.
Once you have created the control,
however, you can add it to the Toolbox
and display it in a visual designer
with full Properties window support
and all the other design-time features
of ASP.NET server controls.
In addition, you can install a single
copy of the Web custom control in the
global assembly cache and share it
between applications, which makes
maintenance easier. For more
information see global assembly cache.
Follow the following steps (from this post by Phil Haacked):
Close VS.NET 2005.
Open the directory C**:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp** (assuming a default installation of VS.NET).
Open the CSharpItems.vsdir file in Notepad. Select the text and copy it to the clipboard.
Now open up the file C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems\CSharpItems.vsdir and paste the contents of the clipboard underneath the existing text.
Now copy the contents of C:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp (excluding CSharpItems.vsdir) into the folder C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems.
Now “Web User Control” should be an option when you select Add | New Item.
Reference: http://haacked.com/archive/2006/02/07/addingwebusercontroltoaclasslibraryinvs.net2005.aspx
As platte's link mentions, if you're going for reuse then Web User Controls aren't very good. The ascx file has to be manually copied to every project you want to use them in, or you have to hack your way around that.
It's better to use System.Web.UI.WebControls.WebControl which is what you get when you add an "ASP.NET Server Control". These are designed for reuse. If one of the existing framework controls fits the bill for the most part and you just need to extend the functionality of it, then add an "ASP.NET Server Control" and change it to inherit from Panel or Menu or whatever.
If you're still determined to get reusable Web User Controls to work, then this article by The Gu should set you on the right path.
There is a project template called "ASP.NET Server Control" that I assume you can use...
--larsw
You can do anything in a class library.
Add reference to System.Web
Create your new Control class that inherits
from WebControl or HtmlControl or
whatever.
That's it. You now have a reusable control for ASP.NET.
You could do some special things like add attributes to your class and properties, but they are really not needed.
[DefaultProperty("Text")]
[Category("...")]
[DefaultValue("")]
You could using virtual path providers but you you should consider whether it really is worth your wile. Consider this codeproject article on the subject.

Resources