0

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();
            }
        }
4
  • I have not much Html code, because the most is created dynamically.. but I will post.. one moment Commented Jun 15, 2015 at 8:36
  • Yes, please, get the dynamically generated code! Commented Jun 15, 2015 at 8:37
  • Good. What I asked you to update is, the HTML that was generated in the browser. :) Commented Jun 15, 2015 at 8:44
  • If you want postback event in ASP.NET, then you can bind the event while creating the checkbox. E.g. chkBox1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged); Then implement the protected void CheckBox_CheckChanged(object sender, EventArgs e) {} event handler. Commented Jun 15, 2015 at 8:53

1 Answer 1

1

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);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

thanks!! I think that would work! Nice Idea I will it test and give you a Feedback.
I had added the code.. but when i click on the checkbox, it does'nt jump to my event..
Oh sorry it worked! thanks, I forgot to set AutoPostBack = true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.