Solved

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

Posted on 2007-04-03
4
175 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
Comment Utility
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
Comment Utility
Try to use FindControl method of the controls' container.
0
 
LVL 7

Expert Comment

by:mem99999
Comment Utility
Oops, one minute difference :-)
0
 

Author Comment

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

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now