2011-01-19 5 views
2

のScriptManagerが私のhtmlでAXDのスクリプト参照の多くを出力している私のasp.net:ASPに登録されているスクリプト参照のリストを取得します:のScriptManager

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

今私は、これらの暗号化されたクエリ文字列は、スクリプトの参照を表すことを認識し、例えばアセンブリと名前。私は、登録されたスクリプトのリストを取得したいので、私はそうのように私の複合スクリプトに追加することができます。

<asp:ScriptManager runat="server"> 
    <CompositeScript> 
    <Scripts> 
     <asp:ScriptReference Name="Something.js" Assembly="System.Something" /> 
    </Scripts> 
    </CompositeScript> 
</asp:ScriptManager> 

私が登録したスクリプトのリストで取得するには、次のコードを試してみたが、それは常にゼロを返します。何らかの理由でスクリプトを実行すると、何が間違っていますか?

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); 
} 
+0

私はこれまでこれを見つけました... http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=13356 – Chris

答えて

2

私は、トリックを行い、私は私のScriptManagerに追加する必要があるスクリプト参照のリストを出力する(http://aspnet.codeplex.com/releases/view/13356)に触発され、このコードを、書いOK:

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()); 
} 

だけでそのコードを置きますページクラスを開き、 'string debug = ...'行にブレークポイントを設定して、必要なスクリプトを確認します。 甘い。