在Gridview丢失状态中动态创建单选按钮

时间:2015-02-06 17:11:29

标签: c# asp.net gridview

背景信息:

由于各种原因,我不得不在GridView的一列中动态创建RadioButtonList

网格向用户提供了大量数据,他们必须在这些数据上发表评论并对3个无线电选项中的一个做出决定:

enter image description here

当然,网格上有很多行。这样做的方式是用户在保存到数据库之前对所有项目做出评论和决定。

此屏幕将被多次使用,因此单选按钮必须反映存储在数据库中的值。

问题:

一切都是有效的:单选按钮总是被重置为初始状态(值= 1),因为网格和控件正在回发时重新创建。

代码:

这是工作代码,其中一些是编辑/编辑的......

protected void TheGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        [...]
        foreach (DataControlFieldCell dc in e.Row.Cells)
        {
            switch (dc.ContainingField.ToString())
            {
                [...]
                case (_ColReview):
                    int status = int.Parse(dc.Text);

                    if (status == 0) 
                    {
                        dc.Text = "Not sent for review";
                    }
                    else
                    {
                        var comment = new TextBox();
                        comment.ID = "ReviewCommentTextBox";
                        comment.CssClass = "form-control";
                        comment.Width = 290;
                        dc.Controls.Add(comment);

                        var radioButtons = new RadioButtonList();
                        var rb = new ListItem();
                        if (status == 2) rb.Selected = true;
                        rb.Value = "2";
                        rb.Text = "Yes, Add to Watch List";
                        radioButtons.Items.Add(rb);

                        rb = new ListItem();
                        if (status == 3) rb.Selected = true;
                        rb.Value = "3";
                        rb.Text = "No, do not add to Watch List";
                        radioButtons.Items.Add(rb);

                        rb = new ListItem();
                        //initial value in database is 1, hence this will be initially selected
                        if (status == 1) rb.Selected = true;
                        rb.Value = "1";
                        rb.Text = "Skip: no decision";

                        radioButtons.ID = "RowRadioButtonList";
                        radioButtons.Items.Add(rb);

                        dc.Controls.Add(radioButtons);
                    }
                    break;

            }
        }
    }
}



protected void BulkupdateLinkButton_Click(object sender, EventArgs e)
{
    foreach(GridViewRow gvr in TheGridView.Rows)
    {
        [...]
        int radioItemValue = 0;

        foreach (DataControlFieldCell dc in gvr.Cells)
        {
            string cellName = dc.ContainingField.ToString();
            string cellText = dc.Text;
            switch(cellName)
            {
                [...]
                case (_ColReview):
                    TextBox tb = (TextBox)gvr.FindControl("ReviewCommentTextBox");
                    comment = tb.Text;
                    RadioButtonList rbl = (RadioButtonList)gvr.FindControl("RowRadioButtonList");
                    foreach(ListItem li in rbl.Items)
                    {
                        //Issue arrives here: selected item is reset on postback, value is always 1
                        if (li.Selected)
                        {
                            radioItemValue = ToolBox.ConvertToInt(li.Value);
                        }
                    }

                    break;
            }
        }
        if (!string.IsNullOrEmpty(comment) && (radioItemValue > 0))
        {
            if (radioItemValue != 1) // 1 = pending/skip
            {
                //code to add update the record and save the comment
                [...]
            }
        }



    }
}

关于解决方案的想法

现在我可以通过在每一行中使用HiddenField并设置一些JavaScript / JQuery来记录所选择的RadioButton来解决这个问题,但我无法帮助但是我觉得我错过了一个技巧这里?任何人都可以提供更好/更整洁的解决方案吗?

1 个答案:

答案 0 :(得分:1)

要将RadioButtonList恢复为已保存的选定值,我认为您需要设置列表的 RadioButtonList.SelectedIndex 属性,而不是ListItem的Selected属性