Despite my best efforts, I've been tasked by my employer to develop a web application using Sharepoint, instead of something useful.
I'm having trouble embedding User Controls into Visual Web Parts in a Sharepoint project in Visual Studio 2010. I'm using the following steps (note that this code is just a synopsis of what I have- as my real code currently contains enough curse words to get me banned from StackOverflow for life):
Create a new, empty Sharepoint project.
Add a User Control to the project (located in ControlTemplates), named MyControl.ascx
MyControl.ascx:
<asp:TextBox runat="server" ID="txtStuff" />
MyControl.ascx.cs:
public class MyControl : UserControl
{
public int Property { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
txtStuff.Text = "I hate you sharepoint";
}
}
- Add a Visual Web Part to the project, MyWebPart.ascx
MyWebPart.ascx
<%@ Register TagPrefix="c" TagName="MyControl" src="~/_controlTemplates/MyControl" %>
<c:MyControl runat="server" ID="mycontrol" />
When I get to this point, I don't get any intellisense for MyControl, more than likely because VS doesn't know of the existence of the .ascx file. I also get the green squiggly line under the control name letting me know it doesnt exist. However, I can still deploy the project to my Sharepoint server itself and all will work well.
However, this falls apart when I need to do anything useful interacting with the control itself:
MyWebPart.ascx.cs
protected void Page_Load(object sender, EventArgs e) {
mycontrol.Property = 1234;
}
Which yields an error of "System.Web.UI.UserControl does not contain a definition of 'Property' and no extension method...'. The designer.cs file indeed specifies the control as a UserControl, and not its real type.
Since this now yields a build error, I can't deploy the project to the Sharepoint server, so I can't really work around this intellisense issue.
I've found this to be very inconsistent behavior; sometimes the intellisense will appear, depending on whether or not the project is currently deployed to the server. However, there's not a predictable set of steps I've been able to take to get the development environment in a working state.
Any thoughts? Things I've tried:
Right clicking on the web part's ASCX and clicking "Open With..." and choosing the "With Encoding" option. No effect.
Developing with the application both deployed and retracted, and various combinations of closing and reopening the .ascx files, the solution, and Visual Studio itself. Sometimes works, sometimes doesn't.
Disabled the project's "Auto-Retract after Debugging" option (recommended from a knowledge base article). No effect.