Frage

Given this webusercontrol:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Comments.ascx.cs" Inherits="Controls_Fresh_Comments" %>

Hello World!

<asp:PlaceHolder runat="server" ID="Javascript">
    <script>
        alert("Hello!");
    </script>
</asp:PlaceHolder>

And this on my master page:

<asp:ContentPlaceHolder runat="server" ID="JSContent"/>

How can I make the Javascript content in the webusercontrol render into the JSContent content placeholder?

For good practise and performance, I wish to have control over where the Javascript is rendered on the page. I'm looking for a solution which doesn't require me to modify anything outside of the webusercontrol.

Is this possible?

War es hilfreich?

Lösung

Doh, just started a bounty then immediately figured it out for myself. On the webusercontrol Page_Load add the lines:

Page.Master.FindControl("JSContent").Controls.Add(Javascript);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top