【问题标题】:Event on dynamically created checkbox asp.net动态创建的复选框 asp.net 上的事件
【发布时间】:2017-04-19 23:19:09
【问题描述】:

我开始用 asp.net 编程,我有一个带有一些复选框的表格。

问题是,我无法创建静态表,因为此操作与某些参数相关联。无论如何..当我单击第一个复选框时,我想反转此表中的其他复选框。 我怎样才能捕捉到这个事件?

<%@ 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();
            }
        }

【问题讨论】:

  • 我没有太多的Html代码,因为大部分是动态创建的..但我会发布..一会儿
  • 是的,请获取动态生成的代码!
  • 好。我要求您更新的是在浏览器中生成的 HTML。 :)
  • 如果你想在 ASP.NET 中回发事件,那么你可以在创建复选框时绑定事件。例如。 chkBox1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged); 然后实现protected void CheckBox_CheckChanged(object sender, EventArgs e) {} 事件处理程序。

标签: c# jquery asp.net checkbox


【解决方案1】:

你可以试试这个代码

 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);
            }
        }
    }

【讨论】:

  • 谢谢!!我认为那会奏效!好主意,我会对其进行测试并给您反馈。
  • 我已经添加了代码..但是当我点击复选框时,它不会跳转到我的事件..
  • 抱歉,它成功了!谢谢,我忘了设置 AutoPostBack = true。
猜你喜欢
  • 2018-09-20
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 2017-10-02
相关资源
最近更新 更多