Wrox Programmer Forums
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 7th, 2007, 07:46 AM
Authorized User
Join Date: Nov 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Somesh
Default How to get control in runtime.

Hi All
I have a page in which the controls (Checkboxes, Textboxes, and Dropdown List) are created programmatically. There is a Button, by clicking it I need to separate the Textboxes and the conditions from the Dropdown List.

My problem is that is why whenever the program is not running then there is no existence of the above said controls that is why when ever I am trying to run the code, the compiler is sending some errors like this: - The CheckBox1 does not exist in the current context.

How can I over come this problem?

I shall be highly obliged if I get any type of suggestion from any one.

With Regards.
Somesh Chatterjee

somesh chaterjee
Old March 7th, 2007, 03:42 PM
Authorized User
Join Date: Mar 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts

When you run off of 'dynamically created controls' two things have to happen.

They have to be recreated identically each time.
They have to be 'found'

so for instance if you insert a 'label' into a page.
You do (simple version)
sub page load()
dim myLabel as new label
dim mylabel.id = "mylabel"
myLabel.text = "hello world"
end sub

It is loaded, and will be recreated each time.

Now to handle that control you have to do:
sub button_click() handles button1.click 'Again simplified
dim _label as label
_label = page.findcontrol("myLabel")
response.write "Labels text is:" & _label.text
end sub

Now, if you dynamically add a button you also need to have a handler function with the same args as a real button so.

dim mybutton as new button
mybutton.text = "Click me"
addhandler mybutton.click, addressof myFunc

public sub myFunc(sender as ...., e as eventa....) 'Has to be a valid button/onclick function
response.write "button clicked"
end sub

The best way to make sure your function is correct is to drag out a button, then go to its onclick event in the code view, and copy and paste its code. For reference a valid button code is like this.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim mybutton as new button
mybutton.text = "click me"
addhandler mybutton.click, addressof btnSubmit_Click
end sub

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
response.write "I was clicked"
end sub

Similar Threads
Thread Thread Starter Forum Replies Last Post
runtime instatiation of form control Baby_programmer C# 2005 1 November 29th, 2007 05:05 AM
How to get control in runtime. Somesh C# 2005 1 March 24th, 2007 02:23 PM
add control at runtime, pls HELP jimmyfrank Pro VB 6 3 February 4th, 2005 03:39 PM
Create Control, Runtime jimmyfrank Access VBA 1 January 29th, 2005 10:40 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.