[OPEN] [#432] RegisterGlobalScript and MVC/ASPX Web Forms not working

  1. #1

    [OPEN] [#432] RegisterGlobalScript and MVC/ASPX Web Forms not working

    Hello,
    I am trying to download a JS and a CSS on demand using the "MvcResourceManager.RegisterGlobalScript" method on my partial view.

    Partial view:

    <%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl" %>
    
    <script runat="server">
        Protected Sub Page_Load(sender As Object, e As EventArgs)
            ResourceManager.RegisterGlobalScript("~/MyScript.js")
        End Sub
    </script>
    
    <ext:Panel runat="server" Title="MyPartial 2 Panel">
        <Content>
            CONTENT HERE <%=Now %>
        </Content>
    </ext:Panel>
    This is the code used when calling the action:

    Function MyPartial2(containerId As String) As ActionResult
            Dim result As New PartialViewResult With {
                .ContainerId = containerId,
                .RenderMode = Ext.Net.RenderMode.AddTo
            }
    
            Return result
    End Function
    ... the action is called using DirectEvent on the primary page:

    <ext:Button runat="server" Icon="Add" Flat="true">
                <DirectEvents>
                    <Click Url="~/Portal/MyPartial2">
                        <ExtraParams>
                            <ext:Parameter Name="containerId" Value="#{TabContainer}" />
                        </ExtraParams>
                    </Click>
                </DirectEvents>
            </ext:Button>

    All this stuff works on Razor views (for instance on your demo at this address:
    http://mvc.ext.net/#/Dynamic_Partial_Rendering/Add_Tab/

    However, while the RegisterGlobalScript call is success it's not firing the correct javascript basically ignoring the request.

    Could you help us giving us any workaround or solution to this issue?

    Thanks a lot,
    Last edited by Daniil; Feb 14, 2014 at 4:38 PM. Reason: [OPEN] [#432]
  2. #2
    Hi @adrianot,

    Please don't rely on the WebForms page life cycle in an MVC application. That life cycle is specific to WebForms.

    Yes, a RegisterGlobalScript call doesn't work in this case.

    As a solution we recommend this:
    <ext:Panel runat="server" Title="MyPartial 2 Panel">
        <ResourceItems>
            <ext:ClientResourceItem Path="~/MyScript.js" />
        </ResourceItems>
        <Content>
            CONTENT HERE
        </Content>
    </ext:Panel>
  3. #3
    Great, thank you Daniil.

    The reason I am using the Form_Load event to add further the scripts is that actually in our application the list of scripts to add is dynamic (I don't know the name of the scripts in advance, the "model" tells me what to render, ... I should be able to render the scripts dynamically without knowing the name of the script at design time). Is there another way to perform this task using WebForms engine in MVC?
  4. #4
    We can suggest the following solution.
    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
     
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Panel1.ResourceItems.Add(new ClientResourceItem("MyScript.js"));
        }
    </script>
    
    <ext:Panel ID="Panel1" runat="server" Title="MyPartial 2 Panel">
        <Content>
            CONTENT HERE
        </Content>
    </ext:Panel>
    Please don't rely on the WebForms page life cycle in an MVC application. That life cycle is specific to WebForms.
    It might be OK to register resources in Page_Load... We would consider it a bug, that a RegisterGlobalResource call doesn't work in this case. We will attempt to fix it. Created an Issue:
    https://github.com/extnet/Ext.NET/issues/432
  5. #5
    Great, thank you Daniil!

Similar Threads

  1. Replies: 8
    Last Post: Apr 22, 2013, 5:00 PM
  2. [CLOSED] Forms authentication RedirectFromLoginPage not working
    By CarWise in forum 2.x Legacy Premium Help
    Replies: 5
    Last Post: Jun 05, 2012, 1:25 PM
  3. Replies: 2
    Last Post: Feb 01, 2012, 8:53 PM
  4. Replies: 0
    Last Post: Nov 08, 2011, 12:20 PM
  5. Replies: 3
    Last Post: Nov 27, 2008, 12:52 PM

Tags for this Thread

Posting Permissions