Accessing dynamically created components in C#

D

drakuu

All,

I created dynamically part of a table and its components such as text
boxes etc...
As you can see in the example below I created txtAddress textbox...
Everything works perfectly until the point where I need to save the
textbox input (normally I would use txtAddress.Text). But in my
btnContinue_Click class I can't because It's out of the scope.
Question: How can I access the dynamically created textboxes and save
the inputs?

public void Address()
{
.....
foreach (DataRow dataRow in objDataTable_address.Rows)
{
TableRow tr0 = new TableRow();
TableCell tc0 = new TableCell();
TextBox txtAddress = new TextBox();
txtAddress.ID = dataRow["AddressID"].ToString();
txtAddress.Width = 250;
tc0.Controls.Add(txtAddress);
txtAddress.Text = dataRow["Address"].ToString(); ........

protected void btnContinue_Click(object sender, EventArgs e)
{
param = command0.Parameters.Add("@FirstName",
SqlDbType.VarChar,25);
param.Direction = ParameterDirection.Input;
param.Value = txtFirstName.Text;


Thanks for all and any help,
draku
 
C

Cor Ligthert [MVP]

Drakuu,

You know at the click event that sender is the textbox.
The only thing you have to do is to cast it.

((TextBox) sender).Text

I hope this helps,

Cor
 
D

dehranph

hi drakku,

sir cor ligthert suggestions is one way to do it. But i also want you
to know that dynamically created controls dont have viewstate in them
which may give some other headaches. As much as possible, do it
declaratively.


best, regards,

rodel e. dagumampan
filipino developer
http://community.devpinoy.org/blogs/dehranph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top