如何动态添加HTML元素(ASP.NET) [英] How to add HTML elements Dynamically (ASP.NET)

查看:52
本文介绍了如何动态添加HTML元素(ASP.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建LINK按钮:

Lnk1

Lnk2

..

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

  Lnk1分隔符Lnk2分隔符 

这是我的代码:

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

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

请让我知道如何在一个linkBut​​ton之后恰好添加一个文本.

解决方案

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

 使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Web;使用System.Web.UI;使用System.Web.UI.HtmlControls;使用System.Web.UI.WebControls;公共局部类_Default:System.Web.UI.Page{受保护的void Page_Load(对象发送者,EventArgs e){对于(int i = 1; i< 11; i ++){LinkBut​​ton linkBut​​ton =新的LinkBut​​ton();linkBut​​ton.Text ="Lnk" + i;linkBut​​ton.Click + = linkBut​​ton_Click;form1.Controls.Add(linkBut​​ton);HtmlGenericControl p =新的HtmlGenericControl("p");p.InnerText =分隔符";form1.Controls.Add(p);}}无效的linkBut​​ton_Click(对象发送者,EventArgs e){Response.Redirect("http://www.stackoverflow.com");}} 

I am creating LINKbuttons Dynamically:

Lnk1

Lnk2

..

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

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:

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

解决方案

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.

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");
    }
}

这篇关于如何动态添加HTML元素(ASP.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆