I am creating a web page with dynamic controls and want to add events to the controls.

Specifically, I have labels and checkboxes on my web page and want to add an event to review the status of the checkboxes.

In the event, I want to check to see if the checkbox that was just changed is checked and if so, make sure the other checkbox is unchecked and if the other checkbox is checked, reset the other checkbox so that it is unchecked - the check boxes are mutually exclusive.

Here is a sample of what I am trying to do:

using System;
using System.Data;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void ClickMe(object sender, EventArgs e)
    {
        //I want to check to see if the checkbox that was just changed is checked 
        //and if so, make sure the other checkbox is unchecked 
       //and if the other checkbox is checked, 
       //reset the other checkbox so that it is unchecked 
       //- the check boxes are mutually exclusive
       // I am just changing the label text to verify it the event is firing
        Label1.Text = "Changed DataBind";
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>    <script runat="server">
        int count = 1;
        int r = 0;
        public void IterateThroughChildren(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.GetType().ToString().Equals("System.Web.UI.WebControls.Label") &&
                      c.ID == null)
                {
                	((Label)c).ID = "lbl" + Convert.ToString(r);
                        ((Label)c).Enabled = true;
                        ((Label)c).Visible = true;
                        ((Label)c).Text = "Label Information " + Convert.ToString(r) ;
                        r++; 
                }
                           
                if (c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox") &&
                      c.ID == null)
                {
                    ((CheckBox)c).ID = "cbx" + Convert.ToString(r);
                    ((CheckBox)c).TabIndex = short.Parse(Convert.ToString(r));
                    ((CheckBox)c).CheckedChanged += new System.EventHandler(this.ClickMe);
                }
                count++;
                if (c.Controls.Count > 0)
                {
                    IterateThroughChildren(c);
                }
            }

        }
        public void LoadData(object sender, EventArgs e)
        {
                SetupHere.Controls.Add(new CheckBox());
                SetupHere.Controls.Add(new Label());
                SetupHere.Controls.Add(new CheckBox());
                SetupHere.Controls.Add(new Label());
                SetupHere.Controls.Add(new LiteralControl("<br>"));
                SetupHere.Controls.Add(new LiteralControl("<br>"));
                SetupHere.Controls.Add(new LiteralControl("<br>"));
                SetupHere.Controls.Add(new LiteralControl("<br>"));
                
           
            IterateThroughChildren(this);
        }

        </script>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
        <asp:Label ID="Label1" runat="server" Text="First Pass" Visible="true" OnLoad="LoadData"></asp:Label>
        <br />    
        <br />
        <asp:PlaceHolder ID="SetupHere" runat="server" ></asp:PlaceHolder>
        </div>
    </form>
</body>
</html>

Any help would be appreciated.

Thank you.

so where u got stuck.

The event does not "fire" when the checkbox is checked or unchecked.

y did u use

((CheckBox)c).CheckedChanged += new System.EventHandler(this.ClickMe);

i think the possible reason why this event didnt fired is u didnt declare the controls to runat at server

(checkbox)c.attributes.add("runat","server");

Thank you for your response and idea.

I added the "runat","server" line, but did not change the outcome - the event still did not fire.

dvlpr30062

hi,
y dont use javascript instead of event handler.

add this code

((CheckBox)c).CheckedChanged += new System.EventHandler(this.ClickMe);  

((CheckBox)c).AutoPostBack = true;

I think you have to set the checkbox propery of PostBack=True. It will send every request that are located with events and executes on rendering the pages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.