T
Tomas Vera
Hello All,
I'm having problems creating a page with dynamic checkboxes in a WebApp.
In my app, I need to query a database, then (based on results) add
checkboxes to my form and set their "Checked" state. Since the controls are
dynamically created, I'm using the OnInit event to create the checkboxes
and set the "Checked" state from the DB.
Next, I want to capture the postback event (AutoPostBack=true) and update
my database based on the cleared/set item.
Lastly, I need to re-render the entire checkbox collection, since clicking
on a "parent" will update "children" on the form.
Simple enough, eh?
The problem is this:
If I have a "Render" operation in the Page_PreRender function, the
CheckedChanged event handler is not getting called, and the database is not
being updated. This in turn leaves the children out of sync with the
database.
After messing around a bit, I've written a very small program that
illustrates this problem (inlude below, inline). To trigger the problem,
simply uncomment the line inthe Page_PreRender function.
QUESTION: Why does the CheckedChanged handler not get called?
To use this code, create a web app, and simply add a PlaceHolder control to
take the checkbox.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CheckTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder
PlaceHolder1;
protected void LclHandleCheck(object sender, System.EventArgs e)
{
CheckBox ckBox = (CheckBox)sender;
ckBox.Text = "Check state: " + ckBox.Checked.ToString();
}
protected void SetItemStatus(CheckBox item)
{
item.Checked = true;
}
protected void CreateCheckBox()
{
CheckBox newBox;
PlaceHolder1.Controls.Clear();
newBox = new CheckBox();
newBox.Text = "OriginalBox";
newBox.AutoPostBack = true;
newBox.CheckedChanged += new
System.EventHandler(this.LclHandleCheck);
newBox.Checked = true;
this.PlaceHolder1.Controls.Add(newBox);
}
private void Page_PreRender(object sender, System.EventArgs e)
{
//CreateCheckBox();
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET
Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
CreateCheckBox();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);
this.PreRender += new
System.EventHandler(this.Page_PreRender);
}
#endregion
}
}
I'm having problems creating a page with dynamic checkboxes in a WebApp.
In my app, I need to query a database, then (based on results) add
checkboxes to my form and set their "Checked" state. Since the controls are
dynamically created, I'm using the OnInit event to create the checkboxes
and set the "Checked" state from the DB.
Next, I want to capture the postback event (AutoPostBack=true) and update
my database based on the cleared/set item.
Lastly, I need to re-render the entire checkbox collection, since clicking
on a "parent" will update "children" on the form.
Simple enough, eh?
The problem is this:
If I have a "Render" operation in the Page_PreRender function, the
CheckedChanged event handler is not getting called, and the database is not
being updated. This in turn leaves the children out of sync with the
database.
After messing around a bit, I've written a very small program that
illustrates this problem (inlude below, inline). To trigger the problem,
simply uncomment the line inthe Page_PreRender function.
QUESTION: Why does the CheckedChanged handler not get called?
To use this code, create a web app, and simply add a PlaceHolder control to
take the checkbox.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CheckTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder
PlaceHolder1;
protected void LclHandleCheck(object sender, System.EventArgs e)
{
CheckBox ckBox = (CheckBox)sender;
ckBox.Text = "Check state: " + ckBox.Checked.ToString();
}
protected void SetItemStatus(CheckBox item)
{
item.Checked = true;
}
protected void CreateCheckBox()
{
CheckBox newBox;
PlaceHolder1.Controls.Clear();
newBox = new CheckBox();
newBox.Text = "OriginalBox";
newBox.AutoPostBack = true;
newBox.CheckedChanged += new
System.EventHandler(this.LclHandleCheck);
newBox.Checked = true;
this.PlaceHolder1.Controls.Add(newBox);
}
private void Page_PreRender(object sender, System.EventArgs e)
{
//CreateCheckBox();
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET
Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
CreateCheckBox();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);
this.PreRender += new
System.EventHandler(this.Page_PreRender);
}
#endregion
}
}