Dynamically generated control id is different at runtime

  • Thread starter Thread starter mazdotnet
  • Start date Start date
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
 
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)
 
Back
Top