I am a long-time ASP.NET developer but a first-time SharePoint developer. One of the most powerful features of ASP.NET custom scriptcontrols. Yet when I try to apply these controls to a custom SharePoint 2010 solution, i have run into nothing but trouble.
Most commonly, the script references are not loaded, so the script descriptors throw a "variable is undefined" error. Usually after I deploy the solution from visual studio, everything will work for only one request; script references are not loaded on additional requests. I have traced through my code & confirmed that my control's GetScriptReferences
method is being called.
The following code is an example of a control i cannot get to work in Sharepoint 2010:
HelloWorld.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Reflection;
[assembly: WebResource(Test.Controls.HelloWorld.ClientResource.Release, "text/javascript")]
namespace Test.Controls {
public class HelloWorld : ScriptControl {
public static class ClientResource {
public const string Release = "Test.Controls.HelloWorld.js";
}
protected class HelloWorldScriptDescriptor : ScriptControlDescriptor {
private const string ClientType = "Test.HelloWorld";
public HelloWorldScriptDescriptor(HelloWorld control) : base(ClientType, control.ClientID) { }
}
public static readonly IEnumerable<ScriptReference> ScriptReferences = new ScriptReference[] {
new ScriptReference(ClientResource.Release, Assembly.GetExecutingAssembly().FullName)
};
protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors() {
yield return new HelloWorldScriptDescriptor(this);
}
protected override IEnumerable<ScriptReference> GetScriptReferences() {
return ScriptReferences;
}
}
}
HelloWorld.js
Type.registerNamespace("Test");
Test.HelloWorld = function (element) {
Test.HelloWorld.initializeBase(this, [element]);
};
Test.HelloWorld.prototype = {
initialize: function () {
Test.HelloWorld.callBaseMethod(this, "initialize");
this.get_element().innerHTML = "Hello World";
}
};
Test.HelloWorld.registerClass("Test.HelloWorld", Sys.UI.Control);
HelloWorldUserControl.ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HelloWorldUserControl.ascx.cs" Inherits="Test.HelloWorld.HelloWorldUserControl" %>
<%@ Register TagPrefix="test" Namespace="Test.Controls" Assembly="$SharePoint.Project.AssemblyFullName$" %>
<test:HelloWorld ID="testHelloWorld" runat="server" />
This exact code works in an non-SharePoint ASP.NET web application.
I appreciate your time.