我正在將一系列從數據庫填充的下拉列表添加到代碼後面的頁面中。因爲我需要可變數量的部分,每部分都包含可變數量的下拉列表,所以我不得不運行我的查詢,然後將構建並將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";
}
請在後面顯示您的代碼。你不是在使用'IsPostback'嗎?順便說一句:你在頁面上的任何地方都有'form'標籤嗎? – Damb 2011-04-17 02:11:12
目前它運行在一個按鈕點擊事件。這是張貼回來;我可以在窗體集合中看到其他控件(我正在使用中繼器添加)。這是一個問題嗎?編輯:是的,有一個「表格」標籤。 – 2011-04-17 02:17:26
我不確定我是否瞭解您的情況。但是如果你在按鈕點擊時生成這些表單元素。這可能是問題的根源。 – Damb 2011-04-17 02:18:47