我有一个代码
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();
}
}
它包含一个需要在每次迭代中重复的表单,但在第一次迭代时,while 循环缺少此表单(html)元素,其余的迭代都可以正常工作。我不知道问题是什么,为什么 while 循环在第一次迭代中缺少表单元素。