动态添加文本框和标签 [英] Dynamically add textboxes and labels

查看:170
本文介绍了动态添加文本框和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者.我有一个aspx页面,我有一个mysql查询,该查询对数据库表中的条目数量进行计数.对于数据库中的每个条目,我需要在页面上添加一个标签和一个文本框.有谁知道如何做到这一点?任何帮助将不胜感激.

Hi i am a beginner programmer. I have a aspx page and i have a mysql query that counts the amount of entries in the table in database. For each entry in the database i need to have a label and a textbox added to the page. Does anyone know how to do this? Any help will be much appreciated.

推荐答案

^ ]

重要的是,请参见
Best suited for you[^]

And importantly, see this[^] also.


您可以看到


您可以使用占位符根据数据库中的数据在运行时创建控件.

使用PlaceHolder ...

像:
You can use Place Holder to create Controls at Runtime according to the Data into your database.

Use PlaceHolder...

like :
string s1 = "your query ";
SqlDataAdapter ad1 = new SqlDataAdapter(s1,con);
DataSet ds1 = new DataSet();
ad1.Fill(ds1);

if (ds1.Tables[0].Rows.Count != 0)
{
    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
    {
        Label l = new Label(); // one label created at Runtime
        Label lbl = new Label(); // second label created at runtime
        l.ID = "l" + i.ToString(); // give ID to the first Label
        l.ForeColor = System.Drawing.Color.Orange;
        lbl.ID = "lbl" + i.ToString(); // give ID to the Second Label

        l.Text = ds1.Tables[0].Rows[i][0].ToString(); // set text to the first label
        lbl.Text = ds1.Tables[0].Rows[i][1].ToString(); // set text to the second label
        PlaceHolder1.Controls.Add(l); // add control to the Placeholder
        PlaceHolder1.Controls.Add(new LiteralControl("<br />")); // use for break one line
        PlaceHolder1.Controls.Add(lbl); // add control to the placeholder
        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
    }

}


这篇关于动态添加文本框和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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