Retrieving ASP.NET System.Web.UI.WebControls.PlaceHolder Content

I have a server control that has a PlaceHolder, which is InnerProperty. In the class when rendering, I need to get the text / HTML content that should be in PlaceHolder. Here is an example of what the external code looks like:

<tagPrefix:TagName runat="server">
    <PlaceHolderName>
      Here is some sample text!
    </PlaceHolderName>
</tagPrefix:TagName>

All this works fine, except that I don’t know how to get the content. I do not see any rendering methods open by the PlaceHolder class. Here is the code to manage the server.

public class TagName : CompositeControl
{
    [TemplateContainer(typeof(PlaceHolder))]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public PlaceHolder PlaceHolderName { get; set; }

    protected override void RenderContents(HtmlTextWriter writer)
    {
       // i want to retrieve the contents of the place holder here to 
       // send the output of the custom control.
    }        
}

Any ideas? Thanks in advance.

+3
source share
2 answers

, . , Generics, "" text/html.

    private static string RenderControl<T>(T c) where T : Control, new()
    {
        // get the text for the control
        using (StringWriter sw = new StringWriter())
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            c.RenderControl(htw);
            return sw.ToString();
        }
    }
+1

. - , PlaceHolder. , :

string s = this.PlaceHolderName...

Intellisense, . PlaceHolder HtmlTextWriter:

   StringWriter sw = new StringWriter();
   HtmlTextWriter htw = new HtmlTextWriter(sw);
   this.PlaceHolderName.RenderControl(htw);
   string s = sw.ToString();
+4

Source: https://habr.com/ru/post/1781868/


All Articles