Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-23
5
Medium Priority
?
429 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
[X]
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
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 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