Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1022379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:27:24+00:00 2026-05-16T11:27:24+00:00

I have a UserControl that has some javascript I’d like to inject into a

  • 0

I have a UserControl that has some javascript I’d like to inject into a known ContentPlaceHolder.

I was hoping to do something like the following except when I append to add the control to found control I get an exception which says I cannot modify the control collection in the Init, Load or PreRender events:

“UserControl.ascx”


 <%@ Control Language="C#" %>
 <asp:Checkbox runat="server" id="checkBox"/>
 <app:JavascriptInjector runat="server" InjectInto="ScriptPlaceHolder">
      $(function(){ $('#<%= checkBox.ClientID %>').click(function() { ... });
 </script></app:JavascriptInjector>

"JavascriptInjector.cs"

using System;
using System.Diagnostics;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

public class JavascriptInjector : PlaceHolder
{
    public string InjectInto
    {
        get { return this.ViewState["InjectInto"] as string; }
        set { this.ViewState["InjectInto"] = value; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        this.PreRender += this.__PreRender;
    }

    private void __PreRender(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.InjectInto))
        {
            goto performRegularlly;
        }

        var injectInto = this.FindControlRecursively(this.Page);

        if (injectInto == null)
        {
            goto performRegularlly;
        }

        injectInto.Controls.Add(this);

        return;

        performRegularlly:

        Debug.WriteLine("performing regularlly");
    }

    private Control FindControlRecursively(Control current)
    {
        var found = current.FindControl(this.InjectInto);

        if (found != null)
        {
            return found;
        }

        foreach (var child in current.Controls.Cast<Control>())
        {
            return this.FindControlRecursively(child);
        }

        return null;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:27:24+00:00Added an answer on May 16, 2026 at 11:27 am

    I figured it out. The following JavascriptInjector class will work, although I don’t know what the implications are of calling the render method in the PreRender. Also think I may need to do something to figure out which type of HtmlTextWriter that the base application is using.

    “JavaScriptInjector.cs”

    using System;
    using System.IO;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public class JavascriptInjector : PlaceHolder
    {
        private bool performRegularlly;
    
        public string InjectInto
        {
            get { return this.ViewState["InjectInto"] as string; }
            set { this.ViewState["InjectInto"] = value; }
        }
    
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
    
            this.PreRender += this.__PreRender;
        }
    
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.performRegularlly)
            {
                base.Render(writer);
            }
        }
    
        private void __PreRender(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.InjectInto))
            {
                goto performRegularlly;
            }
    
            var injectInto = this.FindControlRecursively(this.Page);
    
            if (injectInto == null)
            {
                goto performRegularlly;
            }
    
            performRegularlly = false;
    
            using (var stringWriter = new StringWriter())
            using (var writer = new HtmlTextWriter(stringWriter))
            {
                base.Render(writer);
    
                writer.Flush();
    
                injectInto.Controls.Add(new LiteralControl(stringWriter.GetStringBuilder().ToString()));
            }
    
            this.Controls.Clear();
    
            return;
    
            performRegularlly: this.performRegularlly = true;
        }
    
        private Control FindControlRecursively(Control current)
        {
            var found = current.FindControl(this.InjectInto);
    
            if (found != null)
            {
                return found;
            }
    
            foreach (var child in current.Controls.Cast<Control>())
            {
                return this.FindControlRecursively(child);
            }
    
            return null;
        }
    }
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some UserControls that I created in ProjectA. I have ProjectB that has
I have a usercontrol that has several public properties. These properties automatically show up
I have created a UserControl that has a ListView in it. The ListView is
I have a Page that has a single instance of a UserControl that itself
I have a UserControl in my Asp.net project that has a public property. I
I have a UserControl that consists of three TextBoxes. On a form I can
I have a usercontrol that is meant to take up the entire form. I
If I have a usercontrol (in Silverlight) that I've written, that uses XAML to
i have a user control that descends from UserControl. When dropped onto the form
I have a user control that is pretty basic. It contains several TextBox controls,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.