Solved

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

Posted on 2007-04-03
4
178 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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:…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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