PDA

View Full Version : [OPEN] [#432] RegisterGlobalScript and MVC/ASPX Web Forms not working



adrianot
Feb 13, 2014, 9:25 AM
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,

Daniil
Feb 13, 2014, 1:23 PM
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>

adrianot
Feb 13, 2014, 3:51 PM
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?

Daniil
Feb 14, 2014, 4:37 PM
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

adrianot
Feb 17, 2014, 1:43 PM
Great, thank you Daniil!