2017-09-23 2 views
0

私はC#で少し経験がありますが、私は物事のASP側では新しく、調査プロジェクトに取り掛かっていますMSSQLサーバーから質問/回答を取得し、質問に必要な回答の種類(チェックボックス、ラジオ、ドロップダウンメニュー、テキスト入力)を適切なコントロールに読み込み、その質問に対する可能な回答をListItemとして追加し、単一のアンケートの質問ウェブページ内にあるプレースホルダに移動します。回答提出時に、プレースホルダに次の質問が再設定され、回答タイプに適切なコントローラが読み込まれます。C#/ ASP.NETの動的に作成されたコントロールから選択されたチェックボックス/ラジオとテキスト入力を取得する

私は現在、チェックボックス/ラジオ/テキスト入力のいずれかを参照することができないため、送信ボタンをクリックしたときにユーザーの回答が何であれ取得しようとすると、多くの問題を抱えています選択されたかどうか。

各行のデバッグとステップ実行とローカル変数の監視で、何が起きているのかを確認しながら、this seems to be the hierarchy of the inserted answers at the time they are all added to the placeholder

私は、すべての項目をチェックするためにsubmitButtonの中でforEachループを使用しようとしましたが(特に以下のコード例で残しましたが)、全くアクセスできないようです。 2番目のsubmitButtonが押され、submitButtonメソッドの何かが実行される前に、回答が破棄されたようです。

Question.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
    { 


     .... 



       else if (questionType == 2) //checkbox 
       { 
        CheckBoxQuestionController checkBoxController = (CheckBoxQuestionController)LoadControl("~/CheckBoxQuestionController.ascx"); 

        checkBoxController.ID = "checkBoxQuestionController"; 
        checkBoxController.QuestionLabel.Text = questionText; 

        SqlCommand optionCommand = new SqlCommand("SELECT * FROM answerOptionTable WHERE answerOptionTable.q_Id = " + currentQuestion, connection); 

        //run command 
        SqlDataReader optionReader = optionCommand.ExecuteReader(); 

        //loop through all results 
        while (optionReader.Read()) 
        { 
         ListItem item = new ListItem(optionReader["answerText"].ToString(), optionReader["a_Id"].ToString()); 
         int currentAnswerId = Convert.ToInt32(optionReader["a_Id"]); 

         checkBoxController.QuestionCheckBoxList.Items.Add(item); //add answer to list 
        } 

        QuestionPlaceholder.Controls.Add(checkBoxController); 

       } 


      //other questionType checking here 

      connection.Close(); 
    } 

protected void SubmitButtonClick(object sender, EventArgs e) 
    { 
     //template.Items. 
     Control resultControl = FindControl("checkBoxQuestionController"); 

     //test 1 
     CheckBoxList resultControl2 = (CheckBoxList)FindControl("checkBoxQuestionController"); 

     CheckBoxList resultControl3 = (CheckBoxList)FindControl("questionCheckBoxList"); 

     //test 123213 
     CheckBoxList Cbx = (CheckBoxList)QuestionPlaceholder.FindControl("checkBoxQuestionController"); 

     //test 2 
     //for (int i = 0; i < QuestionPlaceholder.Controls.Count; i++) 
     //{ 
     // if (QuestionPlaceholder.Controls[i].GetType() == typeof(CheckBoxList)) 
     // { 
     //  CheckBoxList myList = QuestionPlaceholder.Controls[i].GetType(); 
     // } 
     //} 

     //test 3 
     //foreach (ListItem cbList in QuestionPlaceholder.Controls.("checkBoxQuestionController") 
     //{ 
     // if (cbList.Selected) 
     // { 

     // } 
     //} 
     //test 4 
     //foreach (ListItem cb in QuestionPlaceholder.Controls.OfType<ListItem>()) 
     //{ 
     // if (cb != null) 
     // { 

     // } 
     //} 

     int count = 0; 

     List<ListItem> selected = new List<ListItem>(); 
     foreach (ListItem item in debugList.Items) 
     { 
      if (item.Selected) 
      { 
       count++; 
      } 
     } 
      Response.Redirect("Question.aspx"); 
    } 

CheckBoxQuestionController.ascx.cs

public partial class CheckBoxQuestionController : System.Web.UI.UserControl 
{ 
    //Getters and setters 
    public Label QuestionLabel 
    { 
     get 
     { 
      return questionLabel; 
     } 
     set 
     { 
      questionLabel = value; 
     } 
    } 


    public CheckBoxList QuestionCheckBoxList 
    { 
     get 
     { 
      return questionCheckBoxList; 
     } 
     set 
     { 
      questionCheckBoxList = value; 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

CheckBoxQuestionController.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CheckBoxQuestionController.ascx.cs" Inherits="Survey_Prototype.CheckBoxQuestionController" %> 

    <div class="bodyTitle"> 
     <asp:Label ID="questionLabel" runat="server" Text="LabelText"></asp:Label> 
    </div> 
    <div class="answerOptionContainer"> 
     <asp:CheckBoxList ID="questionCheckBoxList" runat="server"> 
     </asp:CheckBoxList> 
    </div> 

答えて

0

ないあなたは直接のCheckBoxListコントロールを使用していない理由を確認してください独自のコントロールを使用する

あなた自身のコントロールを使用する場合、ビューステートは自分で処理する必要があります。そうしないと、あなたのケースでは

、question.aspx.csではUCのコントロールセットにアクセスし、しかし、ページのポストバック時には、サブコントロールが作成される前にビューステートが適用されているので、実際にユーザーの選択が失われるため、追加したすべてのサブコントロールがなくなります。

すべての変更コードをコントロール次のオーバーライドされたメソッドに基底を削除しないでください

あなたの子コントロール(ここではQuestionCheckBoxListです)はg ViewStateが適用される前に実行されると、ユーザーの選択は保持され、オプションを使用して選択されたものを表示する方法は何でも使用できます。

 protected override void CreateChildControls() 
     { 
      base.CreateChildControls(); 
     } 
関連する問題