GetScriptReferences does not get called

落爺英雄遲暮 提交于 2019-12-10 16:53:14

问题


I have written a Custom Control that contains the following:

[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
    }
}

Does anyone know of a reason why neither GetScriptDescriptors or GetScriptReferences shouldn't get called? The other parts of the Control works just fine.


回答1:


You need to register the control as a 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);
}


来源:https://stackoverflow.com/questions/1764341/getscriptreferences-does-not-get-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!