Dynamically generated control id is different at runtime

M

mazdotnet

Hi all,

I have Repeater dynamically adding textboxes to a placeholder and
connecting it to AjaxControlToolkit.PopupControlExtender which works
fine. I'm adding the textboxes using the following

TextBox tbxQty = new TextBox();
tbxQty.ID = "tbxQty" + id;
phUserInput.Controls.Add(tbxQty);

Now in the OnCommand I want to read the value of the textbox by using
((TextBox)FindControl("tbxQty" + id)).Text which fails because my
textbox automatically because

<input type="text" id="rptAvailableProducts_ctl01_tbxQty0" />
instead of
<input type="text" id="tbxQty0" />

How can I force it to use tbxQty0 for the generated id at runtime
instead of rptAvailableProducts_ctl01_tbxQty0? Or in the OnCommand
event is there any way to extract the value of tbxQty0 from
rptAvailableProducts_ctl01_tbxQty0?

Thanks
Maz
 
B

bruce barker

FindControl() only searches the current naming container. to use
findcontrol, you need to call it on the datarepeater template that hosts
the control. i assume you made a unique id, so you can just do a
recursive search for a control with the id.


var list = ControlWalker(this, ctl => ctl.ID == "tbxQty" + id");


public List<Control> ControlWalker(
Control ctl,
Predicate<Control> matcher)
{
var list = new List<Control>();
if (matcher(ctl)) list.Add(ctl);
for (int i=0; i < ctl.Controls.Count; ++i)
{
var childList = ControlWalker(
ctl.Controls,matcher);
if (childList.Count > 0)
list.AddRange(childList);
}
return (list);
}

-- bruce (sqlwork.com)
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top