开发者

Get list of script references that are registered with the asp:ScriptManager

My asp.net ScriptManager is outputting lots of AXD script references in my html:

<script src="/ScriptResource.axd?d=B073McDWctL8Kxw1sTGlGNcf...
<script src="/ScriptResource.axd?d=2WiTJxV_YZ2N4lJaAfSKnBVu...
<script src="/ScriptResource.axd?d=jSiywqe6yJ5PAsxeca407Xxb...
...about 4 more ...

Now i realise that those encrypted querystrings represent script references, eg assembly and name. I want to get at that list of registered scripts, so i can add them to my composite script like so:

<asp:ScriptManager runat="server">
  <CompositeScript>
    <Scripts>
        <asp:ScriptReference Name="S开发者_StackOverflowomething.js" Assembly="System.Something" />
    </Scripts>
  </CompositeScript>  
</asp:ScriptManager>

I've tried the following code to get at the list of registered scripts but it always returns zero scripts for some reason, what am i doing wrong?

protected override void Render(HtmlTextWriter output)
{
  var sm = ScriptManager.GetCurrent(this);
  foreach (ScriptReference s in sm.Scripts)
  {
    string debug = s.Assembly + ">" + s.Name + ";" + s.Path;
  }

  base.Render(output);
}


Ok i wrote this code, inspired by ( http://aspnet.codeplex.com/releases/view/13356 ), which does the trick and outputs the list of script references i need to add to my scriptmanager:

protected override void OnInit(EventArgs e)
{
  base.OnInit(e);
  ScriptManager.GetCurrent(this)
    .ResolveScriptReference += new EventHandler<ScriptReferenceEventArgs>(ResolveScriptReferenceHandler);
}

List<string> ScriptRefs = new List<string>();
private void ResolveScriptReferenceHandler(object sender, ScriptReferenceEventArgs e)
{
  ScriptRefs.Add("<asp:ScriptReference Name=\"" + e.Script.Name + "\" Assembly=\"" + e.Script.Assembly + "\" />");
}

protected override void Render(HtmlTextWriter output)
{
  base.Render(output);
  string debug = string.Join("\r\n", ScriptRefs.Distinct());
}

Simply put that code in your Page class and set a breakpoint on the 'string debug=...' line to check which scripts are needed. Sweet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜