0

これは私のグリッドビューがどのように見えるかです:

基本的には「あなた」を正解とします。

ここに画像の説明を入力

しかし、「+」ボタンをクリックすると、ラジオボタンの選択が削除されました。 ここに画像の説明を入力

グリッドビューのすべての行でラジオ ボタンの選択を維持するにはどうすればよいですか?

これは私のコードです:

 protected void Page_Load(object sender, EventArgs e)
 {

     if (!Page.IsPostBack)
     {
         SetInitialRow();
         dropActivity(); // ignore this , this is for drop down list
         dropTask();  // ignore this , this is for drop down list

     }
 }

private void SetInitialRow()
{
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("Question", typeof(string)));
    dt.Columns.Add(new DataColumn("Hints No.1", typeof(string)));
    dt.Columns.Add(new DataColumn("Hints No.2", typeof(string)));

    dr = dt.NewRow();
    dr["Question"] = string.Empty;
    dr["Hints No.1"] = string.Empty;
    dr["Hints No.2"] = string.Empty;

    dt.Rows.Add(dr);

    //Store the DataTable in ViewState
    ViewState["CurrentTable"] = dt;

    GridView1.DataSource = dt;
    GridView1.DataBind();
}

private void AddNewRowToGrid()
{
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values
                TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                drCurrentRow = dtCurrentTable.NewRow();

                dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
                dtCurrentTable.Rows[i - 1]["Hints No.1"] = box2.Text;
                dtCurrentTable.Rows[i - 1]["Hints No.2"] = box3.Text;

                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            GridView1.DataSource = dtCurrentTable;
            GridView1.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    //  Set Previous Data on Postbacks
    SetPreviousData();
}

private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                RadioButton radiobtn1 = (RadioButton)GridView1.Rows[i].Cells[2].FindControl("RadioButton1");
                RadioButton radiobtn2 = (RadioButton)GridView1.Rows[i].Cells[3].FindControl("RadioButton2");

                //Setting previous text to the respective textboxes based on columns.
                box1.Text = dt.Rows[i]["Question"].ToString();
                box2.Text = dt.Rows[i]["Hints No.1"].ToString();
                box3.Text = dt.Rows[i]["Hints No.2"].ToString();

                if (radiobtn1.Checked == true)
                {
                    ViewState["RadioButtonStatus"] = radiobtn1.Checked;
                }

                if (radiobtn2.Checked == true)
                {
                    ViewState["RadioButtonStatus"] = radiobtn2.Checked;
                }

                Session["Question1"] = box1.Text;
                rowIndex++;
            }
        }
    }
}

基本的に、作成したすべての行のラジオ ボタンの選択を維持する必要があります。

4

1 に答える 1

0

「+」ボタンを変更して、新しい行をサーバーにポストするのではなく、クライアント側でイベントをトリガーすることを検討してください。サーバー上の「保存」イベントハンドラーに投稿するときに、コレクションをループすることができます。

現在発生しているように見えるのは、「+」ボタンを押すことからのポストバックです。ポストバックにより、これらのラジオ ボタンがクリアされます。ViewState を介してステータスを保持しようとしているようですが、同じ ViewState プロパティのチェック済みステータスを保存しています。

以下のコードの違いを参照してください。

    if (radiobtn1.Checked == true)
    {
        ViewState["RadioButtonStatus"] = radiobtn1.Checked;
    }

    if (radiobtn2.Checked == true)
    {
        ViewState["DifferentRadioButtonStatus"] = radiobtn2.Checked;
    }

AddNewRow メソッドにいくつかの更新があります。ここでは多くのことが行われていますが、これは少し過剰に設計されている可能性があります。正しい方向に進むのに役立つことを願っています。

private void AddNewRowToGrid()
{
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

            var drCurrentRow = dtCurrentTable.NewRow();
            dtCurrentTable.Rows.Add(drCurrentRow);

        // Updated from > 0 to > 1 since the first action you're taking is in GridView1.Rows[1] via the for loop below
        if (dtCurrentTable.Rows.Count > 1)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values
                TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
                dtCurrentTable.Rows[i - 1]["Hints No.1"] = box2.Text;
                dtCurrentTable.Rows[i - 1]["Hints No.2"] = box3.Text;

                rowIndex++;
            }


            ViewState["CurrentTable"] = dtCurrentTable;

            GridView1.DataSource = dtCurrentTable;
            GridView1.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    //  Set Previous Data on Postbacks
    SetPreviousData();
}
于 2013-06-04T02:29:56.697 に答える