?
Solved

How to Get the Index of a Control on a Web Form?

Posted on 2010-11-23
5
Medium Priority
?
432 Views
Last Modified: 2012-05-10
After searching the KB, I did not find the answer to this question, but I'll bet the answer is out there some where. I've done this in VB, but cannot determine how to do this in C#.

On a web form, I have many textbox controls. One of the controls is named txtFirstName. In VB, if I were to assign the *name* of this control to a string variable, I could then reference the variable in order to access a control, instead of referencing the control directly. For example:

strControlName = "txtFirstName"
Me(strControlName).Focus()

I would like to know how this is done in C#, either by referencing a variable that stores the control name, or by referencing the index value of a control. The following does not work in C#:

this.Controls[strControlName].Focus();

If I knew the index, maybe I could do something like:

intIndex = <control index value>
this.Controls.IndexOf[intIndex].Focus();
or
this.Controls.IndexOf[this.txtFirstName].Focus();

Thanks
0
Comment
Question by:brian_appliedcpu
5 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 34199620
Use the Find method of the Controls collection:

this.Controls.Find("textBox2", true)[0].Focus();
0
 
LVL 16

Accepted Solution

by:
burakiewicz earned 2000 total points
ID: 34199636
this.FindControl("ControlName").Focus();
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34199666
In lieu of the others answers, which are on point, the reason your approach doesn't work, most likely, is that the control you are searching for is the child of some other control, which may be the child of some control, which may...   ...until you get to the form itself. You could write a recursive function or an iterative loop to find the control, but FindControl() does what it sounds like, so why reinvent the wheel  :)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34199731
I should have read the title :/
Controls.Find is Win forms.
0
 
LVL 2

Author Closing Comment

by:brian_appliedcpu
ID: 34200064
Thank you burakiewicz
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question