By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,384 Members | 845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,384 IT Pros & Developers. It's quick & easy.

Creating Checkboxes dynamically

P: n/a
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
Jul 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
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

Jul 16 '06 #2

P: n/a
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/subscripti...ult.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/subscripti...t/default.aspx.

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

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

Jul 17 '06 #3

P: n/a
"SalamElias" <el******@online.nospamwrote in message
news:69**********************************@microsof t.com...
>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.
Jul 17 '06 #4

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

"Shawn Wildermuth" wrote:
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
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


Jul 17 '06 #5

P: n/a
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
Jul 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.