PC Review


Reply
Thread Tools Rate Thread

accessing control by its id in variable

 
 
=?Utf-8?B?YXNoaW0=?=
Guest
Posts: n/a
 
      14th Jun 2006
i have textboxes with ids tb1,tb2,tb3...
now i want to set text to these textboxes somehow like

for (i=1;i<5;i++)
("tb"+i).Text = i;

obviously it won't work.
How to translate ("tb"+i) into actual control id?
is there anyway to access properties of controls using ids stored in
variables ?

please help
thanks a lot

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      14th Jun 2006
ashim,
Yes. You can store all the controls in a Hashtable at startup and access
them from there by key.

Otherwise, what you what to do is more like
foreach (Control c in this.Controls0
{
if (c.id.Substring(0,2)=="tb')
// your cool stuff here
}

.... etc

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"ashim" wrote:

> i have textboxes with ids tb1,tb2,tb3...
> now i want to set text to these textboxes somehow like
>
> for (i=1;i<5;i++)
> ("tb"+i).Text = i;
>
> obviously it won't work.
> How to translate ("tb"+i) into actual control id?
> is there anyway to access properties of controls using ids stored in
> variables ?
>
> please help
> thanks a lot
>

 
Reply With Quote
 
Balasubramanian Ramanathan
Guest
Posts: n/a
 
      14th Jun 2006
Use the controls collection member of the form or reflection to implement
what you need

"ashim" <(E-Mail Removed)> wrote in message
news:87B8A801-2C7F-4E08-B1B7-(E-Mail Removed)...
>i have textboxes with ids tb1,tb2,tb3...
> now i want to set text to these textboxes somehow like
>
> for (i=1;i<5;i++)
> ("tb"+i).Text = i;
>
> obviously it won't work.
> How to translate ("tb"+i) into actual control id?
> is there anyway to access properties of controls using ids stored in
> variables ?
>
> please help
> thanks a lot
>



 
Reply With Quote
 
=?Utf-8?B?YXNoaW0=?=
Guest
Posts: n/a
 
      14th Jun 2006
Thanks Peter. I was thinking,
without looping through all the controls, if we could have something like
getControl(id) to get the control's reference directly.


"Peter Bromberg [C# MVP]" wrote:

> ashim,
> Yes. You can store all the controls in a Hashtable at startup and access
> them from there by key.
>
> Otherwise, what you what to do is more like
> foreach (Control c in this.Controls0
> {
> if (c.id.Substring(0,2)=="tb')
> // your cool stuff here
> }
>
> ... etc
>
> Peter
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "ashim" wrote:
>
> > i have textboxes with ids tb1,tb2,tb3...
> > now i want to set text to these textboxes somehow like
> >
> > for (i=1;i<5;i++)
> > ("tb"+i).Text = i;
> >
> > obviously it won't work.
> > How to translate ("tb"+i) into actual control id?
> > is there anyway to access properties of controls using ids stored in
> > variables ?
> >
> > please help
> > thanks a lot
> >

 
Reply With Quote
 
Andreas Mueller
Guest
Posts: n/a
 
      14th Jun 2006
ashim wrote:

> Thanks Peter. I was thinking,
> without looping through all the controls, if we could have something like
> getControl(id) to get the control's reference directly.
>
>
> "Peter Bromberg [C# MVP]" wrote:
>
>
>>ashim,
>>Yes. You can store all the controls in a Hashtable at startup and access
>>them from there by key.
>>
>>Otherwise, what you what to do is more like
>>foreach (Control c in this.Controls0
>>{
>>if (c.id.Substring(0,2)=="tb')
>>// your cool stuff here
>>}
>>
>>... etc
>>
>>Peter
>>
>>--
>>Co-founder, Eggheadcafe.com developer portal:
>>http://www.eggheadcafe.com
>>UnBlog:
>>http://petesbloggerama.blogspot.com
>>
>>
>>
>>
>>"ashim" wrote:
>>
>>
>>>i have textboxes with ids tb1,tb2,tb3...
>>>now i want to set text to these textboxes somehow like
>>>
>>>for (i=1;i<5;i++)
>>>("tb"+i).Text = i;
>>>
>>>obviously it won't work.
>>>How to translate ("tb"+i) into actual control id?
>>>is there anyway to access properties of controls using ids stored in
>>>variables ?
>>>
>>>please help
>>>thanks a lot
>>>



for (i=1;i<5;i++)
owningControl.Controls["tb"+i].Text = i;

HTH,
Andy
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
accessing a code-behind variable from a user control TB Microsoft ASP .NET 3 12th Jul 2007 02:48 AM
Accessing a control if its name is in a string variable JohnR Microsoft VB .NET 9 6th Jul 2007 11:55 AM
Accessing value of a Variable in parent from custom control =?Utf-8?B?WmFoaWQgSGF5YXQ=?= Microsoft Dot NET Framework 7 19th Nov 2006 07:12 PM
Accessing page variable from control Gerald Microsoft ASP .NET 2 24th Nov 2003 02:50 PM
accessing page variable from user control code behind Gerald Microsoft ASP .NET 1 4th Jul 2003 05:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:16 PM.