tags:

views:

297

answers:

1

I have a page composed from multiple partial asp.net mvc views rendered dynamically.
How can I register in the header of the page some javascript files based on what is needed on on each partial view?

+2  A: 

I set up a ContentPlaceHolder in the header of the master page for the view. In the view, I include both the header and body content place holders and add my javascript includes in the place holder corresponding to the header. I typically don't add the javascript include to the partial view itself since I (too) want it to appear in the html head element. I just make sure to include all the javascript files that all of my partials will need in the view that includes them. I will, often, include inline script in the partial view itself though you need to be careful with this if the partial can be embedded multiple times.

View Code:

 <asp:ContentPlaceHolder ID="headerContent"
                         ContentPlaceHolderID="Header"
                         runat="server">
     ... javascript includes go here...
 </asp:ContentPlaceHolder>

 <asp:ContentPlaceHolder ID="bodyContent"
                         ContentPlaceHolderID="Body"
                         runat="server">
    ... body content goes here...

    <%  Html.RenderPartial( "PartialView1", Model, ViewData ); %>
    <%  Html.RenderPartial( "PartialView2", Model, ViewData ); %>
 </asp:ContentPlaceHolder>
tvanfosson
What happens when, in an Ajax request, only the partial view is re-rendered? In my case, the inline javascript that needs the javascript file referenced stops working. I also tried to reference it inside the partial view but with no success! Do you have any idea about how to do this?
Fabio Milheiro
You might want to use live() handlers for your events. The live handlers are added at the document level so they are not removed when the matching elements are replaced in the DOM. Alternatively, you can re-add them using the complete/success callbacks that you can register in AjaxOptions.
tvanfosson
I created a function:function functionName(sender, args) {}and on OnComplete callback I call it (OnComplete = "functionName"). The problem is that the request is not an Ajax request anymore. Why? Do I have to do anything else? I also tried registering a startup script like this: string script = "<script language='JavaScript'>alert('Hey!')<" + "/script>";Page.RegisterStartupScript("functionName", script); in the page load method but with no results! The second alternative suggested by you seems very simple, but I am probably missing something important! Should I go with the live() handlers?
Fabio Milheiro
I suspect you've introduced a javascript error. When there is a javascript error on the page, the ajax form submission reverts to a normal form submission. Have you checked with Firefox/Firebug or IE8 Developer tool to see if there are javascript errors?
tvanfosson
I guess you are referring to the case where I use the OnComplete call back function. The problem I see now is that it is the same function that I use when the partial view is firstly rendered and it works perfectly before the form submission. Anyway, I will check it tomorrow when I get back here so I can get this working and update you about the problem status. Thank you tv!
Fabio Milheiro