1

我有一个运行 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>
4

1 回答 1

1

你有两个问题。1)您没有正确构建表格(您缺少 a </tr>) 2)该表格可能在另一个表格中。您的代码(稍微清理一下)应如下所示:

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

一旦您确认您没有在另一个表单中执行此操作,您将看到它按预期出现。一个表单不能包含另一个表单,它是一个 HTML 标准。

于 2013-10-03T14:18:59.850 回答