Comanighttrain
Comanighttrain

Reputation: 247

Dynamically (programatically) adding check boxes and checkedchanged events

I am having a bit of a problem adding a few check boxes and an event handler programatically. The check boxes all appear fine, but they don't do anything when clicked. Does anyone have any idea what I am doing wrong?

My code:

foreach (Statement i in theseStatements)
{
    box = new CheckBox();
    box.Text = i.StatementText;
    box.AutoPostBack = true;
    box.CheckedChanged += new EventHandler(this.CheckedChange);
    PlaceHolder.Controls.Add(box);
}

protected void CheckedChange(object sender, EventArgs e) 
{
    CheckBox x = (CheckBox)sender;
    Instructions.Text = "change";            
    WorkPlaceHazardsBox.Text += x.Text;
} 

Upvotes: 4

Views: 20131

Answers (8)

xcx
xcx

Reputation: 1

CheckedChanged may not work if embedded in another control, e.g. embedded in TableCells. Have a try to create a CheckBox and add CheckedChanged before creating an outer control (e.g. Table is outer control if CheckBox is embedded in Table's cells).

It could fix the problem in some cases.

Upvotes: 0

Aaron Daniels
Aaron Daniels

Reputation: 9664

I copied your code into a new VS2005 C# web project (see below). Your code works. There may be something else going on outside of this snippet. Or, is the StatementText property in all of your statements collection always empty?

Page...

<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        Instructions: <asp:TextBox ID="Instructions" runat="server" />
        WorkPlaceHazardsBox: <asp:TextBox ID="WorkPlaceHazardsBox" runat="server" />
    </div>
    </form>

</body>

Code behind...

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CheckboxMadness
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> statements = new List<string>(new string[] { "foo", "bar" });

            foreach (string i in statements)
            {
                CheckBox box = new CheckBox();
                box.Text = i;
                box.AutoPostBack = true;
                box.CheckedChanged += new EventHandler(this.CheckedChange);
                PlaceHolder1.Controls.Add(box);
            }

        }
        protected void CheckedChange(object sender, EventArgs e)
        {
            CheckBox x = (CheckBox)sender;

            Instructions.Text = "change";

            WorkPlaceHazardsBox.Text += x.Text;
        }
    }
}

Upvotes: 0

mustafaozcan
mustafaozcan

Reputation:

You can get value by Request["controlname"] method when you inserted control in runtime.You must set Unique ID for each control.

However you can use CheckBoxList as an alternative instead of dynamically added checkboxes

Upvotes: 0

eglasius
eglasius

Reputation: 36037

Make sure to verify you are doing the following:

  • The same list of checkbox is being added on both the initial load and further postbacks
  • You set a different ID to each checkbox
  • Verify you are getting a postback (set a break point in Page Load)
  • The controls are added to the page on Page Load, or even better on Page Init

If you are trying to do something different than that, update us with more info.

Upvotes: 2

Chris Holmes
Chris Holmes

Reputation: 11584

var box = new CheckBox();

Upvotes: -1

Ken Browning
Ken Browning

Reputation: 29091

You should do the following:

  1. Set the ID property for each instance of CheckBox you create in your foreach loop.
  2. For PostBacks, ensure that your CheckBoxes are created and CheckedChanged event handler is attached at some point of the page life-cycle before control events are raised

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500675

When you say they "don't do anything" - are you getting the postback?

I wouldn't be surprised if you had to assign IDs to the checkboxes - bear in mind that on the postback, you'll get a new page, so it'll have to recreate all the checkboxes, then work out which one was checked etc. This is getting into what is (to me, anyway) somewhat black magic side of ASP.NET. I think you'll have to study the page life cycle and control identification side of things reasonably carefully.

Upvotes: 0

casperOne
casperOne

Reputation: 74530

If you have one Instructions textbox and one WorkPlaceHazards textbox per checkbox, then you have to have a way to associate the checkbox that was clicked with those other two controls.

If that's not the case, then what are they supposed to be doing?

Upvotes: 0

Related Questions