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>";

         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 循环在第一次迭代中缺少表单元素。

4

2 回答 2

1

请与我们分享您的 html 输出
从外观上看,您将生成如下内容:

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

您没有关闭 tr 并且表单应该在 td 中,而不是在 tr 和 td 之间。
结果应如下所示:

<table>
  <tr>
    <td>
      <form></form>
    </td>
  </tr>
</table>
于 2013-10-03T06:48:32.927 回答
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();
     }

同样,您可以</form>在 while 循环之外添加和表格。

于 2013-10-03T06:53:57.833 回答