I have done a lot of reading regarding the recursive issue with FindControl. I have a FormView with an <EditItemTemplate> and all I want to do is add javascript attributes to the form control like this:
smsTxt.Attributes.Add("onkeydown", "backspacerDOWN(this,event);");
My goal is to use JavaScript with the ASP.NET controls. I found some code on another forum to do the recursive lookup and here it is:
Expand|Select|Wrap|Line Numbers
- public static class Utils
- {
- public static Control FindControl(this Control root, string id, bool recurse)
- {
- if (!recurse)
- {
- return root.FindControl(id);
- }
- System.Web.UI.Control controlFound;
- if (root != null)
- {
- controlFound = root.FindControl(id);
- if (controlFound != null)
- {
- return controlFound;
- }
- foreach (Control c in root.Controls)
- {
- controlFound = c.FindControl(id, true);
- if (controlFound != null)
- {
- return controlFound;
- }
- }
- }
- return null;
- }
- }
Expand|Select|Wrap|Line Numbers
- TextBox smsTxt = FormView1.FindControl("SMSTextBox", true) as TextBox;
- if (smsTxt != null)
- {
- smsTxt.Attributes.Add("onkeydown", "backspacerDOWN(this,event);");
- smsTxt.Attributes.Add("onkeyup", "backspacerUP(this,event);");
- }
Can someone help me with this? I sure appreciate it!!
Thank you all.