Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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

    }
Posted
Updated 8-Jul-16 9:45am
v5
Comments
Karthik_Mahalingam 8-Jul-16 1:38am    
use javascript for this.
F-ES Sitecore 8-Jul-16 4:27am    
Do you call GenerateTable on every postback?
Member 12194656 8-Jul-16 13:19pm    
Hi, Yes I do, but I got 'multiple IDs' message now, please see below. Thanks again.

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;

C#
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;

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


Hope this helps.
 
Share this answer
 
Comments
Member 12194656 8-Jul-16 13:18pm    
Thanks for your reply.
I got an error message about multiple TextBox ID were found because I need to regenerate all the textboxes on every postback.

Multiple controls with the same ID 'ROW_0Col_0' were found. FindControl requires that controls have unique IDs.

Is there anyway to reset all the IDs? Please help, many thanks.
Thank you all.

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900