asp.net click event not firing up - c#

i'm having trouble with a Button . The Click event is not working, i'm adding this button to a cell on a table. The rows for the table are filled from a List. It's a simple project for college. I'm already using this code on a previous page and it works.
Here's my code:
aspx:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<!-- Nombre del Cliente -->
<asp:Label ID="lblNombreCliente" CssClass="lbl negrita fs30" runat="server" Text="Cliente: " />
<!-- Listado de Proyectos-->
<asp:Table ID="tblProyectos" CssClass="tbl" runat="server">
<asp:TableRow>
<asp:TableHeaderCell CssClass="th w10">Cod</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="th">Nombre</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="th w10">Horas</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="th w20">Encargado</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="th">Ver</asp:TableHeaderCell>
</asp:TableRow>
</asp:Table>
CS:
protected void Page_Load(object sender, EventArgs e)
{
int codCliente = 0;
if(!IsPostBack)
{
//Usuario Conectado
string nombre = Request.Params["usuario"];
if (nombre != null)
{
this.Master.datosUsuario = String.Format(nombre);
}
//Codigo Cliente
codCliente = int.Parse(Request.Params["codCliente"]);
if (codCliente != 0)
{
foreach (Cliente objCliente in listaClientes)
{
if (objCliente.Cod_cliente == codCliente)
{
lblNombreCliente.Text = objCliente.Nombre;
}
}
}
}//fin isPostBack
//Se agregan proyectos a la tbl
int i = 1;
foreach (Proyecto item in listaProyectos)
{
//Se rellenan las celdas
if (item.Cod_cliente == codCliente)
{
TableRow fila = new TableRow();
TableCell cod = new TableCell();
TableCell nombre_proyecto = new TableCell();
TableCell horas = new TableCell();
TableCell encargado = new TableCell();
TableCell btnsProyecto = new TableCell();
string nombre_encargado = string.Empty;
foreach (Usuario usu in listaUsuarios)
{
if(usu.Cod_usuario == item.Cod_encargado){
nombre_encargado = usu.Nombre;
break;
}
}
Button btnIngreso = new Button();
btnIngreso.CssClass = "btn";
btnIngreso.Text = "Ingresar";
btnIngreso.ID = "btnIngresar_" + i;
btnIngreso.Click += new EventHandler(this.btnIngresar_Click);
btnsProyecto.Controls.Add(btnIngreso);
cod.Text = item.Cod_proyecto.ToString();
nombre_proyecto.Text = item.Nombre_proyecto;
horas.Text = item.Horas.ToString();
encargado.Text = nombre_encargado;
//Se agregan las celdas
fila.Cells.Add(cod);
fila.Cells.Add(nombre_proyecto);
fila.Cells.Add(horas);
fila.Cells.Add(encargado);
fila.Cells.Add(btnsProyecto);
tblProyectos.Rows.Add(fila);
i++;
}
}//fin foreach tbl
}//fin page_load
protected void btnIngresar_Click(Object sender, EventArgs e)
{
Button btn = (Button)sender;
int fila = int.Parse(btn.ID.Substring(btn.ID.Length - 1));
int codProyecto = int.Parse(tblProyectos.Rows[fila].Cells[0].Text);
string url = String.Format("{0}?codProyecto={1}&usuario={2}"
, "IngresoMantencion.aspx"
, codProyecto, this.objUsuario.Nombre);
Response.Redirect(url);
}
I'm from Chile, that's why my code has spanish words. I hope you can help me.
Thanks!

Your button is called Button btnIngreso = new Button(); but you call protected void btnIngresar_Click(Object sender, EventArgs e)
Rename the handler to protected void btnIngreso_Click(Object sender, EventArgs e) and see if it works.
Hope that helps,
Chris

Related

Avoid page refresh after ImageClickEventHandler fire

I have a ImageButton which will show Calendar after it is clicked. I want to stop page refresh when the ImageButton is clicked, but I've tried a couple of ways and none of them work. Is it possible to avoid page refresh and just show the calendar after the Image Button is clicked?
Here is my c# code
TableRow first = new TableRow();
TableCell four = new TableCell();
ImageButton btnDateToggle = new ImageButton();
btnDateToggle.ID = $"btnDateToggle_{j}";
btnDateToggle.ImageUrl = "images/icon2.gif";
btnDateToggle.Click += new ImageClickEventHandler(BtnDateToggle_Click);
Calendar dateddl = new Calendar();
dateddl.ID = $"dateddl_{j}"
dateddl.Width = 250;
dateddl.Visible = false;
dateddl.SelectionChanged += new EventHandler(Dateddl_SelectionChanged);
four.Controls.Add(btnDateToggle);
four.Controls.Add(dateddl);
first.Cells.Add(four);
tasksTable.Rows.Add(first);
ChildPane.ContentContainer.Controls.Add(tasksTable);
ChildAccordion.Panes.Add(ChildPane);
ParentPane.ContentContainer.Controls.Add(ChildAccordion);
MasterAccordion.Panes.Add(ParentPane);
MyContent.Controls.Add(MasterAccordion);
ChildPane and ParentPane are AccordionPane Object. ChildAccordion is an Accordion Object
protected void BtnDateToggle_Click(object sender, EventArgs e)
{
string btnDateToggleID = ((Control)sender).ID;
string index = btnDateToggleID.Substring(btnDateToggleID.IndexOf('_') + 1);
Calendar dateddl = (Calendar)FindControlRecursive(Page, $"dateddl_{index}");
dateddl.Visible = !dateddl.Visible;
}
protected void Dateddl_SelectionChanged(object sender, EventArgs e)
{
Calendar dateddl = (Calendar)sender;
string dateddlID = dateddl.ID;
string index = dateddlID.Substring(dateddlID.IndexOf('_') + 1);
TextBox textBox = (TextBox)FindControlRecursive(Page, $"txtDate_{index}");
dateddl.Visible = false;
textBox.Text = dateddl.SelectedDate.ToShortDateString();//.ToString("MM/dd/yyyy");
}
Here is my client side code (.aspx)
<asp:Content ID="Content1" runat="server">
<div>
<asp:Panel ID="MyContent" runat="server">
<ajaxToolkit:Accordion ID="MasterAccordion" runat="server"> </ajaxToolkit:Accordion>
</asp:Panel>
</div>

Reference non asp checkbox in code behind

I am trying to reference a non-asp check box in C# code behind. The reason the checkbox is not an asp element, is it gets auto-generated on the fly, rather than being a part of the website. So far I have the following relevant aspx:
<asp:Table ID="myTable" runat="server" Width="100%">
<asp:TableRow>
<asp:TableCell>A</asp:TableCell>
<asp:TableCell>B</asp:TableCell>
<asp:TableCell>C</asp:TableCell>
<asp:TableCell>D</asp:TableCell>
<asp:TableCell>E</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:LinkButton runat="server" ID="TEST" CssClass="btn btn-default pull-right" OnClick="TEST_Click">
TEST <i class="m-icon-swapright m-icon-white"></i>
</asp:LinkButton>
And the C# code behind is:
public void GenerateTable()
{
int i = 0;
bool[] box = {true, false, true, false, true};
List<TableRow> tRows = new List<TableRow>();
TableRow newRow = new TableRow();
tRows.Add(newRow);
foreach (var check in box)
{
TableCell tempCell = new TableCell();
if (check)
{
tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" >";
}
else
{
tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" checked>";
}
tRows[0].Cells.Add(tempCell);
i++;
}
foreach (TableRow row in tRows)
{
myTable.Rows.Add(row);
}
}
public void TEST_Click(object sender, EventArgs e)
{
HtmlInputCheckBox chkbox = (HtmlInputCheckBox)FindControl("chk1");
if (chkbox != null)
{
if (!chkbox.Checked)
{
MessageBox.Show("Checked");
}
else
{
MessageBox.Show("NOT Checked");
}
}
else
MessageBox.Show("NOTHING :(");
}
chkbox is always null :(.
You'll need to change two things.
In order to find a checkbox via FindControl it must be part of the pages control collection, which means you have to add a CheckBoxcontrol.
CheckBox c = new CheckBox { ID = "chk" + i };
tempCell.Controls.Add(c);
The dynamically added CheckBox control is part of the control collection of the Table, so you'll have to search for it there instead of on the page.
CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");
Below you find a full update of your code.
protected void Page_Load(object sender, EventArgs e)
{
GenerateTable();
}
public void GenerateTable()
{
int i = 0;
bool[] box = {true, false, true, false, true};
List<TableRow> tRows = new List<TableRow>();
TableRow newRow = new TableRow();
tRows.Add(newRow);
foreach (var check in box)
{
TableCell tempCell = new TableCell();
CheckBox c = new CheckBox { ID = "chk" + i };
c.Checked = check;
tempCell.Controls.Add(c);
tRows[0].Cells.Add(tempCell);
i++;
}
foreach (TableRow row in tRows)
{
myTable.Rows.Add(row);
}
}
public void TEST_Click(object sender, EventArgs e)
{
CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");
if (chkbox != null)
{
if (!chkbox.Checked)
{
MessageBox.Show("Checked");
}
else
{
MessageBox.Show("NOT Checked");
}
}
else
{
MessageBox.Show("NOTHING :(");
}
}

Creating Row Dynamically based on selecting dropdownlist

I written a code for creating dynamically table. the table containing textbox and Dropdownlist.
i try to do creating table based on selected dropdowlist value when i select dropdown list like "Splitter1:5" than Table row will creating 5 . first time it has done successfully when i try to second time i did not get the value of dropdown list.
i am sharing my code please help me.
html code:
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlTextBoxes" runat="server">
<asp:ListItem Value="2" Text="Splitter1:2" />
<asp:ListItem Value="4" Text="Splitter1:4" />
<asp:ListItem Value="8" Text="Splitter1:8" />
<asp:ListItem Value="1" Text="Joint" />
<asp:ListItem Value="0" Text="OLT" />
</asp:DropDownList>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /><br />
<asp:Panel ID="container" runat="server" Visible="false"> <asp:Button ID="btnSubmit" runat="server" Text="Process" OnClick="Submit" /></asp:Panel>
</div>
</form>
</body>
behind code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class testform : System.Web.UI.Page
{
DropDownList DDL; int i;
ArrayList arry_value = new ArrayList(); object[] obj;
Panel pn;
protected void Page_Load(object sender, EventArgs e)
{
//ViewState["droplist"] = obj;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
int numOfTxt = Convert.ToInt32(ddlTextBoxes.SelectedItem.Value);
// DataTable dt = new DataTable();
//dt.Columns.Add("First Textbox");
// dt.Columns.Add("Dropdownlist");
// dt.Columns.Add("Last Textbox");
var table = new Table();
for (i = 0; i < numOfTxt; i++)
{
var row = new TableRow();
var cell = new TableCell();
// var thr = new TableHeaderRow();
// var heading = new TableHeaderCell();
//TableHeaderCell thPNumber = new TableHeaderCell();
// cell.Attributes.Add("runat", "server");
TextBox textbox = new TextBox();
textbox.ID = "Textbox" + i;
textbox.Text = "text" + i;
textbox.Width = new Unit(180);
TextBox textbox1 = new TextBox();
textbox1.ID = "Textbox1" + i;
textbox1.Text = "text1" + i;
textbox1.Width = new Unit(180);
DDL = new DropDownList();
DDL.ID = "DDL1" + i;
DDL.Items.Add("Select Splitter");
DDL.Items.Add(new ListItem("Splitter1:2", "2"));
DDL.Items.Add(new ListItem("Splitter1:4", "4"));
DDL.Items.Add(new ListItem("Splitter1:8", "8"));
DDL.Items.Add(new ListItem("Joint", "1"));
DDL.Items.Add(new ListItem("ONT", "0"));
DDL.AutoPostBack = true;
DDL.TextChanged += dropDown_TextChanged;
// dt.Rows.Add(textbox1);
cell.Controls.Add(textbox1);
cell.Controls.Add(DDL);
cell.Controls.Add(textbox);
//dt.Rows.Add(DDL);
//string val = DDL.SelectedItem.Value;
// string val = DDL.SelectedValue;
// dt.Rows.Add(textbox);
row.Cells.Add(cell);
// row.Cells.Add(thr);
table.Rows.Add(row);
arry_value.Add(DDL.ID);
foreach (object obj in arry_value)
{
var idcollection = new string[] { obj.ToString() };
}
//for (int i1 = 0; i1 < arry_value.Count; i1++)
//{
// //var empty1 = new string[] {arry_value[i].ToString() };
// obj = new object[] { arry_value[i]};
//}
//string count += (DDL.ID.SelectedItem.Value).toString();
}
pn = new Panel();
pn.ID = "pan" + i + 1;
this.form1.Controls.Add(pn);
container.Visible = true;
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Processone";
btnSubmit.Click += new System.EventHandler(Submit);
container.Controls.Add(table);
//pn.Controls.Add(btnSubmit);
// container.Controls.AddAt(0, table);
}
void dropDown_TextChanged(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
}
protected void Submit(object sender, EventArgs e)
{
//Student stud = (Student)ViewState["CurrentStudent"];
//DropDownList ddl = (DropDownList)container.FindControl("DDL1"+i);
// int numOfTxt = 4;
int numOfTxt = 5;
string drpvalue;
for (int j = 0; j < Convert.ToInt32(ddlTextBoxes.SelectedItem.Value); j++)
{
drpvalue = "DDL12" + i + ".SelectedItem.Value";
//int te = Convert.ToInt32(drpvalue);
// numOfTxt += Convert.ToInt32(DDL120.SelectedItem.Value);
}
//int numOfTxt = Convert.ToInt32(ddlTextBoxes.SelectedItem.Value);
var table = new Table();
DataTable dt = new DataTable();
for (int i = 0; i < numOfTxt; i++)
{
var row = new TableRow();
var cell = new TableCell();
// dt.Columns.Add("TextOne1");
cell.Attributes.Add("runat", "server");
TextBox textbox = new TextBox();
textbox.ID = "Textbox" + i;
textbox.Text = "text" + i;
textbox.Width = new Unit(180);
TextBox textbox1 = new TextBox();
textbox1.ID = "Textbox1" + i;
textbox1.Text = textbox1.Text;
textbox1.Width = new Unit(180);
DropDownList DDLs = new DropDownList();
DDLs.ID = "DDL12" + i;
// count= Convert.ToInt32( DDL.ID = "DDL12" + i);
DDLs.Items.Add("Select Splitter");
DDLs.Items.Add(new ListItem("Splitter1:2", "2"));
DDLs.Items.Add(new ListItem("Splitter1:4", "4"));
DDLs.Items.Add(new ListItem("Splitter1:8", "8"));
DDLs.Items.Add(new ListItem("Joint", "1"));
DDLs.Items.Add(new ListItem("ONT", "0"));
//DDLs.AutoPostBack = true;
DDLs.TextChanged += dropDown_TextChanged;
// dt.Rows.Add(textbox1);
cell.Controls.Add(textbox1);
// dt.Columns.Add("ListDropdown");
cell.Controls.Add(DDLs);
// dt.Columns.Add("Textbodx");
cell.Controls.Add(textbox);
row.Cells.Add(cell);
table.Rows.Add(row);
}
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Process";
btnSubmit.Click += new System.EventHandler(Submit);
Panel pans = new Panel();
pans.ID = "panss";
this.form1.Controls.Add(pans);
pans.Controls.AddAt(0, table);
pans.Controls.Add(btnSubmit);
pans.Visible = true;
//pn.Visible = true;
}
}

Not able to store information while postback after creating dynamic dropdown in ASP.NET

here is my code
after button 2 click event it is creating the dropdown in table rows but when I try to save by button 1 click event it just disappear. I have not find any solution regarding this. I have used find control view State etc but it's not helping.
I want to store selected values of dropdown after button_1 click event starts.
public partial class StudentClassSectionMapping : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ClassCode.Enabled = false;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
UpdateModalShowFlag.Value = "true";
Check.Value = "true";
CreateTableRows();
}
private void CreateTableRows()
{
long h = long.Parse(LinkButtonIdCarrier.Value);
List<StudentsClassSectionMapping.StudentsClassSectionMappingForm> allStudentsInClass = StudentsClassSectionMapping.GetStudentsinClass(h);
ClassMaster.ClassMasterForm classCode = Schoolclasses.GetInfo(h);
ClassCode.Text = classCode.cCode;
List<StudentsClassSectionMapping.StudentsClassSectionMappingForm> allSectionsInClass = StudentsClassSectionMapping.GetSectionsinClass(h);
foreach (StudentsClassSectionMapping.StudentsClassSectionMappingForm studentList in allStudentsInClass)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
DropDownList t = new DropDownList();
t.Items.Add("No Section");
foreach (StudentsClassSectionMapping.StudentsClassSectionMappingForm sectionList in allSectionsInClass)
{
t.Items.Add(sectionList.ssSection);
t.Items[t.Items.Count - 1].Value = sectionList.ssSectionID.ToString();
}
t.Attributes.Add("class", "form-control");
t.ID = studentList.ssStudentId.ToString();
cell1.Text = studentList.ssName;
cell2.Text = studentList.ssRegistrationNumber;
cell3.Controls.Add(t);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
Table1.Rows.Add(row);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateTableRows();
long h = long.Parse(LinkButtonIdCarrier.Value);
List<StudentsClassSectionMapping.StudentsClassSectionMappingForm> allStudentsInClass = StudentsClassSectionMapping.GetStudentsinClass(h);
foreach (StudentsClassSectionMapping.StudentsClassSectionMappingForm studentList in allStudentsInClass)
{
DropDownList d = Table1.FindControl(studentList.ssStudentId.ToString()) as DropDownList;
if (d != null)
{
if (d.SelectedIndex != 0)
{
StudentsClassSectionMapping.StudentsClassSectionMappingForm studentSectionMapping = new StudentsClassSectionMapping.StudentsClassSectionMappingForm();
studentSectionMapping.ssClassId = h;
studentSectionMapping.ssStudentId = studentList.ssStudentId;
studentSectionMapping.ssStudentId = long.Parse(d.SelectedItem.Value);
StudentsClassSectionMapping.addSectionStudentMapping(studentSectionMapping);
}
else
{
StudentsClassSectionMapping.StudentsClassSectionMappingForm studentSectionMapping = new StudentsClassSectionMapping.StudentsClassSectionMappingForm();
studentSectionMapping.ssClassId = h;
studentSectionMapping.ssStudentId = 0;
studentSectionMapping.ssStudentId = 0;
StudentsClassSectionMapping.addSectionStudentMapping(studentSectionMapping);
}
}
}
}
It get vanished/disappear because you added it dynamically on page. If you want it back or want to reserver control which is dynamically created you need to recreate again and need to add dynamically.
Here is good example of how you can do it : How to create controls dynamically in ASP.NET and retrieve values from it

Event on dynamically created checkbox asp.net

I started to programming with asp.net, I have a table with some checkboxes.
The problem is, I can't create static tables, because this action is linked with some parameters. Anyway.. When I click on the First checkbox I want to invert the other checkboxes in this table.
How can I catch this event?
<%# Page Title="Fragebogen generieren" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Generate.aspx.cs" Inherits="MAXXREC.Generate" SmartNavigation="True" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2><br /> <br />
<asp:Panel id="pCustomize" runat="server"></asp:Panel> <br /><br />
<asp:Button id="btnSave" class="btn btn-default" Text="Save" runat="server" OnClick="btnSave_Click"></asp:Button>
</asp:Content>
private bool SelectTheData()
{
dtQuestionBlock = taQuestionBlock.GetData();
try
{
int rows = dtQuestionBlock.Rows.Count;
for (int i = 0; i < rows; i++)
{
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.ID = "up" + dtQuestionBlock.Rows[i][1].ToString();
Label lbl = new Label();
lbl.ID = "lbl" + dtQuestionBlock.Rows[i][1].ToString();
lbl.CssClass = "h4";
lbl.Attributes.Add("runat", "server");
lbl.Text = dtQuestionBlock.Rows[i][1].ToString();
pCustomize.Controls.Add(lbl);
pCustomize.Controls.Add(new Literal() { ID = "br" + i, Text = "<br /><br />" });
HtmlTable tbl = new HtmlTable();
tbl.Width = "100%";
tbl.Attributes.Add("class", "table");
tbl.ID = "htmltbl" + dtQuestionBlock.Rows[i][1].ToString();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell hcella = new HtmlTableCell();
CheckBox acb = new CheckBox();
acb.ID = "cb" + dtQuestionBlock.Rows[i]["name"].ToString();
//acb.CheckedChanged += new EventHandler(cb_CheckedChanged);
hcella.Width = "30px";
hcella.Controls.Add(acb);
htr.Cells.Add(hcella);
HtmlTableCell hcellf = new HtmlTableCell();
hcellf.InnerText = "Frage";
hcellf.Style.Add("font-weight", "bold");
hcellf.Style.Add("font-size", "15px");
htr.Cells.Add(hcellf);
tbl.Rows.Add(htr);
string cont = dtQuestionBlock.Rows[i]["ID"].ToString();
dtQuestion = taQuestion.GetDataBy1(Convert.ToInt32(cont));
nCountTables = i;
for (int j = 0; j < dtQuestion.Rows.Count; j++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
acb = new CheckBox();
acb.ID = "cb" + dtQuestion.Rows[j]["content"].ToString();
cell.Width = "30px";
cell.Controls.Add(acb);
tr.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = dtQuestion.Rows[j]["content"].ToString();
cell.ID = "cell" + j + "_" + dtQuestion.Rows[j]["content"].ToString();
tr.Cells.Add(cell);
tbl.Rows.Add(tr);
}
updatePanel.ContentTemplateContainer.Controls.Add(tbl);
//tbl.Visible = false;
pCustomize.Controls.Add(updatePanel);
pCustomize.Controls.Add(new Literal() { ID = "br" + i + rows, Text = "<br />" });
}
return true;
}
catch (Exception ex)
{
Type cstype = ex.GetType();
ClientScriptManager cs = Page.ClientScript;
String cstext = ex.ToString();
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
return false;
}
finally
{
taQuestionBlock.Dispose();
dtQuestionBlock.Dispose();
}
}
you can try this code
List<CheckBox> lstChckBox;
protected void Page_Load(object sender, EventArgs e)
{
// you can create controls programaticaly or html page, doesnt important
//only you should know controls ID and all controls share same checked event
CheckBox chc1 = new CheckBox();
chc1.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc2 = new CheckBox();
chc2.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc3 = new CheckBox();
chc3.CheckedChanged += new EventHandler(chck_CheckedChanged);
// Now, you can create a List so event is fired, you can catch which controls checked or not
lstChckBox = new List<CheckBox>();
lstChckBox.Add(chc1);
lstChckBox.Add(chc2);
lstChckBox.Add(chc3);
}
void chck_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (sender as CheckBox);
foreach (CheckBox item in lstChckBox)
{
if (item != checkBox)
{
item.CheckedChanged -= new EventHandler(chck_CheckedChanged);
item.Checked = !checkBox.Checked;
item.CheckedChanged += new EventHandler(chck_CheckedChanged);
}
}
}

Categories