带右键菜单的树形控件(1)
网络整理 - 08-09
用户自定义控件代码如下:using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
namespace EWFS.CL
{
public struct stuTreeNodes
{
public string strKey;
public string strDispVal;
public string strParentKey;
public string strJavascriptCmd;
}
public struct stuTreeNodesIcons
{
public string strTreeCaptionIcon;
public string strTreeContentOpenIcon;
public string strTreeContentCloseIcon;
}
public struct stuTreePopMenu
{
public string strMenuItemImg;
public string strMenuItemText;
public string strMenuItemCmd;
}
[DefaultProperty("Text"),
ToolboxData("<{0}:TreeView runat=server></{0}:TreeView>")]
public class TreeView : System.Web.UI.WebControls.WebControl
{
private string m_TreeCaption; //标题
private stuTreeNodes[] m_stuTreeNodes; //节点内容
private stuTreeNodesIcons m_stuTreeNodesIcons; //节点图标
private string m_TreeChangedPostURL; //修改后的树传送的地址
private bool m_TreeAddDir;
private bool m_TreeRenDir;
private bool m_TreeDelDir;
private stuTreePopMenu[] m_stuTreePopMenu;
private string m_GotoID;
private bool m_TreeRootPopMenu;
private System.Data.DataSet m_dsTreeNodes;
protected System.Web.UI.WebControls.Table TreeTable;
public string TreeGotoID //定位ID
{
get{return m_GotoID;}
set{m_GotoID=value;}
}
[Bindable(true),
Category("Appearance"),
DefaultValue(true)]
public bool TreeRootPopMenu
{
get{return m_TreeRootPopMenu;}
set{m_TreeRootPopMenu=value;}
}
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public bool TreeAddDir
{
get{return m_TreeAddDir;}
set{m_TreeAddDir=value;}
}
public bool TreeRenDir
{
get{return m_TreeRenDir;}
set{m_TreeRenDir=value;}
}
public bool TreeDelDir
{
get{return m_TreeDelDir;}
set{m_TreeDelDir=value;}
}
public stuTreePopMenu[] StuTreePopMenu
{
get{return m_stuTreePopMenu;}
set{m_stuTreePopMenu=value;}
}
public string TreeChangedPostURL
{
get{return m_TreeChangedPostURL;}
set{m_TreeChangedPostURL=value;}
}
public stuTreeNodesIcons StuTreeNodesIcons
{
get {return m_stuTreeNodesIcons;}
set {m_stuTreeNodesIcons = value;}
}
public stuTreeNodes[] StuTreeNodes
{
get
{
return m_stuTreeNodes;
}
set
{
m_stuTreeNodes=value;
}
}
public string TreeCaption
{
get
{
return m_TreeCaption;
}
set
{
m_TreeCaption = value;
}
}
/// <summary>
/// 获取节点内容
/// </summary>
public System.Data.DataSet dsTreeNodes
{
get{return this.m_dsTreeNodes;}
set
{
this.m_dsTreeNodes=value;
SetTreeNodes();
}
}
private void SetTreeNodes()
{
this.m_stuTreeNodes =new stuTreeNodes[m_dsTreeNodes.Tables[0].Rows.Count];
for(int i=0;i<m_dsTreeNodes.Tables[0].Rows.Count;i++)
{
this.m_stuTreeNodes[i].strKey=m_dsTreeNodes.Tables[0].Rows[i][0].ToString();
this.m_stuTreeNodes[i].strParentKey=m_dsTreeNodes.Tables[0].Rows[i][1].ToString()=="0"?"":m_dsTreeNodes.Tables[0].Rows[i][1].ToString();
this.m_stuTreeNodes[i].strDispVal=m_dsTreeNodes.Tables[0].Rows[i][2].ToString();
this.m_stuTreeNodes[i].strJavascriptCmd=m_dsTreeNodes.Tables[0].Rows[i][3].ToString();
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void CreateChildControls()
{
System.Web.UI.WebControls.Table oTB=new Table();
System.Web.UI.WebControls.Table oTB1=new Table();
System.Web.UI.WebControls.TableRow oTR=new TableRow ();
System.Web.UI.WebControls.TableCell oTD=new TableCell ();
System.Web.UI.WebControls.Unit oU=new Unit("98%");
System.Web.UI.ControlCollection oCC=null;
System.Web.UI.Control oC1=null;
int i;
//输出JS文件
string scr="";//"<script language=javascript src=BuildTree.js></script>\r\n";
//scr+="<script language=javascript src=PopMenu.js></script>\r\n";
scr+="<script>\r\n";
//设置右键菜单
scr+="var MenuItemArray=new Array( ";
if(m_TreeAddDir)
{
scr+="[''<img src=http://www.jz123.cn/Article/10/130/2005/images/mycomputer.gif>'',''增加目录'',''TreeAddDir()''],";
}
if(m_TreeDelDir)
{
scr+="[''<img src=http://www.jz123.cn/Article/10/130/2005/images/mycomputer.gif>'',''删除目录'',''TreeDelDir()''],";
}
if(m_TreeRenDir)
{
scr+="['''',''重命名'',''TreeRenDir()''],";
}
//通过外部增加菜单内容
if(!(m_stuTreePopMenu==null))
{
for(i=0;i<m_stuTreePopMenu.Length ;i++)
{
scr+="[''"+m_stuTreePopMenu[i].strMenuItemImg +"'',"
+"''"+m_stuTreePopMenu[i].strMenuItemText +"'',"
+"''"+m_stuTreePopMenu[i].strMenuItemCmd.Replace("''","\\''") +"''],";
}
}
//scr+="['''','''','''']);\n";
scr=scr.Substring(0,scr.Length-1);
scr+=");\n";
scr+="var oIMGS=new Array(3);\r\n";
scr+="for(i=0;i<3;i++)\r\n";
scr+="{\r\n";
scr+=" oIMGS[i]=new Image;\r\n";
scr+="}\r\n";
scr+="oIMGS[0].src=''"+m_stuTreeNodesIcons.strTreeCaptionIcon+"''\r\n"; //头部图形
scr+="oIMGS[1].src=''"+m_stuTreeNodesIcons.strTreeContentCloseIcon +"''\r\n"; //未选中
scr+="oIMGS[2].src=''"+m_stuTreeNodesIcons.strTreeContentOpenIcon +"''\r\n"; //选中
scr+="document.body.;\n"
+" createspan=o1;\n"
+"}\n";
}
}
}