ASP.NET equilivant of VB6 control array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a project in ASP.NET using C#. I have a large number of Button Controls and a large number of ImageButton Controls. It would be convienent if I could refer to a specific control by it's index number, in the same way in which I used a Command Array in VB6. What is the method of assigning and then obtaining a specific unique identified for a control ? Sample code or reference articles would be appreciated.
Thanks,
Jim
 
Use the "sender" argument of your event handler to identify which control
fired the event.


James McGivney said:
I have a project in ASP.NET using C#. I have a large number of Button
Controls and a large number of ImageButton Controls. It would be convienent
if I could refer to a specific control by it's index number, in the same way
in which I used a Command Array in VB6. What is the method of assigning and
then obtaining a specific unique identified for a control ? Sample code or
reference articles would be appreciated.
 
Cast the sender argument to the control type and then access its properties.

There is a control array collection equivalent to the VB6 model, but AFAIK
it only works on windows forms.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

James McGivney said:
I have a project in ASP.NET using C#. I have a large number of Button
Controls and a large number of ImageButton Controls. It would be convienent
if I could refer to a specific control by it's index number, in the same way
in which I used a Command Array in VB6. What is the method of assigning and
then obtaining a specific unique identified for a control ? Sample code or
reference articles would be appreciated.
 
Can you elaborate on this control array collection available in a windows
forms project?
 
Thanks for your help. I'm almost there
I can retreive the name of the pressed button by
string WhichButton = ((System.Web.UI.WebControls.Button)sender).ID.ToString()
Now, how do I use this information, say to change the BackColor
WhichButton.BackColor = Color.Green; generates an error
What is the syntax to change the BackColor having the name of the control
Thanks
Jim
 
Don't declare WhichButton as a string, declare it as a button. Then set an
object reference to Sender cast as the button type.

Don't have the C# for you, but here it is in VB.NET

Dim WhichButton as System.Web.UI.WebControls.Button
WhichButton = CType(sender, System.Web.UI.WebControls.Button)
WhichButton.BackColor = Color.Green

Or even more simply (no variable reference, just working directly with
sender):

CType(sender, System.Web.UI.WebControls.Button).BackColor = Color.Green
 
I've never used them, but for VB.NET GUI projects in (I believe)
compatibility mode, there are a bunch of framework classes that wrap the
control collection functionality. I don't know how close they are to the VB6
ones.
 
I ask because in .NET you can't create a control array in the VB 6.0 sense
(several controls with the same name identified by an index). In .NET, you
can create a collection of controls, but this is not the same thing.

You can get the same benefits of a control array in .NET by assigning one
event handler to handle all the various controls with the event handlers
"Handles" clause and then you can use a Select statement that looks at the
Sender argument to see which one was responsible for firing the event.
 
I'd like to ask one more related question. From this thread I have learned how to detect which button was clicked and how to use this information to change the properties of the button. But, suppose I find out the 5th button was pressed, now how to I add an image to my 5th ImageButton. Assume that the clicked button is Button5 and the imageButton I want to change is ImageButton5. How do I get the info for imageButton5 ? VB6's index property would make this simple. How do I accomplish this in C#.net
Thanks for your help
Jim
 
Public Sub CommonEventHander (sender as object, e as eventargs) Handles
button1.click, button2.click, button3.click
'Let's assume that web form button controls are what this event handler
handles
dim testButton as system.web.ui.button

'It's safe to cast sender as a system.web.ui.button because we know that
this is the only type of control this sub handles
testButton = CType(sender, system.web.ui.button)

'Check some property of the button that will identify it uniquely
'In this case, we'll use its name since all controls must have a unique
name

Select Case testButton.name
Case "btnItem1"
'Now that you know "btnItem1" was clicked, do whatever you need
to

Case "btnItem2"
'Now that you know "btnItem2" was clicked, do whatever you need
to

Case "btnItem3"
'Now that you know "btnItem3" was clicked, do whatever you need
to

End Select

End Sub


James McGivney said:
I'd like to ask one more related question. From this thread I have
learned how to detect which button was clicked and how to use this
information to change the properties of the button. But, suppose I find out
the 5th button was pressed, now how to I add an image to my 5th ImageButton.
Assume that the clicked button is Button5 and the imageButton I want to
change is ImageButton5. How do I get the info for imageButton5 ? VB6's
index property would make this simple. How do I accomplish this in C#.net ?
 
Back
Top