Solved

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

Posted on 2010-11-23
5
360 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 74

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now