Reputation: 5419
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?
Upvotes: 1
Views: 1899
Reputation: 2283
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
Upvotes: 0
Reputation: 16018
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.
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";
}
}
Upvotes: 1