Finding user controls dynamically

  • Thread starter Thread starter accyboy1981
  • Start date Start date
A

accyboy1981

Hi,

I'm creating an asp text box dynamically within an asp table. I know
the id as this is not dynamic (txBxPass). I need to get the text of
this text box when an asp button is pressed. I know that I need to
recursively go through the controls on the page but I'm having troble
with this. I think that the problem is that the control is within
another control (i.e. the text box being in the table). I've created
two methods but neither seem to work.

I call the methods by:
TextBox txtBxPass = getControl(this, "txtBxPass") as TextBox;

where "txtBxPass" is the text box name.

Below I have included both the methods I have created:

private Control getControl(Control parent, string id)
{
System.Web.UI.Control d = new Control();
foreach (Control c in this.Page.Controls)// parent.Controls)
{
if (c.ID == id )
{
d = c;
return d;
}
if (c.Controls.Count > 0)
{
d = getControl(c, id);
}
}
return d;
}

private Control getControl(Control s, string id)
{
foreach (Control ctrlControl in s.Controls)
{
if (ctrlControl.ID == id)
{
return ctrlControl;
}
getControl( ctrlControl, id );
}
return null;
}


Any help would be much appreciated.

Thanks
Simon
 
Hi,

accyboy1981 said:
Hi,

I'm creating an asp text box dynamically within an asp table. I know
the id as this is not dynamic (txBxPass). I need to get the text of
this text box when an asp button is pressed. I know that I need to
recursively go through the controls on the page but I'm having troble
with this. I think that the problem is that the control is within
another control (i.e. the text box being in the table). I've created
two methods but neither seem to work.


You may be able to use the ID of the control, unless that the control is
generated by bindings it will conserve the ID you give it.
You can use FindControl and it will do the recursive search for you.

You said you are dynamically creating the control, how you do this? Remember
that you need to recreate the very same control in the postback as the
dynamic controls are not recreated by the framework
 
Back
Top