0

ラジオボタンがチェックされているかどうかを確認する必要があります。その値を変数に設定する必要があります。

ボタンのクリック時にユーザーがTrueまたはFalseを選択し、ボタンがグリッドビューとリピーターの外側に配置されていることを確認するために、リピーターでradioButtonListをループするにはどうすればよいですか。

私はこれを試しました: protected void btnsave_Click(object sender, EventArgs e) { if (!Page.IsValid) return; int カウンター = 0; int fcounter = 0;

        for (int i = 0; i < gridMainSurveyQuestion.Rows.Count; i++)
        {
            GridView grid = (GridView)gridMainSurveyQuestion.Rows[i].FindControl("RptQuestions");

            Repeater repea = (Repeater)grid.Rows[i].FindControl("RptQuestions");
            for (int j = 0; j < repea.Items.Count; j++)
            {
                RepeaterItem currentitem = repea.Items[j];
                RadioButtonList rlist = (RadioButtonList)currentitem.FindControl("rblanswer");
                if (rlist.SelectedItem.Value == "0")
                {
                    fcounter++;
                    lblfalse.Text = fcounter.ToString();
                }
                if (rlist.SelectedItem.Value == "1")
                {
                    tcounter++;
                    lbltrue.Text = tcounter.ToString();
                }
            }
        }
    }

しかし、エラーが表示されます: タイプ 'System.Web.UI.WebControls.Repeater' のオブジェクトをタイプ 'System.Web.UI.WebControls.GridView' にキャストできません。

説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

例外の詳細: System.InvalidCastException: タイプ 'System.Web.UI.WebControls.Repeater' のオブジェクトをタイプ 'System.Web.UI.WebControls.GridView' にキャストできません。

ソース エラー:

89 行目: for (int i = 0; i < gridMainSurveyQuestion.Rows.Count; i++) 90 行目: { 91 行目: GridView grid = (GridView)gridMainSurveyQuestion.Rows[i].FindControl("RptQuestions"); 92 行目: 93 行目: リピーター repea = (Repeater)grid.Rows[i].FindControl("RptQuestions");

ソース ファイル: C:\Users\madiha.rahman\Desktop\PICG_SurveyModule\PICG_SurveyModule\Survey.aspx.cs 行: 91

修正できますか

4

1 に答える 1

1

各リピーターを見つけるために、最初に GridView 行を反復処理する必要があります。次に、以下に示すように、各リピーターから各ラジオボタンリストを見つけます。

  protected void Button1_Click(object sender, EventArgs e)
  {
        foreach (GridViewRow gvRow in gvTemp.Rows)
        {
            Repeater repeater = (Repeater)gvRow.FindControl("repeater1");

            foreach (RepeaterItem repItem in repeater.Items)
            {
                RadioButtonList rbList = (RadioButtonList)repItem.FindControl("radiobuttonlist1");

                foreach (ListItem item in rbList.Items)
                {
                    if (item.Selected)
                    {
                        //code for selected items goes here...
                    }
                    else
                    {
                        //code for not selected items goes here...
                    }

                    if (item.Value == "0")
                    { 
                        //code for items with value == "0" goes here...
                    }

                    if (item.Value == "1")
                    {
                        //code for items with value == "1" goes here...
                    }
                }
            }
        }
  }

ハッピーコーディング...;)

編集:質問者の要求に応じて、チェックボックスを削除し、ラジオボタンリストを配置しました。

EDIT :質問者の要求に応じて、radiobuttonlist 内の各ラジオボタンを反復処理する内部 foreach ループを追加しました。

于 2012-11-20T07:53:07.093 に答える