简体   繁体   English

附加到具有特定类的 div

[英]Appending to div with specific class

How can I append some html to a div with a specific class name?如何将一些 html 附加到具有特定类名的 div?

frontend:前端:

<div class="school-team-popup"></div>

backend:后端:

StringBuilder _html = new StringBuilder();

_html.AppendFormat("<li>Hi there</li>");

I would like to append _html inside the school-team-popup div.我想在 school-team-popup div 中附加 _html。 How can I do this from the backend?我怎样才能从后端做到这一点?

I'm going to explain the Web Forms way of doing things.我将解释 Web Forms 的工作方式。

If you have some static markup that you want to selectively show/hide on your page, that's generally accomplished by setting the Visible property of the control.如果您希望在页面上有选择地显示/隐藏某些静态标记,通常可以通过设置控件的 Visible 属性来实现。

<%-- This is in your ASPX markup (.aspx) --%>
<asp:Panel runat="server" id="HelloWorldPanel">
  <p>Hello, world!</p>
</asp:Panel>

//This is in your code behind (.aspx.cs)
//hide the panel
HelloWorldPanel.Visible = false;
//show the panel
HelloWorldPanel.Visible = true;

If you're trying to grab dynamic data from some other source and display it on the page, you would declare the markup on your page to show this data, and then bind the data to the markup.如果您尝试从其他来源获取动态数据并将其显示在页面上,则需要在页面上声明标记以显示此数据,然后将数据绑定到标记。 There are many controls you can bind data to .您可以将数据绑定到许多控件

One example of a control you can bind data to is a repeater.可以将数据绑定到的控件的一个示例是转发器。 Repeaters are good when you want tight control over the markup that gets rendered on the page.当您想要严格控制在页面上呈现的标记时,中继器非常有用。 You bind them to some enumerable object such as a List<T> and then it will repeat some markup for each element in the enumerable object.您将它们绑定到某个可枚举对象,例如List<T> ,然后它将为可枚举对象中的每个元素重复一些标记。

//This is in your project somewhere
namespace MyNamespace
{
    public class Product
    {
        public int Id { get; set; }

        public int Name { get; set; }
    }
}

<%-- This is in your ASPX markup (.aspx) --%>
<ul>
  <asp:Repeater runat="server" id="ProductRepeater" ItemType="MyNamespace.Product">
    <ItemTemplate>
      <li><%#: Item.Id %> - <%#: Item.Name %></li>
    </ItemTemplate>
  </asp:Repeater>
</ul>     

//this is in your code behind (.aspx.cs)
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostback)
    {
        List<Product> products = MyDataLayer.GetProducts();
        ProductRepeater.DataSource = products;
        ProductRepeater.DataBind();
    }
}

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

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