custom user contol in array (asp.net/C#)

  • Thread starter Thread starter dllhell
  • Start date Start date
D

dllhell

Hello,

I would like to create a custom user control with indexes and access this
control like
mycontrol[n],something()

It would be a hundred such controls on the web form placed in table 10x10
cells.
Does anyone know how to force a control to be in array?
 
Hello,

I would like to create a custom user control with indexes and access this
control like
mycontrol[n],something()

It would be a hundred such controls on the web form placed in table 10x10
cells.
Does anyone know how to force a control to be in array?

You cannot force the control to be an array, but you can have an array
of controls, just as you can have an array of any other kinds of
objects:

Button[,] btns = new Button[10, 10];
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
Button btn = new Button();
btn.Text = "Button @ " + i + ","+ j;
...
btns[i,j] = btn;
}
}
 
Pavel Minaev said:
You cannot force the control to be an array, but you can have an array
of controls, just as you can have an array of any other kinds of
objects:


That was what I afride of...
I have a B question then,
If I instance and load control dynamicaly, its impossible to define in which
HTML table cell I wish to load instanced control, isn't it?
So if I can have an array I cant define a possition.
Please tell me I'm wrong! :))
 
That was what I afride of...
I have a B question then,
If I instance and load control dynamicaly, its impossible to define in which
HTML table cell I wish to load instanced control, isn't it?

Why, it is possible, of course. You just add the control to the
ControlCollection of the parent control you want. E.g.:

var button = new Button();
...
panel.Controls.Add(button);

You can do the same for the table cells, of course. I'd expect you'll
need to create that 10x10 grid of table cells first, though - use
HtmlTable/HtmlTableRow/HtmlTableCell controls for that (you can have
HtmlTable created from .aspx, and add the rows/cells from codebehind).

Note that, in general, there's no such thing as "loading control
statically" vs "dynamically" in ASP.NET. It's all the same thing. When
your ASP.NET page is compiled, the result is C# (or VB) code that
creates those controls, exactly the same as if you were the one who
created them.
 
Back
Top