Reputation: 4497
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();
Upvotes: 0
Views: 1941
Reputation: 6805
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.
Upvotes: 1