Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:50:19+00:00 2026-05-27T13:50:19+00:00

this.Controls.Add(new CheckBox{ Checked = true; }) When I add this in the page_load .

  • 0
this.Controls.Add(new CheckBox{ Checked = true; })

When I add this in the page_load. It works, it adds the checkbox and it is visible.

A little different approach:

var button = new CheckBox{ Checked = true; } 
globals.button = button;
this.Controls.Add(button);

Globals is a class with a checkbox property on which I want to set the checkbox in the hope of retrieving it’s a data after pressing a button.

public static CheckBox button { get; set; }

However, when a button is pressed, the control has vanished of my screen and the button in my globals class has not been updated with any changes I have made to the checkbox.

How can I change the checked state of a checkbox and catch it’s current state when I perform a button.click event?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:50:19+00:00Added an answer on May 27, 2026 at 1:50 pm

    You must re-create dynamic controls on every postback, they wont magically re-appear because every request is a new instance of the Page class.

    See my previous post on this subject, it is using a user control but the idea is just the same.

    And another

    You must add the control before Page_Load

    I normally do it in the overridden CreateChildControls but some people use Page_Init.

    see this article

    Update

    This is a very simple way to add the checkbox dynamically, that preserves state/value when the button is clicked.

        <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:PlaceHolder runat="server" ID="ph"></asp:PlaceHolder>
            <asp:Button OnClick="btn_Click" runat="server" ID="btn" Text="Click Me" />
    
            <asp:Label runat="server" ID="lbl"></asp:Label>
        </form>
    </body>
    </html>
    

    Then Code Behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    
    public partial class Test : Page
    {
        private CheckBox MyCheckBox { get; set; }
        protected override void CreateChildControls()
        {
    
            this.MyCheckBox = new CheckBox() { Checked = true };
            this.ph.Controls.Add(this.MyCheckBox);
            base.CreateChildControls();
        }
    
        protected void btn_Click(object sender, EventArgs e)
        {
            var someValue = this.MyCheckBox.Checked;
            this.lbl.Text = someValue ? "Checked" : "Not Checked";
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to add custom controls to the mediaPlayer control view much as this
runat=server /> But this code is not working How can I add multiple controls
I have the following code: foreach (var control in this.Controls) { } I want
Anyone getting this error when using the new free chart controls MS bought from
My goal is to add different Controls/UserControls in a WPF ListView. The Controls can
I have a Panel and I am adding controls inside this panel. But there
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +
Many controls in WPF has this snap to device pixels (as explained on MSDN
This question was originary in my head as Can I use AWT controls in
As an extension to this question here Linking JavaScript Libraries in User Controls I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.