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

ASP.NET 2.0 Dynamic CheckBox example?

P: n/a

Can someone provide a sample of how to add a new CheckBox control to
an ASP.NET page, dynamically completely in code-behind? I am having a
problem understanding which event should handle the control creation
and ViewState rehydration, and also let the checkbox click() event
participate properly.

Thanks.

May 24 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"lucius" <lu****@newsgroup.nospamwrote in message
news:fq********************************@4ax.com...
>
Can someone provide a sample of how to add a new CheckBox control to
an ASP.NET page, dynamically completely in code-behind? I am having a
problem understanding which event should handle the control creation
and ViewState rehydration, and also let the checkbox click() event
participate properly.
something like this

CheckBox cb = new CheckBox();
cb.Text = "Select";
cb.AutoPostBack = true;
cb.CheckedChanged +=new EventHandler(cbChanged);
Controls.Add(cb);

public void cbChanged(object sender, EventArgs e)
{
CheckBox cb= (CheckBox) sender;
Response.Write(cb.Checked)
}
May 24 '07 #2

P: n/a
Thanks, but in which event is the control created? And will multiple
checkboxes get recreated on each page touch? And will ViewState get
rehydrated properly? And will the dynamic checkbox have access to
Session?

Thanks.

On Thu, 24 May 2007 14:43:43 +0200, "Alexey Smirnov"
<al************@gmail.comwrote:
>
"lucius" <lu****@newsgroup.nospamwrote in message
news:fq********************************@4ax.com.. .
>>
Can someone provide a sample of how to add a new CheckBox control to
an ASP.NET page, dynamically completely in code-behind? I am having a
problem understanding which event should handle the control creation
and ViewState rehydration, and also let the checkbox click() event
participate properly.

something like this

CheckBox cb = new CheckBox();
cb.Text = "Select";
cb.AutoPostBack = true;
cb.CheckedChanged +=new EventHandler(cbChanged);
Controls.Add(cb);

public void cbChanged(object sender, EventArgs e)
{
CheckBox cb= (CheckBox) sender;
Response.Write(cb.Checked)
}
May 24 '07 #3

P: n/a
Hi lucius,

For dynamically created controls in ASP.NET web page, the creation and
event handler wireup should be performed in either Load or Init event(also
could be PreInit or other pre... in ASP.NET 2.0). "Init" is preferred since
LoadViewState occurs after "Init" event and before "Load" event.

#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/kb/317794

Hope this helps.

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.

May 25 '07 #4

P: n/a
Hi Lucius,

Have you got any progress on this? If there is still anything we can help,
please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 30 '07 #5

P: n/a

Please consider the issue closed.

thanks.
On Wed, 30 May 2007 01:13:44 GMT, st*****@online.microsoft.com (Steven
Cheng[MSFT]) wrote:
>Hi Lucius,

Have you got any progress on this? If there is still anything we can help,
please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.