2017-09-23 41 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循環(除此之外,在下面的代碼示例中留下它)來檢查所有項目,但似乎無法訪問它..在各種測試之後看起來答案被摧毀了第二個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(); 
     } 
相關問題