FindControl issue (does not find the control)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ptcoder
    New Member
    • Oct 2008
    • 3

    FindControl issue (does not find the control)

    Hello everyone.

    I have done a lot of reading regarding the recursive issue with FindControl. I have a FormView with an <EditItemTempla te> and all I want to do is add javascript attributes to the form control like this:
    smsTxt.Attribut es.Add("onkeydo wn", "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:

    Code:
    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;
        }
    }
    I have my code to add the attributes in the Page_Load:
    Code:
    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);");
             }
    I am not getting any errors, but it does not add the Attributes to the control. I don't think it is actually finding the Textbox control. I wrapped the new FindControl in a Class because I was getting an error. Not sure if I even did that right. I am pretty new to .NET programming.

    Can someone help me with this? I sure appreciate it!!
    Thank you all.
    Last edited by Curtis Rutland; Oct 13 '08, 04:12 PM. Reason: added code tags -- please surround your code with [CODE] and [/CODE]
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If you were getting an error, you should check in to see why you were getting that error, instead of just hiding it.

    Have you set a breakpoint and stepped through to see if the control is found?

    Comment

    Working...