如何检索动态文本框的文本然后更新其他C#? [英] How to retrieve a dynamic textbox's text then update other C#?

查看:86
本文介绍了如何检索动态文本框的文本然后更新其他C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在c#asp.net web app中,

我在每个表行的单元格中动态创建一个表和文本框。



我的问题是



1.如何使用textchanged在一个文本框中查找文本基于文本框ID?



2.如何在第一个文本框值的同一行上更新另一个文本框?



我试着整整一天谷歌但没有运气,提前谢谢。



ZD



我的尝试:



Hi,

In c# asp.net web app,
I dynamically create a table and textboxes in cell on each table row.

My question is

1. How can I use textchanged to find the text in one textbox base on the textbox ID?

2. How can I update another textbox on the same row base on the first textbox value?

I tried to google it for the whole day but have no luck, thanks in advance.

ZD

What I have tried:

private void GenerateTable(int numOfColumns, int numOfRows,  Table tb)
    { 

for (int i = 0; i < numOfRows; i++)
            {
                TableRow row = new TableRow();
                row.Width = new Unit("100%");
                

                for (int j = 0; j < numOfColumns; j++)
                {
                    TableCell cell = new TableCell();

                        TextBox tb = new TextBox();

                        // Set a unique ID for each TextBox added
                        tb.ID = "Row_"+ i + "Col_" + j;
                        tb.Text = "";
                            tb.TextChanged += new EventHandler(textBox_TextChanged);

                        cell.Controls.Add(tb);

                    
                    }


tb.Rows.Add(row);
}
}


    private void textbox_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)FindControl("Row_0Col_0");
        Response.Write(tb.Text);
// I cannot even retrieve the text

    }

推荐答案

您好,



您需要设置自动回复属性文本框为True并将事件处理程序绑定到动态生成的文本框。



假设您添加这样的文本框;



Hi,

You will need to set autopostback property of the textbox as "True" and bind an event handler to the dynamically generated textbox.

Suppose you add textbox like this;

TextBox t = new TextBox();
t.ID = i.ToString();
t.TextChanged += new EventHandler(t_TextChanged);
t.AutoPostBack = true;
this.pnl.Controls.Add(t);





然后你为动态生成的文本框添加一个事件处理程序;





Then you add an event handler for the dynamically generated textbox as;

TextBox t = (TextBox)sender;
string textvalue = t.Text;
Response.Write("Textbox with id: " + t.ID + " event called<br>");
</br>





希望这会有所帮助。



Hope this helps.


谢谢大家。



我根据这里的所有建议搞清楚了。祝周末愉快!
Thank you all.

I figure it out base on all the advice here. Have a nice weekend!


这篇关于如何检索动态文本框的文本然后更新其他C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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