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 6924981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:52+00:00 2026-05-27T10:43:52+00:00

I am trying the following: public partial class PopUps : System.Web.UI.Page { public string

  • 0

I am trying the following:

public partial class PopUps : System.Web.UI.Page {

    public string GetContentFromPlaceHolder(string strContentId) {

        HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter());

        plcPlaceHolderToGet.RenderControl(writer);
        string strHtml = writer.InnerWriter.ToString();
        return strHtml;

    }

    public static string GetContent() {

        PopUps puToUse = new PopUps();
        string strHtml = puToUse.GetContentFromPlaceHolder();
        return strHtml;

    }

}

However, when I run the static method GetContent from another page I get this error:

object reference not set to an instance of an object

on the line

plcPlaceHolderToGet.RenderControl(writer);

The ID of the control is correct, it just seems like I need to run some kind of instantiation of the controls on the Page before I can get the HTML of one of the controls.

What am I missing?

The design view of the page looks like this

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PopUps.aspx.cs" Inherits="PopUps" %>

<asp:PlaceHolder runat="server" id="plcPlaceHolderToGet">

    <div>
        Some more HTML...
    </div>

</asp:PlaceHolder>

<asp:PlaceHolder runat="server" id="plcAnotherPlaceHolder">

    <div>
        Some more HTML...
    </div>

</asp:PlaceHolder>

I am trying to do this to have one place to hold all my HTML. HTML, that sometimes needs to be fetched from other C# code and sometimes through an AJAX call.

  • 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-27T10:43:52+00:00Added an answer on May 27, 2026 at 10:43 am

    I solved it like this:

    HtmlBits.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HtmlBits.aspx.cs" Inherits="HtmlBits" %>
    <%@ Register TagPrefix="testUC" TagName="PopupContent" Src="~/usercontrols/HtmlBitsContent.ascx" %>
    <testUC:PopupContent id="popupMain" runat="server" />
    

    The two only methods of HtmlBits.aspx.cs:

    public static string GetHtml(string strContentId, NameValueCollection args = null) {
    
        string strHtml = "";
    
        // Find the requested Placeholder in the usercontrol
        Page pageTemp = new Page();
        HtmlBitsContent popupContentControl = (HtmlBitsContent)pageTemp.LoadControl(Server.MapPath("/usercontrols/HtmlBitsContent.ascx"));
        popupContentControl.ExtraArguments = args;
        PlaceHolder plcHolder = (PlaceHolder)popupContentControl.FindControl("plc" + strContentId);
    
        // Could the Placeholder be found?
        if (plcHolder != null) {
    
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            Html32TextWriter hw = new Html32TextWriter(sw);
    
            plcHolder.RenderControl(hw);
            strHtml = hw.InnerWriter.ToString();
    
        }
    
        return strHtml;
    
    }
    
    public static void ShowHtml(string strContentId, NameValueCollection args = null) {
    
        string strHtml = GetHtml(strContentId, args);
        HttpContext.Current.Response.Write(strHtml);
    
    }
    

    HtmlBitsContent.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HtmlBitsContent.ascx.cs" Inherits="HtmlBitsContent" %>
    
    <!-- HtmlBit1 -->
    <asp:PlaceHolder runat="server" id="plcHtmlBit1">
        <div>
        Some html
        </div>
    </asp:PlaceHolder>
    
    
    <!-- HtmlBit2 -->
    <asp:PlaceHolder runat="server" id="plcHtmlBit2">
        <div>
        Some other html
        </div>
    </asp:PlaceHolder>
    

    The backend of HtmlBitsContent.ascx:

    public NameValueCollection ExtraArguments = null;
    
    protected void Page_Load(object sender, EventArgs e) {
    
        string strContentId = Request.Querystring("contentid");
    
        PlaceHolder plcHolderToGet = (PlaceHolder)this.FindControl("plc" + strContentId);
        string strHtml = "";
    
        if (plcHolderToGet != null) {
    
            HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter());
            plcHolderToGet.RenderControl(writer);
            strHtml = writer.InnerWriter.ToString();
    
        }
        Response.Write(strHtml);
        Response.End();
    
    }
    
    public string GetArg(string strKey) {
    
        string strArgumentValue = "";
    
        strArgumentValue = Request.Querystring(strKey);
    
        if (ExtraArguments != null) {
            if (ExtraArguments[strKey] != null) {
                strArgumentValue = ExtraArguments[strKey];
            }
        }
    
        return strArgumentValue;
    }
    

    Now the different HTML bits can be called via code in some other random .cs file:

    HtmlBits.ShowHtml("HtmlBit1");
    

    or

    string strHtml = HtmlBits.GetHtml("HtmlBit1");
    

    However the HTML bits can also be fetched via AJAX in some random JavaScript-file (here shown with jQuery):

    $.ajax({
        url : '/htmlbits.aspx?contentid=HtmlBit1',
        success : function(responseHtml) {
            $('#containerElement').html(responseHtml);
        }
    });
    

    Please note that all of the code above is only to give a general idea of what I did.
    It’s not plug n’ play but definitely useful if someone else needs to maintain some small HTML snippets and fetch them from several different places.
    It’s even possible to input some arguments to show in the HTML through the GetArg method.

    Enjoy 🙂

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

Sidebar

Related Questions

Greetings, I'm trying to download a web page with the following code: public partial
using System; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { public void
I have the following page public partial class GenericOfflineCommentary : OfflineFactsheetBase { } where
I am trying to add a label the following way public partial class _base
I am trying to do the following: public class character extends entity { public
I am trying to register the following class using the fluent interface: public class
I was just trying to understand delegates using the following code. public class delegatesEx
The following code is the simplified version of my problem: public partial class Form1
I just created a control with the following code: public partial class KindsEditor :
I am trying to encrypt some data with the following code: public static byte[]

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.