?
Solved

Method to utilize controls dynamically based on param passed in.....(this is possible, no?)

Posted on 2007-04-03
4
Medium Priority
?
180 Views
Last Modified: 2013-12-16
Hi everyone,

I am just curious- is it possible to have a method that takes an string as a parameter which passes in a number (1,2,3,4,etc) that is appended to the control values in order to have the same method work for a number of different controls (which are separated by panels, etc.)?

For instance (I know this won't work- but to get an idea of what I want to know):


((I would have several groups of ASP.NET controls with the same properties, but with names being differentiated by an appended number---->> ie. ddlDrop1, ddlDrop2, ddlDrop3, ddlDrop4......txtNew1, txtNew2, txtNew3, txtNew4, etc.))

   protected void DynamicControlMethod(string inputNum)
        {

            ddlDrop + inputNum + .SelectedIndex = 0;

            txtNew + inputNum + .Text = "something";

            ......etc.

        }


NOTE: I am not actually trying to do anything in the above- I just want to know how I can dynamically use the controls based on the 'inputNum' param. This must somehow be possible in C#, right?


Thanks a bunch in advance to all!!


-colin
0
Comment
Question by:EllsworthAdhesives
[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
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 18844020
You need to use FindControl to get a reference to a control dynamically.  FindControl without UniqueID, needs to have a reference to the parent control, such as a Panel:

Examples:
   Get a reference to a TextBox on the main page:
      TextBox txt = this.FindControl("TextBox1");

   Get a reference to a Label on a panel:
     Label lbl = this.panel1.FindControl("Label1");

   Get a reference to a Button, using an expression:
      int index = 5;
     Button btn = this.FindControl("Button" + index);

Bob
0
 
LVL 7

Expert Comment

by:mem99999
ID: 18844037
Try to use FindControl method of the controls' container.
0
 
LVL 7

Expert Comment

by:mem99999
ID: 18844044
Oops, one minute difference :-)
0
 

Author Comment

by:EllsworthAdhesives
ID: 18844148
Thank you TheLearnedOne- that's exactly what I was looking for.

0

Featured Post

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

765 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