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();
         }
     }

すべての反復で繰り返す必要があるフォームが含まれていますが、最初の反復では、ループにこのフォーム (html) 要素がありませんが、残りの反復では問題なく動作します。問題が何であるかわかりませんが、ループの最初の反復でフォーム要素が欠落している理由は何ですか。

4

2 に答える 2

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 に答える