ASP.NET C#,如何在后面的代码中键入HTML



如何在后面的代码中键入HTML?我在html中有此代码:

<table style="width:100%">
  <tr>
    <th>Product</th>
    <th>Price</th> 
    <th>Edit</th>
  </tr> 
 <!-- I want a loop here to add the Product name , Price from the database ( To add <tr> and <th> for each product in the array -->
</table>

和在:

后面的代码中
       ` List<string> products = new List<string>();
        string name = (string)Session["id"];
        SqlCommand com = new SqlCommand("select id from accounts where 
        username = '" + name + "'", con1);
        SqlDataReader y = com.ExecuteReader();
        y.Read();
        string id = y["id"].ToString();
        SqlCommand com2 = new SqlCommand("select * from CartItems where 
        Buyer = '" + id + "' ", con1);
        SqlDataReader read = com2.ExecuteReader();
        while (read.Read())
        {
            products.Add(y["id"].ToString());
        }
        products.ToArray(); ` 

那么,我该怎么做?,(在数组中进行循环,然后为数组中的每个项目添加一个(TR(和(Th((我在PHP中做到了,是否可以在ASP.NET?

中做到这一点

您可以使用GridView显示选定的列。而且不要忘记设置

GridView1.AutoGenerateColumns=false

另外,而不是写作

select * from CartItems

指定您要获取的列名,

select col1,col2 from CartItems

此外,仅将这些列绑定到要显示的GridView中。

谢谢。

使用 razor view engine循环列表并渲染正确的html。剃须刀循环因此,将此代码放置在视图中-CSHTML文件。

观看一些教程,并了解什么是MVC以及如何将其实现到ASP.NET中。理想的选择是将DB访问从实际视图和业务逻辑分开,不要将它们混在一起。

最新更新