2011-04-17 33 views
0

我正在將一系列從數據庫填充的下拉列表添加到代碼後面的頁面中。因爲我需要可變數量的部分,每部分都包含可變數量的下拉列表,所以我不得不運行我的查詢,然後將構建並將HTML直接放到頁面上。我相信有一個更好的方法來做到這一點(可能嵌套中繼器),但它工作。我的HTML是:下拉列表未出現在表格中

Question 

<input type='hidden' id='h100' /> 
    <select id='q100'> 
     <option>Answer 1</option> 
     <option>Answer 2</option> 
     <option>Answer 3</option> 
    </select> 

但是,當頁面POST回來,我沒有得到這些表單集合中的字段。這很奇怪,因爲他們昨天似乎在那裏,但是我回到頁面並找不到它們。

爲什麼這些不會出現在POST表單集合後?

我使用C#代碼後面,任何幫助,非常感謝。

編輯:這是我背後的代碼(請不要傷害我,我正在學習ASP.NET,因爲我去):

if (!Page.IsPostBack) 
    { 
     // Much stuff that works fine, connecting to database, etc. 

      // Get matching questions - variables 
      ArrayList matchingSections = new ArrayList(); 
      int matchingSectionCount; 

      // Get count of matching sections 
      OracleCommand cmdMatchSectCount = new OracleCommand("Select Count(distinct matching_section) From graphite.question Join graphite.q_matching Using(q_id) Where t_id = " + Session["TestTaking"].ToString(), conn); 
      OracleDataReader drMatchSectCount = cmdMatchSectCount.ExecuteReader(); 
      drMatchSectCount.Read(); 
      matchingSectionCount = (int)drMatchSectCount.GetOracleNumber(0).Value; 
      Session["MatchingSectionCount"] = matchingSectionCount; 

      // Get matching sections 
      OracleCommand cmdMatchSects = new OracleCommand("Select Distinct matching_section From graphite.question Join graphite.q_matching Using(q_id) Where t_id = " + Session["TestTaking"].ToString() + " Order By matching_Section", conn); 
      OracleDataReader drMatchSects = cmdMatchSects.ExecuteReader(); 

      for(int i = 0; i < matchingSectionCount; i++) 
      { 
       drMatchSects.Read(); 
       matchingSections.Add(drMatchSects.GetOracleString(0).Value); 
      } 

      foreach (String s in matchingSections) 
      { 
       string row = string.Empty; 
       int questionCount; 
       ArrayList answers = new ArrayList(); 

       matchManual.InnerHtml += "\n<h2>Matching Section - " + s + "</h2>"; 
       matchManual.InnerHtml += "\n<table>"; 

       OracleCommand cmdQuestionCount = new OracleCommand("Select Count(correct_answer) From graphite.question Join graphite.q_matching Using(q_id) Where t_id = " + Session["TestTaking"].ToString() + " and matching_section = '" + s + "'", conn); 
       OracleDataReader drQuestionCount = cmdQuestionCount.ExecuteReader(); 
       drQuestionCount.Read(); 
       questionCount = int.Parse(drQuestionCount.GetOracleNumber(0).Value.ToString()); 

       OracleCommand cmdMatchAnswers = new OracleCommand("Select correct_answer From graphite.question Join graphite.q_matching Using(q_id) Where t_id = " + Session["TestTaking"].ToString() + " and matching_section = '" + s + "' Order By correct_answer", conn); 
       OracleDataReader drMatchAnswers = cmdMatchAnswers.ExecuteReader(); 
       for (int i = 0; i < questionCount; i++) 
       { 
        drMatchAnswers.Read(); 
        answers.Add(drMatchAnswers.GetOracleString(0).Value.ToString()); 
       } 

       OracleCommand cmdMatchLoop = new OracleCommand("Select q_phrase, q_id From graphite.question Join graphite.q_matching Using(q_id) Where t_id = " + Session["TestTaking"].ToString() + " and matching_section = '" + s + "'", conn); 
       OracleDataReader drMatchLoop = cmdMatchLoop.ExecuteReader(); 
       for (int i = 0; i < questionCount; i++) 
       { 
        drMatchLoop.Read(); 
        row += "\n <tr>"; 
        row += "\n <td>" + drMatchLoop.GetOracleString(0).Value ; 

        row += "<input type='hidden' id='h" + drMatchLoop.GetOracleNumber(1).Value.ToString() + "' />"; 
        row += "\n </td>"; 
        row += "\n <td>"; 
        row += "\n  <select id='q" + drMatchLoop.GetOracleNumber(1).Value.ToString() + "' runat='server'>"; 

        foreach(string answer in answers) 
        { 
         row += "\n  <option>" + answer + "</option>"; 
        } 

        row += "\n  </select>"; 
        row += "\n </td>"; 

        row += "\n </tr>"; 
       } 

       matchManual.InnerHtml += row; 
       matchManual.InnerHtml += "\n</table>\n\n"; 
    } 
+1

請在後面顯示您的代碼。你不是在使用'IsPostback'嗎?順便說一句:你在頁面上的任何地方都有'form'標籤嗎? – Damb 2011-04-17 02:11:12

+0

目前它運行在一個按鈕點擊事件。這是張貼回來;我可以在窗體集合中看到其他控件(我正在使用中繼器添加)。這是一個問題嗎?編輯:是的,有一個「表格」標籤。 – 2011-04-17 02:17:26

+0

我不確定我是否瞭解您的情況。但是如果你在按鈕點擊時生成這些表單元素。這可能是問題的根源。 – Damb 2011-04-17 02:18:47

回答

2

哇。只是......哇。與<select>,你必須包括一個name='value',而不是id='value'。道德:瞭解你的HTML。並且儘量設法避免將原始HTML分發到頁面上。

0

我認爲你Page.IsPostBack條件是問題根源。因爲您僅在!IsPostback =>上生成元素,只在'第一次'時生成元素,而不是在post =>之後生成元素。

+0

我試着把它拿出來,但沒有解決它。 POST後它們不會丟失;如果我留在頁面上(我目前正在進行測試),那麼就有兩個元素的副本。 – 2011-04-17 02:32:31

+3

你將不得不分享你的代碼。或者我們可以繼續猜測:) – Damb 2011-04-17 02:37:24

1

如果它們在提交時不在formcollection中,我認爲問題在於它們不在被子引用的表單標籤內,或者您必須創建表單標籤並將select標籤放入其中。

@edited

啊哈 - 所以這是一個形式中,看到現在的原因是在選擇標籤丟失的name屬性!

表單集合在發佈時使用屬性名稱值作爲鍵。

+0

整個頁面在'form'標籤內。 – 2011-04-19 01:13:33