受经济危机的影响,最近没有做什么大项目,弄了一个二叉树的HTML显示方法,代码如下:


protected string bf(int i, string coding, string pcoding, string s)
{
string c1 = "", c2 = "";
if (coding == "左未注册" || coding == "右未注册")
{ c1 = "<div style='background-color:#ccc;height:40px;line-height:40px;'> 左未注册</div>"; c2 = "<div style='background-color:#ccc;height:40px;line-height:40px;'>右未注册</div>"; }
else
{
// c1 = "<div style='background-color:#0000ff;height:40px;line-height:40px;'> 左未注册</div>"; c2 = "<div style='background-color:#0000ff;height:40px;line-height:30px;'>右未注册</div>";
c1 = string.Format("<div style='background-color:#0000ff;height:40px;line-height:40px;'><a href=Register.aspx?anzhi={0}&site=0 class='qian'>{1}</a> </div>", coding, "左未注册");
c2 = string.Format("<div style='background-color:#0000ff;height:40px;line-height:40px;'><a href=Register.aspx?anzhi={0}&site=1 class='qian'>{1}</a> </div>", coding, "右未注册");
}
Sale.BLL.Customer bcustomer = new Sale.BLL.Customer();
Sale.Model.Customer mcustomer = bcustomer.GetModel(coding);
if (i < 2)
{
i++;
if (mcustomer != null)
{
if (!string.IsNullOrEmpty(mcustomer.LeftChildCoding))
{
c1 = bf(i, mcustomer.LeftChildCoding, coding, s);
}
else
{
c1 = bf(i, "左未注册", coding, s);
}
if (!string.IsNullOrEmpty(mcustomer.RightChildCoding))
{
c2 = bf(i, mcustomer.RightChildCoding, coding, s);
}
else
{
c2 = bf(i, "右未注册", coding, s);
}
}
else
{
c1 = bf(i, "左未注册", coding, s);
c2 = bf(i, "右未注册", coding, s);
}
}
else
{
if (mcustomer != null)
{
if (!string.IsNullOrEmpty(mcustomer.LeftChildCoding))
{
Sale.Model.Customer lcus=bcustomer.GetModel(mcustomer.LeftChildCoding);
c1 = string.Format("<a href='NetTree.aspx?root={0}' class='{2}' >{0}<font style='color:Red; font-size:14px;'>({1}单)</font></a>", mcustomer.LeftChildCoding,lcus.RatingCoding, lcus.IsValid?"alan":"ahuang");
}
if (!string.IsNullOrEmpty(mcustomer.RightChildCoding))
{
Sale.Model.Customer rcus = bcustomer.GetModel(mcustomer.RightChildCoding);
c2 = string.Format("<a href='NetTree.aspx?root={0}' class='{2}'>{0}<font style='color:Red; font-size:14px;'>({1}单)</font></a>", mcustomer.RightChildCoding,rcus.RatingCoding,rcus.IsValid ? "alan" : "ahuang");
}
}
}
if (coding == "左未注册" || coding == "右未注册")
{
if (pcoding == "左未注册" || pcoding == "右未注册")
{
return s.Replace("左孩子", c1).Replace("右孩子", c2).Replace("根节点", string.Format("<div style='background-color:#ccc;height:40px;line-height:40px;' class='qian'>{0}</a> </div>", coding));
}
else
return s.Replace("左孩子", c1).Replace("右孩子", c2).Replace("根节点", string.Format("<div style='background-color:#0000ff;height:40px;line-height:40px;'><a href=Register.aspx?anzhi={0}&site={1} class='qian'>{2}</a> </div>", pcoding, coding == "左未注册" ? "0" : "1", coding));
}
else
return s.Replace("左孩子", c1).Replace("右孩子", c2).Replace("根节点", string.Format("<a href='NetTree.aspx?root={0}' class='{2}'>{0}<font style='color:Red; font-size:14px;'>({1}单)</font></a>", coding, mcustomer.RatingCoding,mcustomer.IsValid?"alan":"ahuang"));
}
调用时:


string s = "<table cellpadding='0' cellspacing='0' border='1' style='100%; height:100%; text-align:center;background-color:#0563AB; color:#ffffff; font-weight:bold;border-top:solid 1px #cccc; border-left:solid 1px #cccc; border-bottom-0px; border-right-0px;'><tr><td colspan='2' style='height:40px;border-right:solid 1px #cccc; border-bottom:solid 1px #cccc; border-top-0px; border-left-0px;' >根节点</td></tr><tr><td style='50%;height:40px;border-right:solid 1px #cccc; border-bottom:solid 1px #cccc; border-top-0px; border-left-0px;' >左孩子</td><td style='50%; height:40px;border-right:solid 1px #cccc; border-bottom:solid 1px #cccc; border-top-0px; border-left-0px;' >右孩子</td></tr></table>";
string str = this.bf(0,root_coding,"",s);


<style type="text/css">
.ahuang{
width:100%;
display:block;
background-color:#FFFF00;
height:40px;
line-height:40px;
}
.alan{
width:100%;
display:block;
background-color:#00ff00;
height:40px;
line-height:40px;
}
.qian{
width:100%;
display:block;
background-color:#0563AB;
height:40px;
line-height:40px;
}
</style>