Dymanic Checkbox problems (Web App) | | |
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
}
} | | | | re: Dymanic Checkbox problems (Web App)
Try setting the ID of the checkbox before you add it to the placeholder
"Tomas Vera" <tavera@NOxSPAMsbcglobal.net> wrote in message
news:456gu0lga6dciom246rre0tgja468dpj34@4ax.com...[color=blue]
> 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[/color]
are[color=blue]
> 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[/color]
not[color=blue]
> 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[/color]
to[color=blue]
> 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
> }
> }
>
>[/color] | | | | re: Dymanic Checkbox problems (Web App)
Thank, but....
That does not change the behavior. This is beginning to look like some
sort of bug, since the eventhandler should always be called, if the
item has changed.
It is as if the code is "looking ahead" at PreRender and determining
that the value will not be altered, therefore the value is not being
evaluted to determine if the handler needs to be called.
Time to call the MS Boys and see what they know.
-tomas
On Fri, 14 Jan 2005 23:10:38 -0500, "Amit" <remove.amitig@hotmail.com>
wrote:
[color=blue]
>Try setting the ID of the checkbox before you add it to the placeholder
>
>
>"Tomas Vera" <tavera@NOxSPAMsbcglobal.net> wrote in message
>news:456gu0lga6dciom246rre0tgja468dpj34@4ax.com.. .[color=green]
>> 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[/color]
>are[color=green]
>> 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[/color]
>not[color=green]
>> 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[/color]
>to[color=green]
>> 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
>> }
>> }
>>
>>[/color]
>[/color] |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|