不是在while循环第一次迭代ASP.NET中读取表单元素

时间:2013-10-03 13:48:49

标签: javascript asp.net

我有一个运行5次的while循环:

string qry = "Select * from tbl_Products order by ProductId";
SqlCommand cmd = new SqlCommand(qry, con);
con.Open();
sbProducts="<table><tr>"; 
using (SqlDataReader sdr = cmd.ExecuteReader()) 
{ 
    while (sdr.Read()) 
    {
        sbProducts = sbProducts + "<td>";
        sbProducts = sbProducts + "<Form>";
        sbProducts = sbProducts + "123";
        sbProducts = sbProducts + "</Form>";
        sbProducts = sbProducts + "</td>"; 
    }
    sbProducts = sbProducts + "</table>";
    CellTwo = sbProducts.ToString();
    con.Close(); 
}

它的输出很好,但它没有包装第一次迭代TD没有换形式标签。 这似乎非常不合逻辑。输出如下:

<table>
<tr>
<td>123</td>
<td><form>123</form></td>
<td><form>123</form></td>
<td><form>123</form></td>
<td><form>123</form></td>


</tr>
</table>

1 个答案:

答案 0 :(得分:1)

你有两个问题。 1)您没有正确构建表格(您错过了</tr>) 2)该表可能在另一种形式中。您的代码(略微清理)应如下所示:

while (sdr.Read()) 
{
    sbProducts += "<td>";
    sbProducts += "<Form>";
    sbProducts += "123";
    sbProducts += "</Form>";
    sbProducts += "</td>"; 
}
sbProducts += "</tr></table>"; //You were missing this </tr>

一旦您确认在另一个表单中执行,您就会看到它按预期出现。表单不能包含其他表单,它是HTML标准。