使用asp c#从数据表填充日期到Html表? [英] fill date from datatable into Html table using asp c# ?

查看:62
本文介绍了使用asp c#从数据表填充日期到Html表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这段代码来填充我的表格,其中包含图片及其名称,

所以如果我有6个项目,我不希望它们显示在两行中(一个用于图像和第二个名称)

i需要在每行中只显示三个值

这意味着:

第一行(image1,image2,image3 )

第二行(name1,name2,name3)

第三行(image4,image5,image6)

第四行(name4,name5, name6)

i used this code to fill my table which is contain image and the name of it ,
so if i have 6 items , i don't want them to display in two rows ( one for image and second for name )
i need to display only three values in each row
which mean :
first row ( image1 , image2 , image3 )
second row (name1 , name2 , name3)
third row (image4 , image5 ,image6 )
fourth row ( name4 , name5 , name6 )

void fillTable()
{
    ProductManagement pm = new ProductManagement();
    DataTable dt = pm.Product_Random();
    StringBuilder html = new StringBuilder();

    //Table start.
    html.Append("<table border = '1'>");

    html.Append("<tr>");
    foreach (DataRow row in dt.Rows)
    {
        int windex = row["Image"].ToString().IndexOf("/");
        html.Append("<td><img src='" + row["Image"].ToString().Substring(windex) + "' height='60' width='60' </></td>");
    }

    html.Append("</tr>");

    html.Append("<tr>");
    foreach (DataRow row in dt.Rows)
    {
        int windex = row["Image"].ToString().IndexOf("/");
        html.Append("<td>" + row["Name"] + "</td>");
    }
    html.Append("</tr>");

    html.Append("</table>");

    //Append the HTML string to Placeholder.
    PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
}

推荐答案

我不完全确定你的问题是什么,但我相信你想要知道如何完成您指定的布局如下:



第一行(image1,image2,image3)

第二行(name1,name2,name3)

第三行(image4,image5,image6)

第四行(name4,name5,name6)



要做到这一点,请使用以下代码。



I am not entirely certain what you question is, but I believe you were wanting to know how to accomplish the layout you specified that looks like:

first row ( image1 , image2 , image3 )
second row (name1 , name2 , name3)
third row (image4 , image5 ,image6 )
fourth row ( name4 , name5 , name6 )

To do that use the following code.

html.Append("<table border="1">");

int itemCount = 0;
StringBuilder sbImageRow = new StringBuilder();
StringBuilder sbNameRow = new StringBuilder();
foreach (DataRow row in dt.Rows)
{
    itemCount++;
    int windex = row["Image"].ToString().IndexOf("/");
    sbImageRow.Append("<td><img src='" + row["Image"].ToString().Substring(windex) + "' height='60' width='60' </></td>");
    sbNameRow.Append("<td>" + row["Name"] + "</td>");

    if(itemCount % 3 == 0)
    {
        //Add Image Row
        html.Append("<tr>");
        html.Append(sbImageRow.ToString());
        html.Append("</tr>");

        //Add Name Row
        html.Append("<tr>");
        html.Append(sbNameRow.ToString());
        html.Append("</tr>");

        //Reset values for next group
        itemCount = 0;
        sbImageRow = new StringBuilder();
        sbNameRow = new StringBuilder();
    }
}

//Check if some of the data hasn't been added to table yet
if(itemCount != 0)
{
    //Pad with empty cells if needed
    for(int i = itemCount; i < 3; i++)
    {
        sbImageRow.Append("<td></td>");
        sbNameRow.Append("<td></td>");
    }

    //Add Image Row
    html.Append("<tr>");
    html.Append(sbImageRow.ToString());
    html.Append("</tr>");

    //Add Name Row
    html.Append("<tr>");
    html.Append(sbNameRow.ToString());
    html.Append("</tr>");
}

html.Append("</table>");





再次,不确定这是否是你想要的,这不是最雄辩的解决方案,但它会完成你想要的。如果您有任何疑问,请与我们联系。



Again, not sure if this what you were looking for and this isn't the most eloquent solution, but it would accomplish what you want. Please let me know if you have any questions.


这篇关于使用asp c#从数据表填充日期到Html表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆