循环输入一行多列的方法
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!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 id="test" runat="server">
</div>
</form>
</body>
</html>
<!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 id="test" runat="server">
</div>
</form>
</body>
</html>
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("id",typeof(int)));
DataRow dr=dt.NewRow();
dr["id"] = 1;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 2;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 3;
dr = dt.NewRow();
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 4;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 5;
dr = dt.NewRow();
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 6;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 7;
dt.Rows.Add(dr);
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i % 2 == 0 && i!=0)
{
test.InnerHtml += "<br>";
test.InnerHtml+=" <a href='#'>test</a>";
}
else
{
test.InnerHtml += " <a href='#'>test</a>";
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("id",typeof(int)));
DataRow dr=dt.NewRow();
dr["id"] = 1;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 2;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 3;
dr = dt.NewRow();
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 4;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 5;
dr = dt.NewRow();
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 6;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 7;
dt.Rows.Add(dr);
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i % 2 == 0 && i!=0)
{
test.InnerHtml += "<br>";
test.InnerHtml+=" <a href='#'>test</a>";
}
else
{
test.InnerHtml += " <a href='#'>test</a>";
}
}
}
}
HTML代码:
代码
<p><a href="#" target="_blank">山东辽</a>宁 <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
<p><a href="#" target="_blank">山东辽宁</a> <a href="#" target="_blank">陕西河南</a> <a href="#" target="_blank">东北三省</a> <a href="#" target="_blank">湖南湖北</a></p>
输出以上格式一行4列: 下面为两种输出方式
代码
private void RouteNavigate_RecommendBinds()
{
TourRouteCategoryService trs = new TourRouteCategoryService();
TourMenuService tms = new TourMenuService();
DataTable dt = trs.GetList("RouteCategoryParent in (select TourRouteCategoryId from Tab_TourRouteCategory where RouteCategoryParent='0') order by Sorting").Tables[0];
StringBuilder strHtml = new StringBuilder();
int i = 0;
strHtml.Append("<p>");
foreach (DataRow dr in dt.Rows)
{
if (i > 3)
{
strHtml.Append("</p><p>");
i = 0;
}
strHtml.Append("<a href=\"#\" target=\"_blank\">" + dr["RouteCategoryName"] + "</a> ");
i++;
}
strHtml.Append("</p>");
dt = tms.GetList(" TourMenuParent in (select TourMenuNumber from Tab_TourMenu where TourMenuParent='0' ) order by sorting").Tables[0];
strHtml.Append("<p>");
for (int j = 0; j < dt.Rows.Count; j++)
{
if (j % 4 == 0 && j != 0)
{
strHtml.Append("</p><p>");
}
strHtml.Append("<a href=\"#\" target=\"_blank\">" + dt.Rows[j]["TourMenuName"] + "</a> ");
}
strHtml.Append("</p>");
divNavigation.InnerHtml = strHtml.ToString();
}
{
TourRouteCategoryService trs = new TourRouteCategoryService();
TourMenuService tms = new TourMenuService();
DataTable dt = trs.GetList("RouteCategoryParent in (select TourRouteCategoryId from Tab_TourRouteCategory where RouteCategoryParent='0') order by Sorting").Tables[0];
StringBuilder strHtml = new StringBuilder();
int i = 0;
strHtml.Append("<p>");
foreach (DataRow dr in dt.Rows)
{
if (i > 3)
{
strHtml.Append("</p><p>");
i = 0;
}
strHtml.Append("<a href=\"#\" target=\"_blank\">" + dr["RouteCategoryName"] + "</a> ");
i++;
}
strHtml.Append("</p>");
dt = tms.GetList(" TourMenuParent in (select TourMenuNumber from Tab_TourMenu where TourMenuParent='0' ) order by sorting").Tables[0];
strHtml.Append("<p>");
for (int j = 0; j < dt.Rows.Count; j++)
{
if (j % 4 == 0 && j != 0)
{
strHtml.Append("</p><p>");
}
strHtml.Append("<a href=\"#\" target=\"_blank\">" + dt.Rows[j]["TourMenuName"] + "</a> ");
}
strHtml.Append("</p>");
divNavigation.InnerHtml = strHtml.ToString();
}