【问题标题】:Fire checked changed event of dynamically created radio buttons?触发检查动态创建的单选按钮的更改事件?
【发布时间】:2013-03-06 03:18:27
【问题描述】:

在下面的代码中,单选按钮是动态创建的,但我想创建动态创建的 КadioИuttons 的选择索引更改事件并处理它.....下面是不触发事件的代码。

DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get",sQuestions);

if (ds.Tables.Count > 0 ?? ds.Tables[0].Rows.Count > 0)
{
    int iCnt = 0;
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        Table tblQsn = new Table();

        //.....Begin Text Qsn Creation.....//
        tblQsn.Width = 500;

        TableRow trQsn = new TableRow();
        iRowCounter++;
        trQsn.ID = "Row_" + iRowCounter.ToString();

        TableCell tcQsn = new TableCell();
        TableCell tcQsnSNo = new TableCell();

        tcQsn.CssClass = "Label";
        tcQsn.BackColor = System.Drawing.Color.Gainsboro;
        tcQsn.Font.Bold = true;
        tcQsn.Text = ds.Tables[0].Rows[i][1].ToString();
        tcQsn.Width = Unit.Percentage(99.5);
        iCellCounter++;
        tcQsn.ID = "Cell_" + iCellCounter.ToString();

        tcQsnSNo.CssClass = "Label";
        tcQsnSNo.Attributes.Add("valign", "top");
        tcQsnSNo.BackColor = System.Drawing.Color.Gainsboro;
        tcQsnSNo.Font.Bold = true;
        tcQsnSNo.Width = Unit.Percentage(0.5);
        iCellCounter++;
        tcQsnSNo.ID = "Cell_" + iCellCounter.ToString();
        iCnt++;
        tcQsnSNo.Text = iCnt.ToString() + ".";

        trQsn.Cells.Add(tcQsnSNo);
        trQsn.Cells.Add(tcQsn);
        tblQsn.Rows.Add(trQsn);

        int rcnt = i;
        int iOptCnt = 0;
        string sStatus = "N";
        while ((rcnt >= 0) && (rcnt < ds.Tables[0].Rows.Count))
        {
            if (ds.Tables[0].Rows[rcnt][2].ToString() == ds.Tables[0].Rows[i][2].ToString())
            {
                if (sStatus == "N")
                {
                    sStatus = "Y";
                }

                TableRow trQsnOpt = new TableRow();
                iRowCounter++;
                trQsnOpt.ID = "Row_" + iRowCounter.ToString();
                TableCell tcQsnOpt = new TableCell();
                tcQsnOpt.CssClass = "Label";
                iCellCounter++;
                tcQsnOpt.ID = "Cell_" + iCellCounter.ToString();
                tcQsnOpt.Attributes.Add("valign", "top");
                tcQsnOpt.VerticalAlign = VerticalAlign.Middle;
                TableCell tcQsnOptSNo = new TableCell();
                tcQsnOptSNo.CssClass = "Label";
                iCellCounter++;
                tcQsnOptSNo.ID = "Cell_" + iCellCounter.ToString();

                iOptCnt++;
                RadioButton oRbOptions = new RadioButton();
                oRbOptions.GroupName = ds.Tables[0].Rows[rcnt][2].ToString() + "_Group";
                oRbOptions.Text = ds.Tables[0].Rows[rcnt][3].ToString().Trim();
                iRbTCounter++;
                oRbOptions.ID = ds.Tables[0].Rows[i][0].ToString() + "_" + ds.Tables[0].Rows[rcnt][2].ToString() + "_" + "Option" + iOptCnt.ToString() + "_" + iRbTCounter.ToString();
                oRbOptions.Enabled = true;

                if (ds.Tables[0].Rows[i][2].ToString() == "Option" + iRbTCounter.ToString())
                {
                    oRbOptions.Checked = true;
                }

                oRbOptions.CssClass = "Label";
                tcQsnOpt.Controls.Add(oRbOptions);
                oRbOptions.CheckedChanged += new System.EventHandler(CheckedChanged);
                oRbOptions.AutoPostBack = false;
                tcQsnOptSNo.Text = iOptCnt.ToString() + ".";
                trQsnOpt.Cells.Add(tcQsnOptSNo);
                trQsnOpt.Cells.Add(tcQsnOpt);
                rcnt++;
                //.....Add Option Image.....//
                tblQsn.Rows.Add(trQsnOpt);
            }
            else
                break;
        }
        i = rcnt - 1;
        PlPreview.Controls.Add(tblQsn);
    }
}

【问题讨论】:

    标签: c# asp.net .net events radio-button


    【解决方案1】:

    简单地为检查的更改事件附加一个事件处理程序

    oRbOptions.CheckedChanged += (s,e) => {
        oRbOptions.AutoPostBack = true;
    }
    

    【讨论】:

    • oRbOptions.CheckedChanged += new System.EventHandler(CheckedChanged); oRbOptions.CheckedChanged += (s, e) => { oRbOptions.AutoPostBack = true; }; My Event protected void CheckedChanged(object sender, EventArgs e) { } Still not working
    【解决方案2】:

    您正在为单选按钮设置 AutoPostBack 为 false。所以,

    请把假改为真

    oRbOptions.AutoPostBack = true;
    

    【讨论】:

    • 谢谢!当我更改 oRbOptions.AutoPostBack = true;它发回页面但仍然没有触发事件。
    猜你喜欢
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多