473,668 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=t rue) 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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace CheckTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Plac eHolder
PlaceHolder1;
protected void LclHandleCheck( object sender, System.EventArg s e)
{
CheckBox ckBox = (CheckBox)sende r;
ckBox.Text = "Check state: " + ckBox.Checked.T oString();
}

protected void SetItemStatus(C heckBox item)
{
item.Checked = true;
}

protected void CreateCheckBox( )
{
CheckBox newBox;
PlaceHolder1.Co ntrols.Clear();

newBox = new CheckBox();
newBox.Text = "OriginalBo x";
newBox.AutoPost Back = true;
newBox.CheckedC hanged += new
System.EventHan dler(this.LclHa ndleCheck);
newBox.Checked = true;

this.PlaceHolde r1.Controls.Add (newBox);
}

private void Page_PreRender( object sender, System.EventArg s e)
{
//CreateCheckBox( );
}
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET
Web Form Designer.
//
InitializeCompo nent();
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 InitializeCompo nent()
{
this.Load += new
System.EventHan dler(this.Page_ Load);
this.PreRender += new
System.EventHan dler(this.Page_ PreRender);

}
#endregion
}
}
Nov 16 '05 #1
2 2329
Try setting the ID of the checkbox before you add it to the placeholder
"Tomas Vera" <ta****@NOxSPAM sbcglobal.net> wrote in message
news:45******** *************** *********@4ax.c om...
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=t rue) 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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace CheckTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Plac eHolder
PlaceHolder1;
protected void LclHandleCheck( object sender, System.EventArg s e)
{
CheckBox ckBox = (CheckBox)sende r;
ckBox.Text = "Check state: " + ckBox.Checked.T oString();
}

protected void SetItemStatus(C heckBox item)
{
item.Checked = true;
}

protected void CreateCheckBox( )
{
CheckBox newBox;
PlaceHolder1.Co ntrols.Clear();

newBox = new CheckBox();
newBox.Text = "OriginalBo x";
newBox.AutoPost Back = true;
newBox.CheckedC hanged += new
System.EventHan dler(this.LclHa ndleCheck);
newBox.Checked = true;

this.PlaceHolde r1.Controls.Add (newBox);
}

private void Page_PreRender( object sender, System.EventArg s e)
{
//CreateCheckBox( );
}
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET
Web Form Designer.
//
InitializeCompo nent();
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 InitializeCompo nent()
{
this.Load += new
System.EventHan dler(this.Page_ Load);
this.PreRender += new
System.EventHan dler(this.Page_ PreRender);

}
#endregion
}
}

Nov 16 '05 #2
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****@NOxSPAM sbcglobal.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=t rue) 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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace CheckTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Plac eHolder
PlaceHolder1;
protected void LclHandleCheck( object sender, System.EventArg s e)
{
CheckBox ckBox = (CheckBox)sende r;
ckBox.Text = "Check state: " + ckBox.Checked.T oString();
}

protected void SetItemStatus(C heckBox item)
{
item.Checked = true;
}

protected void CreateCheckBox( )
{
CheckBox newBox;
PlaceHolder1.Co ntrols.Clear();

newBox = new CheckBox();
newBox.Text = "OriginalBo x";
newBox.AutoPost Back = true;
newBox.CheckedC hanged += new
System.EventHan dler(this.LclHa ndleCheck);
newBox.Checked = true;

this.PlaceHolde r1.Controls.Add (newBox);
}

private void Page_PreRender( object sender, System.EventArg s e)
{
//CreateCheckBox( );
}
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET
Web Form Designer.
//
InitializeCompo nent();
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 InitializeCompo nent()
{
this.Load += new
System.EventHan dler(this.Page_ Load);
this.PreRender += new
System.EventHan dler(this.Page_ PreRender);

}
#endregion
}
}


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1188
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. 2003 (at least I think that's the latest) However, whenever I start visual Studio the dynamic help can not find the server for any of the help entries.
2
3125
by: Sebi | last post by:
Hello all is it possible to add a checkbox in a DataGrid for Boolean Data? Thanks in advance
2
2091
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, etc What I'm looking for is how to determine what checkbox was selected from the three checkbox web controls and have the selected checkbox(es) display in a label
0
1407
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 using the checkbox object. These are all created on-the-fly using the following code: While dtrSelect.Read Dim DocumentRow As New TableRow Dim DocumentCell As New TableCell Dim DocumentCell1 As New TableCell Dim chkDocument As CheckBox
1
4493
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 checkbox and I can't get it to default to checked. I tried radiobuttons and experienced the same result.. can't start them as checked. The really frustrating thing is that I set the attributes of other input controls in the Init() with no problem. ...
1
2843
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 ...... .....
16
2524
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 uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
1
4104
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: I dynamically add an htmlcheckbox to a webform in the pages render and set the checked value to true. When the page loads, if I remove the check from the checkbox and then submit it, in the submit event the checkbox' checked value is still...
2
1973
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 show even show the checkbox, but i cant make it checked????
0
8374
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8791
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.