开发者

ASP.Net radio button list

im trying to create a feedback form, i have a some mock code which looks similar to this

    for (int i = 1; i < 3; i++)
    {
        TableRow tr = new TableRow();
        Table1.Rows.Add(tr);
        TableCell QuestionCell开发者_如何学Python = new TableCell();

        // get the text for the question and stick it in the cell
        QuestionCell.Text = "<b>"+i+".</b> Question " + i;
        tr.Cells.Add(QuestionCell);

        TableRow tr2 = new TableRow();
        Table1.Rows.Add(tr2);

        // create a cell for the choice

        TableCell ChoicesCell = new TableCell();
        ChoicesCell.Width = 1000;

        // align the choices on the left
        ChoicesCell.HorizontalAlign = HorizontalAlign.Left;
        tr2.Cells.Add(ChoicesCell);

        RadioButtonList rbl = new RadioButtonList();
        rbl.ID = "Radio1_" + i;
        ChoicesCell.Controls.Add(rbl);

        rbl.Items.Add("1");
        rbl.Items.Add("2");
        rbl.Items.Add("3");
        rbl.Items.Add("4");
    }

obviously this code doesn't mean anything, its just to experiment on how i can do this feedback form, the problem im having now is once some one presses the submit button (there's a submit button on the form) how do i go through the table and get text from the radio buttons the user has selected ?? the feedback from in created on page_load !!

thanks for any help !!

EDIT so i have this code once the button is pressed

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (TableCell cell in Table1.Rows)
    {
        foreach (Control ctrl in cell.Controls)
        {
            if (ctrl is RadioButtonList)
            {
                if (ctrl.selected) // this doesnt works 
                {
                    string selected = ctrl.text // this doesnt work either 
                }
            }
        }
    }
}

this doesn't seem to be working ... i don't know where im going wrong !!


I would think that you would have to cast the Control inside your loop to be a RadioButtonList.

Also, try using the SelectedValue property instead of selected

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (TableCell cell in Table1.Rows)
    {
        foreach (Control ctrl in cell.Controls)
        {
            if (ctrl is RadioButtonList)
            {
                string selected = ((RadioButtonList)ctrl).SelectedValue;
            }
        }
    }
}


Do you have EnableViewState=false for those controls. The server cannot see the changed by the user state of the controls after postback if their ViewState is set to false


Since you are creating controls the way you are (pretty much on the fly) you will need to re-create these every page request in Page_Init and maintain state yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜