开发者

Dropdown list not appearing in Form

I'm adding a series of dropdown lists populated from a database to a page from code behind. Because I need a variable number of sections with each containing a variable number of dropdown lists, I'm having to run my queries and then put build and place the HTML straight onto the page. I'm sure there's a better way to do this (nested repeaters, possibly) but it is working. My HTML is:

Question

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

However, when the page POSTs back, I'm not getting these fields in the form collection. It's really odd because they seemed to be there yesterday, but then I come back to the page and can't find them.

Why would these not be appearing in the form collection after POST?

I'm using C# for code behind, any help is very appreciated.

Edit: Here is my code behind (please don't hurt me, I'm learning ASP.NET as I go):

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);
            O开发者_运维问答racleDataReader 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";
   }


Wow. Just...wow. With <select>, you have to include a name='value', not id='value'. Moral: know your HTML. And try harder to find ways to avoid spewing raw HTML onto a page.


If they are not present in the formcollection when submiting, I think the problem is they are not within a form tag that is submited, or you have to create the form tag and place the select tag in it.

@edited

Aha - so it was within a form, see now that the reason was the missing name attribute in the select tag!

The form collection uses the attribute name values as keys when posted.


I think that your Page.IsPostBack condition is source of problems. Because you are generating elements only on !IsPostback => only 'first' time, not after the post => they are missing after the post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜