首页 > 编程 > .NET > 正文

一棵好树,梅花树,后台生成前台htm树(asp.net c)

2024-07-10 12:55:51
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,

首先模板treetemplate.htm文件如下:

<html>
<head>
<title>tree</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript" src="mztreeview10.js"></script>
<link href="menutree/menu.css" type="text/css" rel="stylesheet">
<style> a.mztreeview { padding-left: 3px; font-size: 12px } </style>
</head>
<body leftmargin="0" topmargin="2">
<script language="javascript">
window.tree = new mztreeview("tree");
tree.seticonpath("menutree/"); //可用相对路径
abcdefg
document.write(tree.tostring()); //亦可用 obj.innerhtml = tree.tostring();
//-->
</script>
</body>
</html>

好了,下面是页面managertre.aspx的代码:

<%@ page language="c#" codebehind="managertree.aspx.cs" autoeventwireup="false" inherits="hualong.manager" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>管理中心</title>
<meta content="microsoft visual studio .net 7.1" name="generator">
<meta content="c#" name="code_language">
<meta content="javascript" name="vs_defaultclientscript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
<link href="style.css" type="text/css" rel="stylesheet">
<script language="javascript">
<!--
function showjie()
{
tianjia.style.display = "";
}
function window.onload()
{
if(arr=document.cookie.match(/scrolltop=([^;]+)(;|$)/))
document.body.scrolltop=parseint(arr[1]);
}
function window.onbeforeunload(){
document.cookie="scrolltop="+document.body.scrolltop;
}
//-->
</script>
</head>
<body>
<form id="form1" method="post" runat="server">
<br>
<table cellspacing="0" cellpadding="0" width="350" border="0">
<tr>
<td align="center" width="50%"><input type="button" value="添加节点"></td>
<td align="center"><asp:button id="btntree" runat="server" text="生成树"></asp:button></td>
</tr>
</table>
<table id="tianjia" bordercolor="#6595d6"
cellspacing="1" cellpadding="0" width="1000" align="center" border="1">
<tr>
<td align="center" width="60"><asp:button id="btnaddok" runat="server" text="添加"></asp:button></td>
<td align="center" width="50"><asp:textbox id="txtid" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center" width="50"><asp:textbox id="txtparentid" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center" width="120"><asp:textbox id="txttext" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center" width="120"><asp:textbox id="txthint" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center" width="60"><asp:textbox id="txticon" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center"><asp:textbox id="txtdata" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center" width="150"><asp:textbox id="txturl" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center" width="50"><asp:textbox id="txttarget" runat="server" wrap="false" width="100%"></asp:textbox></td>
<td align="center" width="120"><asp:textbox id="txtmethod" runat="server" wrap="false" width="100%"></asp:textbox></td>
</tr>
</table>
<table id="table1" cellspacing="0" cellpadding="0" width="1000" align="center" border="0">
<tr>
<td align="center" width="100%"><asp:datagrid id="dgtree" runat="server" width="100%" datakeyfield="biaoshi_id" borderwidth="1px"
bordercolor="#6595d6" autogeneratecolumns="false">
<edititemstyle wrap="false"></edititemstyle>
<itemstyle wrap="false"></itemstyle>
<headerstyle wrap="false" horizontalalign="center" height="30px" forecolor="white" verticalalign="middle"
backcolor="#6595d6"></headerstyle>
<columns>
<asp:editcommandcolumn buttontype="linkbutton" updatetext="更新" headertext="管理" canceltext="取消" edittext="编辑">
<headerstyle wrap="false" horizontalalign="center" width="60px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
<footerstyle wrap="false"></footerstyle>
</asp:editcommandcolumn>
<asp:buttoncolumn text="删除" headertext="删除" commandname="delete">
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:buttoncolumn>
<asp:boundcolumn datafield="id" headertext="id">
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="parentid" headertext="父id">
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="text" headertext="节点文本">
<headerstyle wrap="false" horizontalalign="center" width="120px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="hint" headertext="节点提示">
<headerstyle wrap="false" horizontalalign="center" width="120px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="icon" headertext="节点图标">
<headerstyle wrap="false" horizontalalign="center" width="60px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="data" headertext="url参数">
<headerstyle wrap="false" horizontalalign="center"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="url" headertext="url">
<headerstyle wrap="false" horizontalalign="center" width="150px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="target" headertext="target">
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="method" headertext="method">
<headerstyle wrap="false" horizontalalign="center" width="120px"></headerstyle>
<itemstyle wrap="false" horizontalalign="center"></itemstyle>
</asp:boundcolumn>
</columns>
</asp:datagrid></td>
</tr>
</table>
</form>
</body>
</html>

下面是后台managertree.aspx.cs后台文件:

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.io;
using system.data.sqlclient;
using system.configuration;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace hualong
{
/// <summary>
/// manager 的摘要说明。
/// </summary>
public class manager : system.web.ui.page
{
protected system.web.ui.webcontrols.button btntree;
protected system.web.ui.webcontrols.textbox txtid;
protected system.web.ui.webcontrols.textbox txtparentid;
protected system.web.ui.webcontrols.button btnaddok;
protected system.web.ui.webcontrols.textbox txthint;
protected system.web.ui.webcontrols.textbox txticon;
protected system.web.ui.webcontrols.textbox txtdata;
protected system.web.ui.webcontrols.textbox txturl;
protected system.web.ui.webcontrols.textbox txttarget;
protected system.web.ui.webcontrols.textbox txtmethod;
protected system.web.ui.webcontrols.textbox txttext;
protected system.web.ui.webcontrols.datagrid dgtree;


private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(!ispostback)
{
binddata();
}
}


#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.btntree.click += new system.eventhandler(this.btntree_click);
this.btnaddok.click += new system.eventhandler(this.btnaddok_click);
this.dgtree.cancelcommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_cancelcommand);
this.dgtree.editcommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_editcommand);
this.dgtree.updatecommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_updatecommand);
this.dgtree.deletecommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_deletecommand);
this.load += new system.eventhandler(this.page_load);

}
#endregion

private void binddata()
{
string connstring = configurationsettings.appsettings["connstr1"].tostring();
sqlconnection conn = new sqlconnection(connstring);
sqlcommand comm = new sqlcommand("select_treeview_pwqzc",conn);
comm.commandtype = commandtype.storedprocedure;
conn.open();
dgtree.datasource = comm.executereader();
dgtree.databind();
conn.close();
}

private void dgtree_editcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
dgtree.edititemindex = e.item.itemindex;
binddata();
}

private void dgtree_cancelcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
dgtree.edititemindex = -1;
binddata();
}

private void btntree_click(object sender, system.eventargs e)
{
string str;//用他来包含所有树的节点信息
string connectstring = system.configuration.configurationsettings.appsettings["connstr1"].tostring();
system.data.sqlclient.sqlconnection conn = new system.data.sqlclient.sqlconnection(connectstring);
system.data.sqlclient.sqldataadapter adapter = new system.data.sqlclient.sqldataadapter("select_treeview_pwqzc",conn);
try
{
system.text.stringbuilder node = new system.text.stringbuilder();
conn.open();
system.data.dataset dataset = new system.data.dataset();
dataset.tables.add("treeview");
adapter.fill(dataset,"treeview");
foreach(system.data.datarow row in dataset.tables[0].rows)
{
node.append("/r/n tree.nodes[/""+ row["parentid"].tostring() + "_" + row["id"] + "/"] = /"");
node.append("text:" + row["text"].tostring().replace("/0xe",";") + ";");
node.append(row["hint"] != dbnull.value && row["hint"].tostring() != string.empty ? "hint:" + row["hint"].tostring().replace("/0xe",";") + ";" : string.empty);
node.append(row["icon"] != dbnull.value && row["icon"].tostring() != string.empty ? "icon:" + row["icon"].tostring().replace("/0xe",";") + ";" : string.empty);
node.append(row["data"] != dbnull.value && row["data"].tostring() != string.empty ? "data:" + row["data"].tostring().replace("/0xe",";") +server.urlencode(row["text"].tostring())+ ";" : string.empty);
node.append(row["url"] != dbnull.value && row["url"].tostring() != string.empty ? "url:" + row["url"].tostring().replace("/0xe",";") + ";" : string.empty);
node.append(row["target"] != dbnull.value && row["target"].tostring() != string.empty ? "target:" + row["target"].tostring().replace("/0xe",";") + ";" : string.empty);
node.append(row["method"] != dbnull.value && row["method"].tostring() != string.empty ? "method:" + row["method"].tostring().replace("/0xe",";") + ";" : string.empty);
node.append("/"");

}

str = node.tostring();
dataset.dispose();
}
finally
{
conn.close();

}
streamreader sr = new streamreader(server.mappath(".")+"//bbs//treetemplate.htm",system.text.encoding.getencoding("gb2312"));//读取模板
string str1 = sr.readtoend();
str1 = str1.replace("abcdefg",str);//替换摸板里面的字符串
sr.close();//关闭sr
streamwriter sw = new streamwriter(server.mappath(".")+"//bbs//left.htm",false,system.text.encoding.getencoding("gb2312"));//写入文件left.htm
sw.write(str1);
sw.flush();
sw.close();
response.write("<script language=/"javascript/">alert('生成树成功!');</script>");
}

private void btnaddok_click(object sender, system.eventargs e)
{
if(txtid.text.trim()=="" || txtparentid.text.trim()=="")
{
page.registerstartupscript("","<script>alert('节点值与父节点值不能为空!而且只能够是整数!');document.getelementbyid('tianjia').style.display='';</script>");
return;
}
else
{
string connstring = configurationsettings.appsettings["connstr1"].tostring();
sqlconnection conn = new sqlconnection(connstring);
string selstring = "select count(id) from treeview where id = @id";
sqlcommand comm = new sqlcommand(selstring,conn);
comm.parameters.add(new sqlparameter("@id",sqldbtype.int));
comm.parameters["@id"].value = convert.toint32(txtid.text.trim());
conn.open();
int i = (int)comm.executescalar();
conn.close();
if(i>0)
{
page.registerstartupscript("","<script>alert('已经存在相同id值的节点,请重新输入!');document.getelementbyid('tianjia').style.display='';</script>");
return;
}
else
{
string insertstring = "insert into treeview(id,parentid,text,hint,icon,data,url,target,method) values(@id,@parentid,@text,@hint,@icon,@data,@url,@target,@method)";
//插入数据库命令
sqlcommand comm1 = new sqlcommand(insertstring,conn);
comm1.parameters.add(new sqlparameter("@id",sqldbtype.int));
comm1.parameters.add(new sqlparameter("@parentid",sqldbtype.int));
comm1.parameters.add(new sqlparameter("@text",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@hint",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@icon",sqldbtype.varchar,50));
comm1.parameters.add(new sqlparameter("@data",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@url",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@target",sqldbtype.varchar,50));
comm1.parameters.add(new sqlparameter("@method",sqldbtype.varchar,255));
//上面都是添加参数,下面是给参数赋值
comm1.parameters["@id"].value = convert.toint32(txtid.text.trim());
comm1.parameters["@parentid"].value = convert.toint32(txtparentid.text.trim());
comm1.parameters["@text"].value = txttext.text.trim();
comm1.parameters["@hint"].value = txthint.text.trim();
comm1.parameters["@icon"].value = txticon.text.trim();
comm1.parameters["@data"].value = txtdata.text.trim();
comm1.parameters["@url"].value = txturl.text.trim();
comm1.parameters["@target"].value = txttarget.text.trim();
comm1.parameters["@method"].value = txtmethod.text.trim();
conn.open();
comm1.executenonquery();//执行更新命令
conn.close();
response.write("<script>alert('插入数据成功!');</script>");
binddata();//再次绑定数据
}
}
}

private void dgtree_deletecommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
string connstring = configurationsettings.appsettings["connstr1"].tostring();//连接数据库
sqlconnection conn = new sqlconnection(connstring);
string delstring = "delete from treeview where [email protected]_id";//删除语句
sqlcommand comm = new sqlcommand(delstring,conn);
comm.parameters.add(new sqlparameter("@biaoshi_id",sqldbtype.int));//添加参数并且给参数赋值
comm.parameters["@biaoshi_id"].value = dgtree.datakeys[e.item.itemindex];//把单击删除按钮这行的主键值给参数赋值
conn.open();
comm.executereader();//执行删除命令
conn.close();
binddata();//重新绑定数据
}

private void dgtree_updatecommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
//更新数据
string connstring = configurationsettings.appsettings["connstr1"].tostring();//连接数据库
sqlconnection conn = new sqlconnection(connstring);
//先看是不是有id重复的节点
string selstring = "select count(*) from treeview where [email protected]";
sqlcommand comm = new sqlcommand(selstring,conn);
comm.parameters.add(new sqlparameter("@id",sqldbtype.int));//添加参数并且给参数赋值
comm.parameters["@id"].value = convert.toint32((e.item.cells[2].controls[0] as textbox).text.trim());//把单击更新按钮这行的id值给参数赋值
conn.open();
int i = (int)comm.executescalar();
conn.close();
if(i>0)//如果存在相同的节点值
{
page.registerstartupscript("","<script>alert('已经存在相同id值的节点,请重新输入!');document.getelementbyid('tianjia').style.display='';</script>");
return;
}
else
{
string updatestring = "update treeview set [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]_id";
//更新数据库命令
sqlcommand comm1 = new sqlcommand(updatestring,conn);
comm1.parameters.add(new sqlparameter("@biaoshi_id",sqldbtype.int));//添加参数并且给参数赋值
comm1.parameters["@biaoshi_id"].value = dgtree.datakeys[e.item.itemindex];//把单击删除按钮这行的主键值给参数赋值
comm1.parameters.add(new sqlparameter("@id",sqldbtype.int));
comm1.parameters.add(new sqlparameter("@parentid",sqldbtype.int));
comm1.parameters.add(new sqlparameter("@text",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@hint",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@icon",sqldbtype.varchar,50));
comm1.parameters.add(new sqlparameter("@data",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@url",sqldbtype.varchar,255));
comm1.parameters.add(new sqlparameter("@target",sqldbtype.varchar,50));
comm1.parameters.add(new sqlparameter("@method",sqldbtype.varchar,255));
//上面都是添加参数,下面是给参数赋值
comm1.parameters["@id"].value = convert.toint32((e.item.cells[2].controls[0] as textbox).text.trim());
comm1.parameters["@parentid"].value = convert.toint32((e.item.cells[3].controls[0] as textbox).text.trim());
comm1.parameters["@text"].value = (e.item.cells[4].controls[0] as textbox).text.trim();
comm1.parameters["@hint"].value = (e.item.cells[5].controls[0] as textbox).text.trim();
comm1.parameters["@icon"].value = (e.item.cells[6].controls[0] as textbox).text.trim();
comm1.parameters["@data"].value = (e.item.cells[7].controls[0] as textbox).text.trim();
comm1.parameters["@url"].value = (e.item.cells[8].controls[0] as textbox).text.trim();
comm1.parameters["@target"].value = (e.item.cells[9].controls[0] as textbox).text.trim();
comm1.parameters["@method"].value = (e.item.cells[10].controls[0] as textbox).text.trim();
conn.open();
comm1.executenonquery();//执行更新命令
conn.close();
response.write("<script>alert('更新数据成功!');</script>");
dgtree.edititemindex = -1;//让编辑状态取消
binddata();//再次绑定数据
}
}
}
}

我在数据表中添加了一个字段biaoshi_id,他是标识列,自动递增.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表