使用参数渲染ascx控件并在asp.net中插入html ?

时间:2022-09-02 12:18:37

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

如何将ascx控件替换为html中的字符串标记?

I want to create a dynamic html template.

我想创建一个动态html模板。

This is my html

这是我的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 个解决方案

#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.

您不能只将usercontrol添加到html中,因为它是在服务器上呈现和处理的。但是您可以在页面上放置一个占位符,然后向这个palaceholder添加控件。

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

下面是如何在运行时加载和添加usercontrol的示例代码:

  //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.

GenControl控件现在将显示在页面上。

#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.

您不能只将usercontrol添加到html中,因为它是在服务器上呈现和处理的。但是您可以在页面上放置一个占位符,然后向这个palaceholder添加控件。

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

下面是如何在运行时加载和添加usercontrol的示例代码:

  //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.

GenControl控件现在将显示在页面上。