ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

Last post 11-06-2006 7:49 PM by wyx2000. 13 replies.

Sort Posts:

  • ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    10-30-2006, 5:01 AM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    I put my ScriptManager in a master page, and in my usercontrol, if it need a Preview js, it will add code like this

      ScriptReference sr = new ScriptReference();
      sr.Assembly = "Microsoft.Web.Preview";
      sr.Name = "Microsoft.Web.Resources.ScriptLibrary.PreviewScript.js";
      CurrentSM.Scripts.Add(sr);

    I got the CurrentSM  by searching the ScriptManager control.

    It worked before, after upgrade to Ajax, it doesn't work. The code runs fine, and the count of the Scripts is increased, but the webresource link doesn't be added in output. If I put the same code in the master page where the ScriptManager located, it works.

     Any idea?

     

  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 5:05 AM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    Could someone help here? I hope AJAX team can spend some time to help peole like me who is in big trouble caused by the sudden change of the structure.

     It seems ScriptManager run the private void RegisterScripts() in very early stage, when I add ScriptReference in my usercontrol, it will not register it. When I add OnResolveScriptReference, I can tell it.

     

    Any suggestion?

  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 6:57 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello.

    can you tell us more? is it possible for you to put a page+user control that shows the problem?

    thanks. 

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 12:18 PM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    I have master page, I put ScriptManager in my masterpage , in my usercontrol, I will add scriptreference when the usercontrol need it, the idea is only required scripts will be referenced, I guess this is the right way to di it.

    But after upgrade to Ajax, it doesn't work anymore. I can only dynamically add scriptreference in the master page, when I add scriptreference in my usercontrol, the link doesn't register. As I said in the previous post, it seems ScriptManager register the links before I add the scriptreference.

    My usercontrol could be loaded in any stage of the page cycle, could be right before page render, when ScriptManager register references? it seems it does this quite early, and after it does that, it will not register any other referenced added dynamically.

  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 12:22 PM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    ok, the code

    Masterpage

    <%@ Master Language="C#" Description="layout shell 1" Inherits="LinkLibrary.PageSnippet" %>   
     
    <script runat="server">
    void Page_Init(object sender, EventArgs e)
    {
     
    }

    protected void OnResolveScriptReference(object sender,ScriptReferenceEventArgs e)
    {
     //ErrorHandler.Report(e.Script.Name+":"+e.Script.Path);
    }
    </script>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
     <title></title>
     <link rel="shortcut icon" href="/favicon.ico">
     <lsp:LinkWebPartManager ID="WebPartManager1" Runat="server"></lsp:LinkWebPartManager>
    </head>
    <body>

    <form id="me" runat="server">
     <asp:ScriptManager ID="ScriptManager" runat="server" OnResolveScriptReference="OnResolveScriptReference">
      <Scripts>
      <asp:ScriptReference Assembly="Microsoft.Web.Preview" Name="Microsoft.Web.Resources.ScriptLibrary.PreviewScript.js" />
      <asp:ScriptReference Assembly="Microsoft.Web.Preview" Name="Microsoft.Web.Resources.ScriptLibrary.PreviewDragDrop.js" />
      </Scripts>
     </asp:ScriptManager>
     <asp:ContentPlaceHolder Runat="server" ID="B"/>
    </form>


    </body>
    </html>

     

    usercontrol:

    <%@  Control Language="C#" Inherits="LinkLibrary.LinkControls.LinkControl" %>

    <script runat="server">
    protected void Page_Init(object sender, EventArgs e)
    {
        ScriptReference s0=new ScriptReference();
        s0.Path = CurrentSite.GetSiteObjectUrl("javascripts", "atlas/alstreaming");
        s0.ScriptMode = ScriptMode.Auto;
        ScriptManager.GetCurrent(Page).Scripts.Add(s0);

    }

    ...

  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 6:32 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs
    wyx2000:

    But after upgrade to Ajax, it doesn't work anymore. I can only dynamically add scriptreference in the master page, when I add scriptreference in my usercontrol, the link doesn't register. As I said in the previous post, it seems ScriptManager register the links before I add the scriptreference.



    well, if i recall correctly, the scripts are only added to the page during the prerender event. so adding it before that should work without any problem...
    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 7:20 PM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    Please don't assume.

    A lot of things worked before doesn't work now.

    Do you think my usercontrol will display if I add it after PreRender?

  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 7:37 PM
    • Contributor
      4,482 point Contributor
    • jodywbcb
    • Member since 03-12-2003, 3:52 PM
    • West Seattle,WA
    • Posts 985

    I usually add my scripts on the pre render event myself...and if you read the migration docs they even indicate that will be where the RTM release will handle those.. just not incorporated here and now with current beta...  The issue comes when using stuff from the toolkit - which ignores it regardless where you place it unless you do a bunch of workarounds...So while our friend from Portugal is indeed correct in his assumptions...it will ultimately depend upon how you are creating the controls...  The solid test is to put your js in the ascx or aspx .... if it executes fine there - then you can play around with where you need to place it in code behind...

     

    Unfortunately right now - there is no correct answer as some of us can get away with it some can not... and its really going to depend on your code structure...

     

     

    -- jody
    My Blogs on .Net 2.0 and Ajax
    http://csk.wbcb.com
    http://ArtbyJody.com
  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 7:49 PM
    • Contributor
      4,482 point Contributor
    • jodywbcb
    • Member since 03-12-2003, 3:52 PM
    • West Seattle,WA
    • Posts 985

    I'll also state that you really shouldn't run the following:

     

     <asp:ScriptManager ID="ScriptManager" runat="server" OnResolveScriptReference="OnResolveScriptReference">

     

    UNLESS, you have recoded a extention js inclusion or overriding a .js declaration you know is being imported - you do not need this OnResolveScriptReference="OnResolveScriptReference".  Why?

     

     The resolve deal - is that you are indicating you are overriding something or that there is the possibility there may be two and you need to override...  I'd say that only 1% of all people whom use that need to - that 1% being custom component devs that need to override default behaviors....from the initial code snippett - you are merely loading default scripts that are unique (the js files you are refere3ncing are part of the value add on that are not actually associated with included controls...)...

     

    Not that has anything to do with your issue - just less code is better code and if you do not need it - why question later why it is there?

     

    cheers..

     

     

     

    -- jody
    My Blogs on .Net 2.0 and Ajax
    http://csk.wbcb.com
    http://ArtbyJody.com
  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-02-2006, 8:07 PM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    Hi, I think you misunderstand why I add OnResolveScriptReference in there. I did use it to add any thing, I just add to try what js files are handled by it, if it includes the file I add in the usercontrol. Since in the dll I can tell in RegisterScript, it will call OnResolveScriptReference.

    As in your last post, you are not sure if the add script works in all stages before Render, that is about to be right. What I want to confirm is if that is the case,  my expience is it call RegisterScript before I add my usercontrol, I add my usercontrol in CreateChildControls, and add the ScriptReference in Page_Init of the usercontrol.

    I am not creating a demo page, I had adjusted my site structure to work fine with the CTP, I can not just put the ScriptReference in a special stage, if I can do that, I will just add them in the ScriptManager.

  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-05-2006, 3:30 AM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    Hi, I have to raise this again, I found if the ScriptManager do register before loading my usercontrol, I can not use any AJAX feature

    So the question is :

    1, Will next release change it to make sure that register happens after all other page actions?

    2, Is there a way I can call the register or delay the register?

  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-05-2006, 4:51 AM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810
    Strange, it seems it is easy to solve, Ijust need move the ScriptManager to the last of the form.
  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-05-2006, 8:02 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello.

    can you build a small demo project which reproduces the problem you're having and send it to me? I'm not saying that i'll be able to solve your problem, but having a running demo will sure make it easy...

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: ScriptManager.Scripts.Add doesn't work in usercontrol? is it a bug?

    11-06-2006, 7:49 PM
    • Contributor
      3,349 point Contributor
    • wyx2000
    • Member since 11-25-2002, 6:43 AM
    • Posts 810

    It is easy to express.

    ScriptManager do the register in PreRender. If we put a ScriptManager and a Content control in a Master page, and in the content control, we load control in any PreRender event.  If the Content control is after ScriptManager, the PreRender of ScriptManager will fire before those controls that need to be loaded in PreRender of control in the Content Control.

     

Page 1 of 1 (14 items)