-
Referencing dynamically loaded controls
Hi,
Since vb.net doesn't support control arrays, I've created some button controls
at runtime by giving each of them a different name based on a counter. The
problem I'm facing now is, I can recreate each of the control name but how
can I get the properties or methods of a dynamically created control ? I
can give you an example. (strControlName = "Button" & iCounter , So the
first button name will be Button1. I got the control name stored in a string
variable. Now I wan't to check a property of the control Button1. Pls help.
Thanks
Vinod
-
Re: Referencing dynamically loaded controls
Hello Vinod,
>Since vb.net doesn't support control arrays, I've
>created some button controls at runtime by giving
>each of them a different name based on a counter.
>The problem I'm facing now is, I can recreate each
>of the control name but how can I get the properties
>or methods of a dynamically created control? I can
VB.Net does support control arrays. The short coming is that the
form designer does not create them for you automatically. You can
create a control array yourself with code similar to the following:
<Untested Code>
'General Declarations Section
Private MyButtons As Button()
'Form Load or Constructor
MyButtons = New Button() {btnLoad, btnSave, _
btnDelete, btnClose}
</Untested Code>
After that you can access your buttons from the array in pretty much
the same way you would from a VB6 app. i.e.
MyButtons{1}.Text = "&Save"
This is much easier than trying to access them by name, which would
involve looping through the array and comparing the .Name property with
a string.
Hope this helps,
Len
-
Re: Referencing dynamically loaded controls
Vinod,
To find the on control and access it's properties you could do a FindControl.
Now you could find it by the control name, which it looks like you already
have, or by the control index as it pertains to the page. I would go with
the name as follows
dim btn as button = FindControl("Button1")
Now you should be able to access all the properties of the button
Hope this helps,
Anthony
"Vinod" <vinod@svw.com> wrote:
>
>Hi,
> Since vb.net doesn't support control arrays, I've created some button
controls
>at runtime by giving each of them a different name based on a counter. The
>problem I'm facing now is, I can recreate each of the control name but how
>can I get the properties or methods of a dynamically created control ? I
>can give you an example. (strControlName = "Button" & iCounter , So the
>first button name will be Button1. I got the control name stored in a string
>variable. Now I wan't to check a property of the control Button1. Pls help.
>Thanks
>Vinod
-
handle syntax for dynamic controls
The Handles portion of an event handler call requires <control name>.event. Since my control is created at runtime, and there could be many of them, I don't know how to specify the control name in the Handles clause. Help would be appreciated! Nancy
-
Hi, Nancy: For dynamically-created controls, you should use AddHandler: http://www.thescarms.com/dotNet/EventHandler.asp
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks