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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm a Mercurial developer. Please report problems with our man… May 16, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer WordPress handles canonical redirects for you by sending 301 Moved… May 16, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer I can't say how Twilio does it, but we've been… May 16, 2026 at 6:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a master page that has a usercontrol on the left side of
I have a usercontrol in ASP.Net featuring a button that I use to postback.
I have a UserControl that I want to include in a page multiple times
Im creating a usercontrol which is controled client side, it has an javascript-file attatched
I have some pages that have content that is relevant to both logged in
I'm sure I'm going to have to write supporting javascript code to do this.
I have a problem with a Dev Express component, namely AspxComboBox. My context is
when i try to call a javascript function in a dynamically loaded user control,
Clarification: Put simply, I'd like to put an ASP.NET UpdatePanel inside the info window
I don't know what I'm doing wrong here. I have a ListBox whose DataContext

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.