Creating Checkboxes dynamically

G

Guest

I am creating several chkBoxes dynamically and assigning an event handler in
the Page_load as foillows
*****************************
Dim chkCatOption As CheckBox = New CheckBox
chkCatOption.Text = rowDS.Item("Option_Item_Desc")
chkCatOption.ID = rowDS.Item("ID").ToString
chkCatOption.Checked = rowDS.Item("HasOption")
chkCatOption.AutoPostBack = True
AddHandler chkCatOption.CheckedChanged, AddressOf
AddOrUpdate
******************************
When the page is displayed, I click on the check box nothing happens. I hqve
put a stop point to debug, nothing is launched.
Thanks
 
S

Shawn Wildermuth

Hello SalamElias,

Maybe a silly question, but are you adding the checkbox to the Page's (or
container's) Controls collection?


Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
 
S

Steven Cheng[MSFT]

Hi Salam,

Does Shawn's suggestion address your problem? For dynamic created controls,
they need to be created in each page requests (no matter initiall request
or postback). Thereforce, you should take care that do not put the control
creation code into If Not IsPostBack Then ..... code block. Here is a
complete test page which contains code that creating a checkbox dynamically
and add it into Page's Form Control collection:

=====================

Partial Class Dynamic_DynamicControls
Inherits System.Web.UI.Page

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init

Dim chk As New CheckBox
chk.ID = "chkSelected"
chk.Text = "Selected"
chk.AutoPostBack = True
AddHandler chk.CheckedChanged, AddressOf chkSelected_CheckedChanged


Page.Form.Controls.Add(chk)

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

End Sub


Protected Sub chkSelected_CheckedChanged(ByVal sender As Object, ByVal
e As System.EventArgs)
Response.Write("<br/>" & sender.Checked)
End Sub
End Class
=====================

In addition, here are some articles that dicussing on creating dynamic
controls on ASP.NET webform:

#HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET
http://support.microsoft.com/kb/317515/en-us

#How to: Add Controls to an ASP.NET Web Page Programmatically
http://msdn2.microsoft.com/en-us/library/kyt0fzt1.aspx

Hope this helps some. If you have any further questions on this, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial

response from the community or a Microsoft Support Engineer within 1
business day is

acceptable. Please note that each follow up response may take approximately
2 business days

as the support professional working with you may need further investigation
to reach the

most efficient resolution. The offering is not appropriate for situations
that require

urgent, real-time or phone-based interactions or complex project analysis
and dump analysis

issues. Issues of this nature are best handled working with a dedicated
Microsoft Support

Engineer by contacting Microsoft Customer Support Services (CSS) at

http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Rae

I am creating several chkBoxes dynamically and assigning an event handler
in
the Page_load as foillows

See Steven Cheng's post - the trick here is to create dynamic controls in
the Page_Init event, not the Page_Load event.
 
G

Guest

No, I am adding it to a panel object container which is declared between the
<from></form>
Thanks
 
S

Steven Cheng[MSFT]

Hi Salam,

Does the code in my last reply helps? Or if you still suffer the problem,
would you please provide us as simplifed test page so that we can have a
look and perform some test on it?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,086
Latest member
ChelseaAmi

Latest Threads

Top