开发asp.net自定义控件介绍
/// <summary>
/// 方法GenerateGetPosScript用于生成一段Javascript代码,其作用是为菜单定位
/// </summary>
/// <returns></returns>
private string GenerateGetPosScript()
{
string getPosScript;
getPosScript = "<script language=/"JavaScript/">/n";
getPosScript = getPosScript + "function getPos(el,sProp) {/n";
getPosScript = getPosScript + "/tvar iPos = 0/n";
getPosScript = getPosScript + "/tel = el.offsetParent;/n";
getPosScript = getPosScript + "/twhile (el!=null) {/n";
getPosScript = getPosScript + "/t/tiPos+=el[/"offset/" + sProp]/n";
getPosScript = getPosScript + "/t/tel = el.offsetParent/n}/n/treturn iPos;}/n";
getPosScript = getPosScript + "</script>/n";
return(getPosScript);
}
/// <summary>
/// 方法TraverseMenuTree用于解析XML文件并生成HTML代码
/// </summary>
/// <param name="node">欲开始解析的节点</param>
/// <returns></returns>
private string TraverseMenuTree(System.Xml.XmlNode node)
{
string s = "";
string rightCode;
string[] Rule = RuleString.Split(',');
if(node!=null&&node.HasChildNodes)
{
s = s + "<table border=/"0/" cellpadding=/"0/" cellspacing="+cellspacing.ToString()+" width=/"100%/" class=/"a/">";
s = s + "<tr align="+mainMenuAlign+" valign=/"middle/" class=/"mainmenu/">";
for(int current=0; current<node.ChildNodes.Count; current++)
{
string MenuLabelString = node.ChildNodes.Item(current).Attributes["Label"].Value;
s = s + "<td height="+mainMenuHeight.ToString()+" ><a href=/"#;/" onMouseOver=/"expandMenu(null,'menu"+current+"',getPos(this,'Left'),getPos(this,'Top')+this.offsetHeight);/" class=/"a/">/n/t/t"+MenuLabelString+"</a></td>/n";
s = s + "<div id=/"menu"+current+"/" class=/"menu/" onMouseOut=/"hideMe();/">";
if(node.ChildNodes.Item(current).FirstChild!=null&&node.ChildNodes.Item(current).FirstChild.HasChildNodes)
{
for(int i=0;i<node.ChildNodes.Item(current).FirstChild.ChildNodes.Count;i++)
{
string ItemLabelString = node.ChildNodes.Item(current).FirstChild.ChildNodes.Item(i).Attributes["Label"].Value;
string ItemURLString="";
if(node.ChildNodes.Item(current).FirstChild.ChildNodes.Item(i).Attributes["URL"] != null)
{
ItemURLString = node.ChildNodes.Item(current).FirstChild.ChildNodes.Item(i).Attributes["URL"].Value;
}
if(node.ChildNodes.Item(current).FirstChild.ChildNodes.Item(i).Attributes["RightCode"] != null)
{
rightCode = node.ChildNodes.Item(current).FirstChild.ChildNodes.Item(i).Attributes["RightCode"].Value;
}
else
{
rightCode = "";
}
foreach(string rule in Rule)
{
if(rule==rightCode || ruleString=="admin")
{
s = s + "/n/t/t<a href=/"#;/" onMouseOver=/"expandMenu('menu"+current+"');/" onclick=/"window.location='"+ItemURLString+"'/">";
s = s + "/n/t/t"+ItemLabelString+"</a><br>";
}
}
}
s = s + "</div>";
}
}
s = s + "</tr>";
}
s = s + "</tr></table>/n/n";
s = s + "<p class=/"a/"></p>/n";
return s;
}
重写方法:
/// <summary>
/// 重写OnInit方法,初始化条件
/// </summary>
/// <param name="e"></param>
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
return;
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
output.Write(GenerateScript());
output.Write(GenerateHtml());
output.Write(GenerateGetPosScript());
}
XML格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<MenuData ImagesBaseURL="Menuimages/">
<MenuGroup>
<MenuItem Label="文件">
<MenuGroup ExpandOffsetY="1" ExpandOffsetX="-1">
<MenuItem Label="新建" URL="i_New.htm" RightCode="vd_ba"/>
<MenuItem Label="保存" RightCode="vd_ba"/>
</MenuGroup>
</MenuItem>
</MenuGroup>
</MenuData>
如前所述,这个Menu支持权限设置,只要设置它的RuleString属性即可。
本文地址:http://www.45fan.com/a/question/71471.html