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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top