簡體   English   中英

防火檢查動態創建的單選按鈕的已更改事件?

[英]Fire checked changed event of dynamically created radio buttons?

在下面的代碼中,單選按鈕是動態創建的,但是我想創建動態創建的К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);
    }
}

只需附加一個事件處理程序即可檢查更改事件

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

您將單選按鈕的AutoPostBack設置為false。

請將false更改為true

oRbOptions.AutoPostBack = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM