检索动态创建的文本框的文本 [英] Retrieve text of a dynamically created text box

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

问题描述

大家好!

我正在制作一个ASP.net网络应用程序,我动态创建一个文本框。



 i =  0 ; 
public void dynamic ( )
{
TextBox txt = new TextBox();
txt.ID = i.ToString();
txt.MaxLength = Convert.ToInt32(lenth);
TableCell td2 = new TableCell();
td2.Controls.Add(txt);
tr.Cells.Add(td2);
table.Rows.Add(tr);
i ++;
}
panel1.Controls.Add(table);



现在我还有一个保存按钮,我需要动态找到文本创建了文本框,以便将其保存在数据库中。

请帮帮我。



提前致谢!



添加代码块以保留格式 - OriginalGriff [/ edit]

代码缩进,语法和拼写,删除了textpeak - Manfred R. Bihy [/ edit]

解决方案

  foreach (控制c   panel1.Controls){
if (c TextBox){
TextBox txt =(TextBox)c;
string str = txt.Text;
}
}







如果您知道你的文本框的ID。

 TextBox txt =(TextBox)panel1.FindControl(txt); 



使用txt.Text。


我将添加另一个可能性。

创建文本框时,可以将处理程序附加到 TextChanged 保持更新变量的事件:



 public void dynamic()
{
TextBox txt = new TextBox();
...
//在这里添加事件处理程序
txt.TextChanged + = new EventHandler(System.EventHandler(this.txt_TextChanged));
...
}
//添加变量以保持文本框内容
string yourText = string.Empty;
//一旦文本框的内容改变就更新变量
private void txt_TextChanged(object sender,EventArgs e)
{
yourText =(sender as TextBox) 。文本;
}





选择您喜欢的解决方案;)


有几种方法去做这个。首先,我建议仅在InIt或Pre_Init函数中创建动态文本框。

现在要检索数据,如果您知道ID,那么您可以直接访问,或者您可以在面板中找到所有控件并找到每个文本框并在保存时获取每个文本框的值

Hello to all!
I am making an ASP.net web application where I dynamically create a textbox.

i=0;
public void dynamic()
{
    TextBox txt = new TextBox();
    txt.ID = i.ToString();
    txt.MaxLength = Convert.ToInt32(lenth);
    TableCell td2 = new TableCell();
    td2.Controls.Add(txt);
    tr.Cells.Add(td2);
    table.Rows.Add(tr);
    i++;
}
panel1.Controls.Add(table);


Now I also have a save button and I need to find the text of the dynamically created text box so I can save it in a database.
Please help me.

Thanks in advance!

[edit]Code block added to preserve formatting - OriginalGriff[/edit]
[edit]Code indentation, grammar and spelling, textspeak removed - Manfred R. Bihy[/edit]

解决方案

foreach (Control c in panel1.Controls) {
  if (c is TextBox) {
    TextBox txt = (TextBox)c;
    string str = txt.Text;
  }
}




Or if you know the IDs of your textboxes.

TextBox txt = (TextBox)panel1.FindControl("txt");


Use txt.Text.


I will add another possiblity.
When you create your textbox, you can attach a handler to the TextChanged event to keep an updated variable:

public void dynamic()
{
    TextBox txt = new TextBox();
    ...
    //add the event handler here
    txt.TextChanged += new EventHandler(System.EventHandler(this.txt_TextChanged));
    ...
}
//add a variable to keep the textbox content
string yourText = string.Empty;
//update the variable as soon as the content of the text box is changed
private void txt_TextChanged(object sender, EventArgs e)
{
    yourText = (sender as TextBox).Text;
}



Choose the solution you prefer ;)


There are several ways to do this. First I would suggest to create your dynamic textboxes at InIt or Pre_Init functions only.
Now to retrieve the data, if you know the ids then you can directly access or you can find all the controls in your panel and find every textbox and get the value for each textbox at the time of saving


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

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