这个字符串后台如何拼接出来。
{
strTxt.Append("<dd>"+dt[i][j].ToString()"+</dd>");
}
//或者
strTxt.Append("<dd>"+dt[i]["列名"].ToString()"+</dd>");
strTxt.Append("<dd>"+dt[i]["列名"].ToString()"+</dd>");
strTxt.Append("<dd>"+dt[i]["列名"].ToString()"+</dd>");
strTxt.Append("</dt>");
}
else
{
strTxt.Append("<dd>");
strTxt.Append("<a href="" + dr["PageUrl"] + "">" + dr["Name"] + "</a>");
for (int j = 0; j < dt.Columns.Count; j++)
{
strTxt.Append("<dd>"+dt[i][j].ToString()"+</dd>");
}
strTxt.Append("</dd>");
}
strTxt.Append("</dl>");
}
[解决办法]
public StringBuilder sb = new StringBuilder();
public void GetResult(DataTable dt,int pid=0,bool flag=true)
{
DataRow[] drs = dt.Select(" pid="+pid);
foreach (DataRow dr in drs)
{
if (flag)
{
sb.Append( "<dl>");
sb.Append( dr["Name"].ToString());
GetResult( dt, (int)dr["ID"], false);
sb.Append( "</dl>");
}
else
{
sb.Append( "<dd>");
sb.Append( dr["Name"].ToString());
sb.Append( "</dd>");
}
}
}
GetResult(dt);
string _result = sb.ToString();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if ((int)dr["ParentId"] == 0)
{
if(i==0)
strTxt.Append("<dl>");
else
strTxt.Append("</dl><dl>");
strTxt.Append("<dt>");
strTxt.Append(dr["Name"]);
strTxt.Append("</dt>");
}
else
{
strTxt.Append("<dd>");
strTxt.Append("<a href="" + dr["PageUrl"] + "">" + dr["Name"] + "</a>");
strTxt.Append("</dd>");
}
}
strTxt.Append("</dl>");
StringBuilder strTxt = new StringBuilder();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if ((int)dr["ParentId"] == 0)
{
if (i == 0)
strTxt.Append("<dl>");
else
strTxt.Append("</dl><dl>");
strTxt.Append("<dt>");
strTxt.Append(dr["Name"]);
strTxt.Append("</dt>");
}
else
{
strTxt.Append("<dd>");
strTxt.Append("<a href="" + dr["PageUrl"] + "">" + dr["Name"] + "</a>");
strTxt.Append("</dd>");
if (i == dt.Rows.Count - 1)
strTxt.Append("</dl>");
}
}