Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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