转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
一棵好树,梅花树,后台生成前台htm树(asp.net c)         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1947 更新时间:2009/4/23 10:40:34
;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.P

上一页  [1] [2] [3]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台