我有一个代码
public void LoadProducts()
{
//StringBuilder sbProducts = new StringBuilder();
string qry = "Select * from tbl_Products order by ProductId";
SqlCommand cmd = new SqlCommand(qry, con);
con.Open();
sbProducts="<table cellpadding='0' align='center' height: '250px'; width: '1000px'><tr>";
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sbProducts = sbProducts + "<form action='ProductDetail.aspx'><td style='border-right:1px solid
blue;border-top:1px solid blue;border-bottom:1px solid
blue;border-left:1px solid blue;text-align:center;width:300px'>";
Session["code"] = sdr[0].ToString();
sbProducts = sbProducts + "<img src= " + sdr[5].ToString().Substring(2) + " width=120px height=150px><br>";
sbProducts = sbProducts + sdr[0].ToString() + sdr[2].ToString() + "<br>";
sbProducts = sbProducts + sdr[3].ToString() + "<br>";
sbProducts = sbProducts + "Rs: " + sdr[4].ToString();
sbProducts = sbProducts + "<br><input type='hidden' name='pid' value='" + Session["code"] + "'><input type='submit'
value='View Details'>";
sbProducts = sbProducts + "</td></form>";
}
sbProducts = sbProducts + "</table>";
CellTwo = sbProducts.ToString();
con.Close();
}
}
它包含一个需要在每次迭代时重复但在第一次迭代时重复的表单,而循环缺少此表单(html)元素,其余的迭代工作正常。 我不知道问题是什么,为什么while循环在第一次迭代中缺少表单元素。
答案 0 :(得分:1)
请将您的html输出分享给我们
从外观上看,你会产生这样的东西:
<table>
<tr>
<form>
<td></td>
</form>
</table>
你没有关闭tr,表格应该在td中,而不是在tr和td之间。
结果应如下所示:
<table>
<tr>
<td>
<form></form>
</td>
</tr>
</table>
答案 1 :(得分:0)
你可以这样使用
public void LoadProducts()
{
//StringBuilder sbProducts = new StringBuilder();
string qry = "Select * from tbl_Products order by ProductId";
SqlCommand cmd = new SqlCommand(qry, con);
con.Open();
sbProducts="<table cellpadding='0' align='center' height: '250px'; width: '1000px'><tr>";
int counter=0;
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
if(counter++=0)
{
sbProducts = sbProducts + "<form action='ProductDetail.aspx'><td style='border-right:1px solid
blue;border-top:1px solid blue;border-bottom:1px solid
blue;border-left:1px solid blue;text-align:center;width:300px'>";
}
sbProducts = sbProducts +"<tr>";
Session["code"] = sdr[0].ToString();
sbProducts = sbProducts + "<img src= " + sdr[5].ToString().Substring(2) + " width=120px height=150px><br>";
sbProducts = sbProducts + sdr[0].ToString() + sdr[2].ToString() + "<br>";
sbProducts = sbProducts + sdr[3].ToString() + "<br>";
sbProducts = sbProducts + "Rs: " + sdr[4].ToString();
sbProducts = sbProducts + "<br><input type='hidden' name='pid' value='" + Session["code"] + "'><input type='submit'
value='View Details'>";
sbProducts = sbProducts + "</td>";
sbProducts = sbProducts +"</tr>";
}
//sbProducts = sbProducts + "</table>";
// CellTwo = sbProducts.ToString();
con.Close();
}
sbProducts = sbProducts + "</table>";
CellTwo = sbProducts.ToString();
}
同样,你可以在while循环之外添加</form>
和table。