By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,330 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,330 IT Pros & Developers. It's quick & easy.

Finding user controls dynamically

P: n/a
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

Apr 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Take a look at the FindControl() method of the Page class.

Apr 4 '06 #2

P: n/a
Hi,

"accyboy1981" <ac*********@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.