开发者

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 cont开发者_开发知识库rol 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜