User Controls-MasterPages and finding TextBox in DataList EditItemTemplate

  • Thread starter Thread starter Hillbilly
  • Start date Start date
H

Hillbilly

Anybody have any sage advice on this frustrating "feature" of ASP.NET? I
have a TextBox in the EditItemTemplate of a DataList I can't seem to find
for some reason that is I believe related to the imfamous complexity and
undocumented vagaries of user controls and MasterPages which are also a type
of user control.
 
Typically, you need to access template controls in one of the item events,
like ItemCreated or ItemDataBound. Event parameters provide a reference to
the item, something like e.Item. To detect EditItem, you need to check
ItemType or check if (e.Item is ListItemType.EditItem). Once you have
detected EditItem, you can use e.ItemFindControl("myTextBox") to get to the
textbox.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
there is nothing really complex about it. html (and asp.net) is a
parent/child (tree) data structure. recursion was invented to handle this
common data structure.

var list = ControlWalker(this, ctl => ctl.ID == "textboxid");
....

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