在fo​​r循环中动态创建一个文本框 [英] Creating a Textbox dynamically in for loop

查看:97
本文介绍了在fo​​r循环中动态创建一个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态创建一个表,并把文本框在里面。在这里,在以下code,我试图创建一个文本框为每个K A不同的名称。但只有k的最后一个值是越来越显示在文本框中。我想知道我怎么可以给这样所有显示的名称为文本框。

 的for(int k = 0; K< tblCols; k ++)
{
    TableCell的TC =新的TableCell();
    文本框txtCompanyName =新的TextBox();
    txtCompanyName.Text = dsmissing.Tables [0] .Rows [tblCols- 1] [NewCompanyName]的ToString();
tc.Controls.Add(txtCompanyName);
}


解决方案

替换该行

  dsmissing.Tables [0] .Rows [tblCols- 1] [NewCompanyName]的ToString();

通过

  dsmissing.Tables [0] .Rows [K] [NewCompanyName]的ToString();

我假设你像下面谈论ID。

 的for(int k = 0; K< tblCols; k ++)
{
    TableCell的TC =新的TableCell();
    文本框txtCompanyName =新的TextBox();
    。txtCompanyName.Text = dsmissing.Tables [0] .Rows [K] [NewCompanyName]的ToString();
    。txtCompanyName.ID = Guid.NewGuid()的ToString(N);
    tc.Controls.Add(txtCompanyName);
}

I was trying to create a table dynamically and put textboxes in it. Here in the following code, i was trying to create a textbox with a distinct name for each k. But only the last value of k is getting displayed in the textbox. I was wondering how i could give a name to Textbox so all are shown.

for (int k = 0; k < tblCols; k++)
{
    TableCell tc = new TableCell(); 
    TextBox txtCompanyName = new TextBox();
    txtCompanyName.Text = dsmissing.Tables[0].Rows[tblCols- 1]["NewCompanyName"].ToString();
tc.Controls.Add(txtCompanyName);
}

解决方案

Replace this line

dsmissing.Tables[0].Rows[tblCols- 1]["NewCompanyName"].ToString();

With

dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();

I assume you are talking about ID like below.

for (int k = 0; k < tblCols; k++)
{
    TableCell tc = new TableCell(); 
    TextBox txtCompanyName = new TextBox();
    txtCompanyName.Text = dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();
    txtCompanyName.ID = Guid.NewGuid().ToString("N");
    tc.Controls.Add(txtCompanyName);
}

这篇关于在fo​​r循环中动态创建一个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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