Sambyal Amit
Sambyal Amit

Reputation: 1

Fire checked changed event of dynamically created radio buttons?

In the below code Radio buttons are created dynamically, But I want to create the select index change event of КadioИuttons created dynamically and handle it.....below is the code not firing the event.

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

Upvotes: 0

Views: 1557

Answers (2)

TalentTuner
TalentTuner

Reputation: 17556

Simple attach an eventhandler for checked change event

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

Upvotes: 1

user1193035
user1193035

Reputation:

You are setting AutoPostBack is false for radio button .So,

please change false to true

oRbOptions.AutoPostBack = true;

Upvotes: 0

Related Questions