50 Buttons!

  • Thread starter Thread starter Krunom Ancini
  • Start date Start date
K

Krunom Ancini

Hi,


I have on my page something about 50 buttons that "user" should use only for
loading some session-variables:

click on the button -> session variables loaded

and i wish that after click, clicked button changes his color and thats no
problem. I use:

Button43.Color=red; ... or something like this... and it works

But here is the problem:

if i after clicking on Button43, wish to click on Button12, my Button 12
also become red (and thats good), but Button43 stays red (and thats bad)!
and i wish that Button43 gets his old/unclicked color!

I want this:
I push one button and this button get his "clicked" color, and all other
buttons should get their "unclicked" color independent (!) on their previous
color/state (clicked or unclicked)...


Thanks in advance,

Krunom.
 
Hi,

earlier we had control array concepts , with that we can solve this easily.
But now in .NET you can try the following way for your need.

Loop through the controls in the page.... and if you find a control is a
button and it is not that one you clicked then make it's collour as unclicked
color. This way you can do it.

foreach (Control control in this.Controls)
{
if (control is Button)
 
Or you could keep the latest button pressed.

You could also have cheboxes or radio buttons allowing to depending on
wether or not the user should be able to load several values at once or not
and have a single button that loads the selected values...

Patrice
 
Krunom,

\\\
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
Control frm = this.FindControl("Form1");
foreach (Control ctl in frm.Controls)
if (ctl is Button)
((Button) ctl).BackColor =
System.Drawing.Color.Azure;
}}
///

And then set the buttoncolor in the normal event to its color.

I hope this helps?

Cor
 
If your buttons name's follow a pattern, you can use an indexer and loop
like this.
Button button = null;
int buttonIndex = 0x1;
while (null != (button = base.FindControl(string.Concat("Button",
buttonIndex ++.ToString())) as Button))
{
button.BackColor = System.Drawing.Color.Azure;
}
 
Dennis Myrén said:
If your buttons name's follow a pattern, you can use an indexer and loop
like this.
Button button = null;
int buttonIndex = 0x1;
while (null != (button = base.FindControl(string.Concat("Button",
buttonIndex ++.ToString())) as Button))
{
button.BackColor = System.Drawing.Color.Azure;
}

Or, much better IMO, would be to have an array of buttons:

Button[] buttons = new Button[50];

Then create the buttons dynamically (rather than with the designer),
say from a list of strings. Then it's really easy to add a particular
event handler to all buttons, etc.
 
If you're looking for a quick and dirty hack, you could (assuming nothing
else on the page depends on it) disable the ViewState for the page. Then the
other buttons would simply "forget" they had been pressed.
A less heavy-handed version would be to disable ViewState just on the buttons.
But again, this is a bit of a hack and probably not a solution for the
purists ;)
 
Jon,

I agree, that would be the best solution.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
If your buttons name's follow a pattern, you can use an indexer and loop
like this.
Button button = null;
int buttonIndex = 0x1;
while (null != (button = base.FindControl(string.Concat("Button",
buttonIndex ++.ToString())) as Button))
{
button.BackColor = System.Drawing.Color.Azure;
}

Or, much better IMO, would be to have an array of buttons:

Button[] buttons = new Button[50];

Then create the buttons dynamically (rather than with the designer),
say from a list of strings. Then it's really easy to add a particular
event handler to all buttons, etc.
 
Cor Ligthert said:
Why

Be aware that this is about a webform

Because whatever kind of solution it is, dealing with an ordered
collection of objects is almost always easier if that collection is
easily indexed. An array (or list) provides that ability very simply.
 
Back
Top