vb.net 如何动态添加 HTML 元素 (ASP.NET)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28367374/
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-09-17 18:46:49  来源:igfitidea点击:

How to add HTML elements Dynamically (ASP.NET)

c#asp.netvb.net

提问by S Nash

I am creating LINKbuttons Dynamically:

我正在动态创建 LINKbuttons:

Lnk1

链路1

Lnk2

链路2

..

..

I want to Dynamically but a text(Or any HTML Element) between them for example:

我想动态但它们之间的文本(或任何 HTML 元素)例如:

Lnk1
Seperator
Lnk2
Sepeperator

Here is my code:

这是我的代码:

 for (int i=0;i<10;i++)
            {
    form1.Controls.Add(lnks[i]);
    Response.Write("Seperator<br>");
}

But in output I get the following picture:

但在输出中我得到以下图片:

enter image description here

在此处输入图片说明

Please let me know how to add one text exactly after one linkButton.

请让我知道如何在一个链接按钮之后添加一个文本。

回答by williamdnapier

Take a look at this approach. You're on the right track, but you need to think of the form itself. The form has a collection property called Controls. If you instantiate controls in your for loop, you can effectively add each control sequentially. First, create your LinkButton instance and assign it's properties. Then, create whatever sort of separator control you would like to use. I used an HtmlGenericControl in this example which allows us to assign a text property (of Separator). Notice I started my loop at 1 instead of zero to match your example ... hopefully this helps.

看看这个方法。您走在正确的轨道上,但您需要考虑表单本身。该表单有一个名为 Controls 的集合属性。如果在 for 循环中实例化控件,则可以有效地按顺序添加每个控件。首先,创建您的 LinkBut​​ton 实例并为其分配属性。然后,创建您想要使用的任何类型的分隔符控件。我在这个例子中使用了一个 HtmlGenericControl,它允许我们分配一个文本属性(分隔符)。请注意,我从 1 而不是零开始我的循环以匹配您的示例......希望这会有所帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 1; i < 11; i++)
        {
            LinkButton linkButton = new LinkButton();
            linkButton.Text = "Lnk" + i;
            linkButton.Click += linkButton_Click;
            form1.Controls.Add(linkButton);

            HtmlGenericControl p = new HtmlGenericControl("p");
            p.InnerText = "Separator";
            form1.Controls.Add(p);
        }
    }

    void linkButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://www.stackoverflow.com");
    }
}

回答by Paritosh S.

Try using this as a ref. its working code in my past project.

尝试使用它作为参考。它在我过去的项目中的工作代码。

for (int i = 0; i <= rows - 1; i++)
            {
                HyperLink MyLink = new HyperLink();

                //Set the Hyperlink Text and ID properties.
                MyLink.Text = "YOUR TEXT";
                MyLink.ID = "Link Id";
                // Add a spacer in the form of an HTML <br /> element.
                Panel1.Controls.Add(new LiteralControl("<br />"));
                Panel1.Controls.Add(MyLink);
            }