473,782 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FindControl issue (does not find the control)

3 New Member
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:

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 2054
Plater
7,872 Recognized Expert Expert
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
7783
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 regularly. Has anyone seen this before? It's pretty aggravating to have to iterate through the controls in each grid cell to find the ones I need, especially since finding those cells is not always easy. Here's my ItemDataBound() handler: private void...
1
12063
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
23302
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, how would I reference the following BoundField ("NTAccount") in the Sub dvDetail_PreRender sub? Any help would be appreciated... <asp:BoundField DataField="NTAccount" HeaderText="NT Account" />
2
3480
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 control... Let's say the control I want to find is "myControl". So I call e.theObject.FindControl("myControl"); It always returns null and I know the name of the control is good (that's the ID I gave it, is it possible it has changed because it's...
4
5679
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 (FileUpload)FormView1.FindControl(FileUpload1).FileName doesn't work. The purpose is to insert a graphic in the InsertItemTemplate section of the form. I'm using the expample that's in the online help for the post back. protected void Page_Load(object sender, EventArgs e)
4
2223
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 links in the Form View. The below code is what I've been trying to use, and I've tried it in various events with no luck. I need some help. Control btn1 = this.fvCapture.FindControl("EditButton"); Control btn2 =...
15
3919
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
11012
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 to iterate thru the gridview's rows, examine the databound controls, then perform a database action. for some reason, i can't find the controls. i have a two templates: one that uses a label the other uses a textbox to display data. when the...
4
2986
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 = Page.Master.FindControl("CenterPanelContent $ItemBuilderWizard $StepNavigationTemplateContainerID $StepNavFinalStepButton") as Panel;
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.