1
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?

Curtis
  • 101,612
  • 66
  • 270
  • 352
Theun Arbeider
  • 5,259
  • 11
  • 45
  • 68

2 Answers2

1

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";
    }

}
Community
  • 1
  • 1
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • 1
    adding the controls again isn't the problem. Getting the value when I changed it on a button click is my problem. – Theun Arbeider Dec 08 '11 at 10:05
  • you said `However, when a button is pressed, the control has vanished of my screen ` - this would indicate you did not re-add the control in each postback, if it isnt added you cant get the value – Richard Friend Dec 08 '11 at 10:06
  • 1
    I want to get the value from the checkbox set in the globals class. – Theun Arbeider Dec 08 '11 at 10:15
  • From where ? the button only exists within that one request, once that request has finished the button is gone, you can't store it and retrieve it later. If you need to store a value to use later, you could use `Session` or `Application`. – Richard Friend Dec 08 '11 at 10:18
  • So I can't store the button in a different class and access it from there after I did a postback? I'm quite sure I have done this before with textbox's .. but I lost that project`. PS. I can't put the control creation in OnInit or CreateChildControl since it's dependent on the value of a drop down box – Theun Arbeider Dec 08 '11 at 10:20
  • You could store it in the globals class, as long as you used it within the same request, but whats the point, you already have a reference to the control within the page so why do you need to do this ? – Richard Friend Dec 08 '11 at 10:27
  • I still dont exactly understand how this works. But it does work! Thank you for your answer and time – Theun Arbeider Dec 08 '11 at 10:55
0

If dynamic controls are created in the Page_Load(object sender, EventArgs e) method they will not return the changes the user made.

The reason you're having problems is the ASP.Net view state is created before the Page_Load(object sender, EventArgs e) method is called. The ASP.Net view state hold what controls are on the page and their values. The Page_Init(object sender, EventArgs e) method is called before the ASP.Net view state is created. By creating the controls in the Page_Init(object sender, EventArgs e) method will return what the user enter, furthermore the controls will only need to be created if the page isn't a post back.

If you can't create the controls in the Page_Init(object sender, EventArgs e) method for some reason, you will edit to change the ASP.Net view state the Page_Load(object sender, EventArgs e).

If you need to create the controls in the Page_Load(object sender, EventArgs e) method this question should help How to Persist Variable on Postback

Community
  • 1
  • 1
TheLukeMcCarthy
  • 2,253
  • 2
  • 25
  • 34