Minify RadScriptBlock Content

4 posts, 0 answers
  1. ADe
    ADe avatar
    42 posts
    Member since:
    Jul 2008

    Posted 22 Feb 2010 Link to this post

    I have a server side method that given a string of JScript as an input returns a compressed "minified" version.

    I want to run some code after the page has loaded to find all RadCodeBlocks and RadScriptBlocks and minify their content before the page is finally rendered in the browser.

    I can do this for some RadScriptBlocks but not all of them.

    Some of them simply appear to have no sub controls at all and so I can't get what they currently contain in order to minify them.

    I call something similar to the following in every page's Page_PreRender,

     
            public static void MinifyAllPageJScript(ControlCollection PageControls)  
            {  
                foreach (Control pageControl in PageControls)  
                {  
                    if ((pageControl as RadScriptBlock) != null ||  
                        (pageControl as RadCodeBlock) != null)  
                    {  
                        foreach (Control script in pageControl.Controls)  
                        {  
                            if ((script as LiteralControl) != null)  
                            {  
                                (script as LiteralControl).Text = Common.CompressJS((script as LiteralControl).Text);  
                            }  
                        }  
                    }  
                    MinifyAllPageJScript(pageControl.Controls);  
                }  
            } 

    Like I say, it successfully finds all the RadScriptBlocks and RadCodeBlocks on every page but some of them have Controls.Count = 0 and so I can't get at their content to correct it. Have tried calling my MinifyAllPageJScript from various event methods but with no luck.

    Little help?

    Regards
    ADe
  2. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 23 Feb 2010 Link to this post

    The reason the controls collection contains no controls is because the script tags inside the RadScriptBlock/RadCodeBlock are not server controls, you would need to set the script tags to runat="server" and then they should show up in the controls collection.

    I hope that helps.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ADe
    ADe avatar
    42 posts
    Member since:
    Jul 2008

    Posted 23 Feb 2010 Link to this post

    Doh! Of course. Thanks.

    Problem is, if I do that then it complains about my use of the $find command saying that $ is an invalid character.
  5. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 23 Feb 2010 Link to this post

    I believe it does that because when you set the script tag to runat="server", it's expecting .NET code to be inside of it. It pretty much turns it into a .NET code block.

    So I don't know how you would resolve something like that.
Back to Top