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
}
} 2 2280
Try setting the ID of the checkbox before you add it to the placeholder
"Tomas Vera" <ta****@NOxSPAMsbcglobal.net> wrote in message
news:45********************************@4ax.com... 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 } }
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" <re***********@hotmail.com>
wrote: Try setting the ID of the checkbox before you add it to the placeholder
"Tomas Vera" <ta****@NOxSPAMsbcglobal.net> wrote in message news:45********************************@4ax.com.. . 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 } }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Wardlaw |
last post by:
Hi,
I've just installed Visual C# .Net 2003 Standard onto my PC. The operating
system is XP professional. I've also loaded the MSDN libraries and
downloaded the latest from Microsoft: Oct....
|
by: Sebi |
last post by:
Hello all
is it possible to add a checkbox in a DataGrid for
Boolean Data?
Thanks in advance
|
by: bebop |
last post by:
I'm using three checkbox web controls in C# .NET
and one button, and one labe
Is there a way to "group" these individual checkbox web controls
If so, do I use a for loop, hashtable, array,...
|
by: Scott P. |
last post by:
I'm creating an app using ASP .NET (my second app so bear with me here) that
basically builds a PDF file based on a bunch of user selections. I have a
page which displays a series of checkboxs...
|
by: dx |
last post by:
I'm extremely frustrated with ASP.NET...again! To me this should be as
simple as setting oCheckBox.Checked = True.. yet for some reason it isn't.
I have a user control (ascx) that that has a...
|
by: Samuel Chowdhuri |
last post by:
this peace of code is giving me trouble
DIM i as DataGridItem
for each i in myDataGrid
checkbox chkbox = Ctype(i.findcontrol("deletethis"),checkbox)
if(chkbox.checked) then
......
.....
|
by: pamelafluente |
last post by:
I am still working with no success on that client/server
problem. I need your help. I will submit simplified versions
of my problem so we can see clearly what is going on.
My model:
A client...
|
by: Kevin R |
last post by:
This is one of the weirdest problems I have ever run into. I have had to
trim down a bunch of code to give a sample that is more easily readable by
those who will view this. Here is the problem:...
|
by: calms |
last post by:
HI,
First of all im vry new to PHP, i like to retrieve a value from DB,
accordingly i like to display the checkbox, either checked or not.
Now im able to retrieve the value from DB, i can...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |