Concatination

  • Thread starter Thread starter Andrew Banks
  • Start date Start date
A

Andrew Banks

How wold I loop through a set of buttons on a web form and change some
properties?

I have some buttons named as follows

btn1User
btn2User
btn3User
btn4User
btn5User

I need to change a few of their properties in a loop and am having problems
referencing them with the value of the int used in the loop. For example (n
is the int used in the loop)

Idea 1
btn+n+User.Visible

Idea 2
string theButton = "btn"+n.ToString()+"User"
theButton.Visible
 
to do that kind of trick you need to
A) use Reflection (not recommended, but look it up)
B) (better) add the buttons to a collection or mark them somehow - e.g.
add them to a collection and iterate it:
init()
{
ht = new Hashtable();
ht.Add(btn1User);
ht.Add(btn2User);
}

..
..
..

foreach(Button b in ht)
b.Visible = false;

C) (best, I guess) bind the properties of the buttons to a single
datasource.

Uri
 
Option D)
Use the FindControl method
VB Pseudocode follows...

Function GetButtonControl(num) as Button
DIM ctl as Button
ctl = FindControl("Btn" + Trim(Cstr(num)) + "User")
return ctl
END Function

GetButtonControl(3).Visible = false
 
Back
Top