Groups | Blog | Home
all groups > asp.net webcontrols > january 2007 >

asp.net webcontrols : can i assign a name for a control dynamically?


miladhatam NO[at]SPAM gmail.com
1/25/2007 2:54:51 PM
hi ladies and gentlemen
how can i call a control dynamically?
for example
for(i=1;i<10;i++) {
"label" + i + ".text" =i*2;
}
thanks
Klesh Wong
1/25/2007 5:46:02 PM


[quoted text, click to view]
miladhatam NO[at]SPAM gmail.com
1/26/2007 4:29:32 AM
hi klesh
may i ask you just a bit explain how can i do that ?
thanks
Ladislav Mrnka
1/26/2007 6:56:00 AM

I don't understand what you mean by 'assign a name for control dynamically'
but if you want to construct id of your controls as a string you should use
FindControl as follows.

for (int i = 1; i < 10; i++)
{
Label lb = Page.FindControl(String.Format("label{0}", i)) as Label;

if (lb != null)
{
lb.Text = i * 2;
}
}

Regards,
miladhatam NO[at]SPAM gmail.com
1/26/2007 8:58:05 AM
thanks Ladislav
sorry i am not english
your help was correct and it is working now
thanks for your good help
AddThis Social Bookmark Button