Creating references to Controls ID's/Names in code behind pages 
Author Message
 Creating references to Controls ID's/Names in code behind pages

Hi All,

Here is what I am trying to do:
I have an ASPX project that has a lot of repeatition in the code. A
lot of this code that is repeated because at the moment I haven't
found a way to programmatically create a "control's" ID and assign it
a value.

The controls in question have been labelled btnVal1, btnVal2, btnVal3,
...btnVal15. They are all ImageButtons

Now, not all the controls are shown at one time, and what I want to do
is something like (very simplified)

for i = 1 to how many controls should show a tick
   temp = "btnVal" & CStr(i)
   temp.imageurl = "images/ticked.jpg"
Next

Of course, I get the error that imageurl is not in the class String.

I have tried the CTyping the string to an imagecontrol but I get an
error stating the String cannot be converted to a type control.

Can anyone suggest ways of begin able to do this..

Thanks,
Robert



Mon, 25 Jul 2005 12:19:15 GMT  
 Creating references to Controls ID's/Names in code behind pages
Hi Robert.  Try putting your controls into an array, and then referencing
the array elements, like this:

Dim ImgBtn() as New ImageButton = {btnVal1, btnVal2, btnVal3, ... btnVal15}
Dim ii as Integer

For ii = 0 to How Many Controls should show a tick
    ImgBtn(ii).ImageUrl = "images/ticked.jpg"
Next

I don't know of a way to "programatically" build a control's name.  And
unfortunately, VB.NET doesn't support Control Arrays like VB 6 does.  So,
what I illustrated above is as close as you can get.

Hope that helps!

Regards,

Jeff Ptak


Quote:
> Hi All,

> Here is what I am trying to do:
> I have an ASPX project that has a lot of repeatition in the code. A
> lot of this code that is repeated because at the moment I haven't
> found a way to programmatically create a "control's" ID and assign it
> a value.

> The controls in question have been labelled btnVal1, btnVal2, btnVal3,
> ...btnVal15. They are all ImageButtons

> Now, not all the controls are shown at one time, and what I want to do
> is something like (very simplified)

> for i = 1 to how many controls should show a tick
>    temp = "btnVal" & CStr(i)
>    temp.imageurl = "images/ticked.jpg"
> Next

> Of course, I get the error that imageurl is not in the class String.

> I have tried the CTyping the string to an imagecontrol but I get an
> error stating the String cannot be converted to a type control.

> Can anyone suggest ways of begin able to do this..

> Thanks,
> Robert



Wed, 27 Jul 2005 11:33:11 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to reference a literal control from code-behind

2. Control name of subform from code behind subform

3. Disabling Outlook 2000 Menu Items - Finding Control ID, IDs, ID's

4. Code-behind file inheriting from another code-behind file

5. Trying to create a Checkbox control array (code behind)

6. How do I reuse code in the Code-Behind section of an ASPX page

7. Self-referencing a control's name within one of it's event procedures

8. Can't add web reference from behind proxy server

9. Passing Form Fields to VB.NET Custom Business Object in an ASP.NET Code Behind Page

10. Referencing a control's name

11. How can I retrieve the HTML codes behind a web page

12. Create IIS Application with webforms Code Behind causes Problem

 

 
Powered by phpBB® Forum Software