C# 在asp.net中添加html元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9484023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:36:15  来源:igfitidea点击:

adding html elements with in asp.net

c#asp.net

提问by samy

I need some help figuring out how to do something.

我需要一些帮助来弄清楚如何做某事。

I got this gallery (galleriffic) and some images that are store in Flicker.com so I used the flicker api to get the images but still add them manually to test the gallery.

我得到了这个画廊(画廊)和一些存储在 Flicker.com 中的图像,所以我使用了闪烁 api 来获取图像,但仍然手动添加它们以测试画廊。

Now I'm looking for a good way to insert the images into the html after I get them with the flicker api.

现在,我正在寻找一种在使用闪烁 api 获取图像后将图像插入 html 的好方法。

I found this htmltextwriter and used the function

我找到了这个htmltextwriter并使用了该功能

Response.Write(GetDivElements());

Response.Write(GetDivElements());

but this is adding the div's on the top of the html and not inside the body tag.

但这是在 html 顶部而不是在 body 标签内添加 div。

my qustions is:

我的问题是:

  1. is HtmlTextWriter writer = new HtmlTextWriter(stringWriter)a good way to build html tags on the server side?

  2. Is there a better way to add elements to the html other then Response.Write("");?

  1. HtmlTextWriter writer = new HtmlTextWriter(stringWriter)在服务器端构建 html 标签的好方法吗?

  2. 有没有更好的方法来向 html 添加元素Response.Write("");呢?

采纳答案by Justin Pihony

If you are using the older style of asp.net, and not asp.net MVC, then you can just create a div with an id and runat="server". Then you can just write directly to the html.

如果您使用的是旧样式的asp.net,而不是asp.net MVC,那么您只需创建一个带有id 和runat="server" 的div。然后你可以直接写入html。

aspx page

aspx页面

<div id = "DivINeedToAddStuffTo" runat="server" />

aspx.cs

aspx.cs

DivINeedToAddStuffTo.InnerHtml = GetDivElements();   

Also, I do not see anything wrong with using HtmlTextWriter to create your Html markup

另外,我认为使用 HtmlTextWriter 创建 Html 标记没有任何问题

回答by Jason Lattimer

You might try looking into Placeholders. That way you can create an instance of an image control and then add it your your placeholder.

您可以尝试查看Placeholders。这样您就可以创建图像控件的实例,然后将其添加为您的占位符。

Image myImg = new Image(); 
myImg.ImageUrl = "MyPicture.jpg"; 
myPlaceholder.Controls.Add(myImg);

回答by Dave Becker

Here is what I do when I need to add mark-up.

当我需要添加标记时,这是我所做的。

in my page

在我的页面

<asp:PlaceHolder ID="MyPlaceholder" runat="server"></asp:PlaceHolder>

in my code behind

在我后面的代码中

MyPlaceholder.Controls.Add(new Literal() { Text="<div>some markup</div>"});

I do it this way because:

我这样做是因为:

1) you can put the PlaceHolder where you need it in the structure of your page

1) 您可以将 PlaceHolder 放在页面结构中需要的位置

2) by adding a Literal at runtime to the Controls collection prevents ViewState getting bloated with it's contents.

2) 通过在运行时向 Controls 集合添加 Literal 可防止 ViewState 因其内容而变得臃肿。

回答by Paulie Waulie

You should be able to use the ASP literal control:

您应该能够使用 ASP 文字控件:

        foreach (var item in items)
        {
            Literal literal = new Literal();
            literal.text = item.html; //Assuming the item contains the html.
            MyPlaceholder.Controls.Add(literal);
        }

You could have that code before the page has rendered.

您可以在页面呈现之前拥有该代码。

Hope that helps

希望有帮助

Paul

保罗

EDIT

编辑

Sorry, I think I was mistaken, I thought you had the html with the link to the image(s) and not the actual image itself, Justin's answer would suit you if that's the case.

抱歉,我想我弄错了,我以为您拥有带有图像链接的 html,而不是实际图像本身,如果是这种情况,贾斯汀的回答会适合您。

回答by demp

    var ctrl = new WebControl(HtmlTextWriterTag.Div) { CssClass = "SomeClass" };
    ctrl.Attributes["style"] = "float:left;display:inline-block;margin:3px;";

    ctrl.Controls.Add(new Image
    {
        ImageUrl =
        Page.ResolveUrl("image path here")
    });

    this.Controls.Add(ctrl);