我将从数据库填充的一系列下拉列表添加到后面的代码页面中。因为我需要一个可变数量的部分,每个部分包含可变数量的下拉列表,所以我必须运行我的查询然后放置构建并将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";
}
答案 0 :(得分:2)
哇。只是......哇。使用<select>
,您必须包含name='value'
,而不是id='value'
。道德:了解你的HTML。并且更加努力地寻找避免将原始HTML喷射到页面上的方法。
答案 1 :(得分:1)
如果提交时表单集中没有它们,我认为问题是它们不在提交的表单标记内,或者您必须创建表单标记并将select标记放入其中。
@edited
啊哈 - 所以它在一个表单中,现在看看原因是select标签中缺少name属性!
表单集合在发布时使用属性名称值作为键。
答案 2 :(得分:0)
我认为您的Page.IsPostBack
条件是问题的根源。因为您只在生成元素!IsPostback =&gt;只有'第一'时间,而不是在帖子=&gt;之后在帖子之后他们失踪了。