Solved

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

Posted on 2010-11-23
5
385 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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to call weather service by using asmx? 4 30
MediaHelp 4 23
List<PaisEntity> - show some 1 24
insert value of checklistbox checked 4 27
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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