GetScriptReferences不会被调用

Kid*_*dan 4 c# asp.net-ajax iscriptcontrol

我编写了一个包含以下内容的自定义控件:

[assembly: System.Web.UI.WebResource("InSysControls.AssignmentLists.AssignmentLists.js", "text/javascript")]
namespace InSysControls
{
    [ToolboxData("<{0}:AssignmentLists ID='AssignmentListsID' runat=\"server\"> </{0}:AssignmentLists>"), ParseChildren(true, "Items")]
    public class AssignmentLists : CompositeDataBoundControl, IScriptControl, INamingContainer
    {
        #region IScriptControl Members

        public IEnumerable<ScriptDescriptor> GetScriptDescriptors() {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor(this.GetType().FullName, this.ClientID);
            yield return descriptor;
        }

        public IEnumerable<ScriptReference> GetScriptReferences() {
            yield return new ScriptReference("InSysControls.AssignmentLists.AssignmentLists.js", "InSysControls");
        }

        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么不应该调用GetScriptDescriptors或GetScriptReferences?Control的其他部分工作得很好.

djd*_*d87 7

您需要将控件注册为ScriptControl:

protected override void OnPreRender(EventArgs e)
{

    if (!this.DesignMode)
    {

        // Link the script up with the script manager
        ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
        if (scriptManager != null)
        {
            scriptManager.RegisterScriptControl(this);
        }

    }

    base.OnPreRender(e);

}

protected override void Render(HtmlTextWriter writer)
{
    if (!this.DesignMode)
    {
        ScriptManager.GetCurrent(this.Page).RegisterScriptDescriptors(this);
    }
    base.Render(writer);
}
Run Code Online (Sandbox Code Playgroud)