473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 <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
  1. public static class Utils
  2. {
  3.     public static Control FindControl(this Control root, string id, bool recurse)
  4.     {
  5.         if (!recurse)
  6.         {
  7.             return root.FindControl(id);
  8.         }
  9.         System.Web.UI.Control controlFound;
  10.         if (root != null)
  11.         {
  12.             controlFound = root.FindControl(id);
  13.             if (controlFound != null)
  14.             {
  15.                 return controlFound;
  16.             }
  17.             foreach (Control c in root.Controls)
  18.             {
  19.                 controlFound = c.FindControl(id, true);
  20.                 if (controlFound != null)
  21.                 {
  22.                     return controlFound;
  23.                 }
  24.             }
  25.         }
  26.         return null;
  27.     }
  28. }
  29.  
I have my code to add the attributes in the Page_Load:
Expand|Select|Wrap|Line Numbers
  1. TextBox smsTxt = FormView1.FindControl("SMSTextBox", true) as TextBox;
  2.         if (smsTxt != null)
  3.          {
  4.               smsTxt.Attributes.Add("onkeydown", "backspacerDOWN(this,event);");
  5.               smsTxt.Attributes.Add("onkeyup", "backspacerUP(this,event);");
  6.          }
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.
Oct 13 '08 #1
1 2035
Plater
7,872 Expert 4TB
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?
Oct 13 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back...
1
by: Michael Murschell | last post by:
If I call Page.FindControl() on an aspx, it finds the control, but if I call it on an ASCX, it does not. Why not? And how would I call it?
5
by: sck10 | last post by:
Hello, I am using the code below to set the values of a DetailsView template field using FindControl. My question is how would you find a control if its a Boundfield control? For example,...
2
by: ThunderMusic | last post by:
Hi, I have a custom WebControl. I fire an event and send, as the EventArg, another WebControl that contains sub controls (ParseChildren(true))... It can contain anything, but I want to find one...
4
by: Dave | last post by:
If you had a FileUpload control inside of a FormView...how would you use FindControl to access the FileUpload properties? Let me just say that...
4
by: Dave | last post by:
I have a web page that I'm trying to toggle between current data and archived data. So far so good. To preserver the integrity of the archived data, I need to disable the Edit, Delete, and New...
15
by: | last post by:
I dynamically create controls (textboxes) in a repeater control. I know their names (eg. TextBox1). How do I find the text of TextBox1 in the Form? FindControl does not seem to work.
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
4
by: Hillbilly | last post by:
Maybe this is or isn't some kind of bug but it sure is goofy and remains a mystery that really has me puzzled for two reasons... // goofy syntax functions as expected... Panel finalStepButton =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.