0

How can i replace a ascx control to a string tag in html ?

I want to create a dynamic html template.

This is my html

<div id="Detail_Container">

   <div>Gallery 1</div>

    <load_gallery>G6</load_gallery>

   <div>Gallery 2</div>

   <load_gallery>G7</load_gallery>

</div>

I tryied this : Control is rendering but my images doesnt load into the control.

            StringBuilder sb = new StringBuilder();
            HtmlTextWriter textWriter = new HtmlTextWriter(new StringWriter(sb));
            UserControl objControl = (UserControl)LoadControl(inControlPath);
            objControl.GalleryId = "G5";

            if (objControl != null)
                objControl.RenderControl(textWriter);

            string controlstr = tw.ToString();

1 Answer 1

1

You can not just add usercontrol as string to html because it's rendered and processed on server. But you can place a placeholder on your page and then add control to this palaceholder.

Here is sample code how you can load and add usercontrol at runtime:

  //here I am loading usercontrol from ascx file...
  WfGenericFormUserControl genControl = (WfGenericFormUserControl)LoadControl("~/UserControls/WfGenericFormUserControl.ascx");

  //FormPlaceHolder is placeholder inside target page
  FormPlaceHolder.Controls.Add(genControl);

GenControl control will now be shown on the page.

2
  • yes but how can add this placeholder correct point dinamically ? I mean can i replace "<load_gallery>G6</load_gallery>" with formplaceholder ?
    – Mennan
    Oct 4, 2012 at 19:21
  • Yes you can produce all html code at runtime and then use following command for injecting inside your placeholder: placeholder.Controls.Add(new LiteralControl("<div class='myclass'>some html produced at runtime</div>")); Oct 4, 2012 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.