触摸屏租赁
Touch screen rental
广告机租赁
Advertising rental
机器人租赁
Robot leasing
 当前位置:首页 > 全部 > 如何用foreach二重循环取字符串值

前台:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%=FamilyStr%>
    </div>
    </form>
</body>
</html>

后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected string FamilyStr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BuiltAirTable();
        }
    }
    private void BuiltAirTable()
    {
        //如何用foreach二重循环取字符串值,分别取,但又存到同一记录中
        string str = "class1|Name1|money1|num1,class2|Name2|money2|num2,class3|Name3|money3|num3";

        StringBuilder sb = new StringBuilder();
        sb.Append("<table width='95%' border='0' cellpadding='0' cellspacing='0' align='center'>");
        sb.Append("<tr>");
        sb.Append("<td align='center' class='but' width='14%' colspan='7' height='26px'>");
        sb.Append("新增DIY配置");
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.Append("<td align='center'  width='14%' height='26px' class='but'>");
        sb.Append("配 件");
        sb.Append("</td>");
        sb.Append("<td align='center'  width='14%' class='but'>");
        sb.Append("品 名");
        sb.Append("</td>");
        sb.Append("<td align='center'  width='14%' class='but'>");
        sb.Append("价 格(元)");
        sb.Append("</td>");
        sb.Append("<td align='center' class='but'>");
        sb.Append("数 量");
        sb.Append("</td>");

        sb.Append("</tr>");
        string[] arr = str.Split(',');
        foreach (string str1 in arr)
        {
            sb.Append("<tr>");

            string[] arr2 = str1.Split('|');
            foreach (string str2 in arr2)
            {
                sb.Append("<td style='text-align:center;'  width='14%' height='26px'  class='but'>");
                sb.Append(str2);
                sb.Append("</td>");
            }

            sb.Append("</tr>");
        }
        sb.Append("</table>");

        FamilyStr = sb.ToString();
    }
}


工作室地址:重庆石桥铺电脑城B座 | 微信:z35544216 | 网址:www.35544216.com