简体   繁体   English

在asp.net中动态添加html标签

[英]dynamically add html tag in asp.net

I need to add html tags dynamically in asp.net code behind , here is the scenario : 我需要在asp.net代码后面动态添加html标签,这是场景:

List<Product> products = ProductManager.GetProductList();//read data from database

Product is a class containing information I need to show in website such as product name, image,… . Product是一类,其中包含我需要在网站上显示的信息,例如产品名称,图片等。 For adding html code I tried this to show 5 products per page : 为了添加html代码,我尝试了此操作以每页显示5个产品:

String  finalHtmlCodeContainer="";
for(int i=0;i<=5;i++)
{
   String myBox= "<div class=\"box\"> </div>";
   finalHtmlCodeContainer+=mybox;
}
boxWrapper.InnerHtml=finalHtmlCodeContainer;

boxWrapper is a div that would contain our 5 product info.up to now everything is ok but problem appears when insted of "<div class=\\"box\\"> </div>" , myBox contains long characters of html code , the original myBox should include this: boxWrapper是一个div,其中将包含我们的5个产品信息。到目前为止,一切正常,但是在插入"<div class=\\"box\\"> </div>"myBox包含html代码的长字符,原始的myBox应该包含以下内容:

<div class="boxWrapper">
                    <div class="box">
                        <div class="rightHeader">rightHeader</div>
                        <div class="leftHeader">leftHeader</div>

                        <div class="article">
                            <img src="../Image/cinemaHeader.jpg" />
                            <p>
                              some text here <!--product.content-->  
                            </p>
                          </div><!--end of article-->

                        <div class="rightFooter"><span>rightFooter</span>
                            <div class="info">
                                <ul>
                                    <li>item1</li>
                                    <li>item2</li>
                                    <li>item3</li>
                                </ul>
                            </div>
                        </div><!--end of rightFooter-->

                        <div class="leftFooter"><span>leftFooter</span>
                        </div><!--end of leftFooter-->

                    </div><!--end of box-->                 
                </div><!--end of boxWrapper-->

you see if i add producet.atrributes to code snippet above, it would be more messy,hard to debug , has less scalability and... . 你看我是否在上面的代码片段中添加了producet.atrributes ,它会变得更加混乱,难以调试,可伸缩性更差……。 what is your solution? 您有什么解决方案?

It depends on the underlying product. 它取决于基础产品。 If you are using web forms, I'd suggest using a data bound control that supports templating like the ListView or Repeater. 如果您使用的是Web表单,建议您使用一个支持模板的数据绑定控件,例如ListView或Repeater。 It gives you full control over the UI, and also supports reloading the UI in ViewState. 它使您可以完全控制UI,还支持在ViewState中重新加载UI。 In your scenario, you'd have to build the UI on every postback. 在您的方案中,您将必须在每个回发上构建UI。 The ListView or Repeater track it in ViewState. ListView或Repeater在ViewState中对其进行跟踪。

If you are using MVC, well using a helper method can make this easier as you can stick the item template within and then use a foreach and render out the helper. 如果您使用的是MVC,那么很好地使用帮助程序方法可以使此操作更容易,因为您可以将项目模板粘贴在其中,然后使用foreach并渲染出该帮助程序。

If you are talking doing it in JavaScript, then there are templating components for doing the same thing. 如果您正在谈论使用JavaScript进行操作,则可以使用模板组件来执行相同的操作。

I suggest you to Add repeater in your <p> tag , 建议您在<p>标记中添加中继器,

       <p>
        <asp:Repeater runat="server" ID="rpDetail">
          <ItemTemplate>
           <div class="box">
               <%# Eval("YourDataField") %>
           </div>
          </ItemTemplate>
          </asp:Repeater>
        </p>

And make your products list as Repeater's datasourse in code behind . 并在后面的代码中将您的products列为Repeater的数据源。

rpDetail.DataSource = products ;
rpDetail.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM