OneCleverMonkey
OneCleverMonkey

Reputation: 422

ASP.Net MVC3 Populating Master Page Sections from UserControl/PartialView

Pessimistic as to whether this is even possible, but what I'd like to do is populate a ContentPlaceholder on the Master Page from a User Control on the View.

More specifically, I would like to put a block in my master page like:

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        <asp:ContentPlaceHolder ID="StartupScript" runat="server" />
    });
</script>

Then in my page, I add my user control like:

<%Html.RenderPartial("MyUserControl");%>

Then have MyUserControl put some javascript that it wants to run at startup into the StartupScript placeholder on the master page, like:

<div id="MyUserControl"></div>

<Content ID="StartupScriptContent" ContentPlaceholderID="StartupScript" runat="server">
    $("#MyUserControl").load(...);
</Content>

In case it isn't obvious, the javascript isn't the actual use case. In case anyone is curious, I'd like to initialize the DynaTree jQuery plugin when the DOM loads, only if the UserControl is on the p

Upvotes: 0

Views: 710

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40160

A Control (including an ASCX partial in MVC) is simply rendered where ever you tell it to be rendered in the parent page/control markup.

So, if you want the output of a control to end up going into a ContentPlaceholder, wrap the tag for it in the page with the Content tag. But it's not valid to put Content tags directly in controls/partials.

I suspect there may be another way to do what you are thinking of, though; maybe someone else will have a brain fart regarding that, or maybe you could elaborate on why you need it.

Upvotes: 1

Related Questions