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

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

Posted on 2007-04-03
4
177 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
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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:…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

856 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