User Controls-MasterPages and finding TextBox in DataList EditItemTemplate

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.
 
E

Eliyahu Goldin

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
 
B

bruce barker

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)
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top