TreeView控件添加、修改、删除节点

在VS.NET中用三层架构编写TreeView读取数据库,添加、修改、删除后能保存在数据库中代码要怎么写啊... 在VS.NET中用三层架构编写
TreeView读取数据库,添加、修改、删除后能保存在数据库中

代码要怎么写啊
展开
 我来答
百度网友0a3558898
2010-05-27 · TA获得超过1863个赞
知道小有建树答主
回答量:1080
采纳率:0%
帮助的人:0
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class App_page_NaviLeft : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string SchoolID = "";
if (Session["SchoolID"] != null && Session["SchoolID"].ToString() != "")
{
SchoolID = Session["SchoolID"].ToString();
this.TextBox_SchoolID.Text = SchoolID;
}
else
{
ShowMessage("Session已超时,请重新登录!");
return;
}
//动态绑定DateList
DatelistAdd();
//只添加第一层的节点
PopulateCategories();
}
}

#region 动态绑定DateList
private void DatelistAdd()
{
string strConn = ConfigurationManager.AppSettings["conn"].ToString(); // 获取连接数据库的字符串

string strSQL = "Select * From Schoolname_IP where IsDelete='0' order by id desc";
System.Data.SqlClient.SqlConnection sqlConn = new SqlConnection(strConn);
System.Data.SqlClient.SqlCommand sqlComm = new SqlCommand(strSQL, sqlConn);
try
{
sqlConn.Open();

System.Data.SqlClient.SqlDataReader sqlDR = sqlComm.ExecuteReader();

if (sqlDR != null)
{
while (sqlDR.Read())
{
ListItem LI = new ListItem();
LI.Text = sqlDR["schoolname"].ToString();
LI.Value = sqlDR["id"].ToString();
this.DropDownList_School.Items.Add(LI);
}
}

sqlDR.Close();
this.DropDownList_School.SelectedValue = this.TextBox_SchoolID.Text;
}
catch
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.alert('获得数据的过程出现了错误!请检查数据库的连接是否正确!');</script>");
}
finally
{
sqlConn.Close();
}
}
#endregion

#region 填充第一层树节点
private void PopulateCategories()
{
TVMenu.Nodes.Clear();
SqlCommand sqlQuery = new SqlCommand("Select * From LM_Navi where NaviGrade=1 and schoolid = '" + this.TextBox_SchoolID.Text + "' order by NaviID");
DataSet resultSet = RunQuery(sqlQuery);
if (resultSet == null)
{
return;
}

if (resultSet.Tables.Count > 0)
{
foreach (DataRow row in resultSet.Tables[0].Rows)
{
TreeNode NewNode = new TreeNode(row["NaviName"].ToString(), row["NaviCode"].ToString() + "," + row["NaviGrade"].ToString() + "," + row["NaviChild"].ToString());
NewNode.PopulateOnDemand = true;

this.TVMenu.Nodes.Add(NewNode);

if (TextBox1.Text.Trim() == "1")
{
NewNode.ExpandAll();
}
else
{
NewNode.CollapseAll();
}
}
}

resultSet.Dispose();
}
#endregion

#region 执行SQL语句并返回数据集
/// <summary>
/// 函数原型:private DataSet RunQuery(SqlCommand sqlQuery)
/// 函数功能:执行SQL语句并返回数据集
/// 函数参数:SqlCommand sqlQuery 带SQL语句的SqlCommand
/// 函数返回:执行SQL语句后的数据集
/// </summary>
/// <param name="sqlQuery">带SQL语句的SqlCommand</param>
private DataSet RunQuery(SqlCommand sqlQuery)
{
string connectionString = ConfigurationManager.AppSettings["conn"];
SqlConnection DBConnection = new SqlConnection(connectionString);
SqlDataAdapter dbAdapter = new SqlDataAdapter();
dbAdapter.SelectCommand = sqlQuery;
sqlQuery.Connection = DBConnection;
DataSet resultsDataSet = new DataSet();
try
{
dbAdapter.Fill(resultsDataSet);
}
catch
{
resultsDataSet = null;
}
return resultsDataSet;
}
#endregion

#region 展开节点时触发的事件
protected void TVMenu_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
//防止重复填充节点
if (e.Node.ChildNodes.Count > 0)
return;

string[] str = e.Node.Value.Split(',');
string strNaviCode = str[0].ToString();
string strGrade = "";
bool BoolFlag = e.Node.Checked;

strGrade = Convert.ToString((Convert.ToInt32(str[1]) + 1));
SqlCommand sqlQuery = new SqlCommand("Select * From LM_Navi where schoolid = '" + this.TextBox_SchoolID.Text + "' and NaviCode like '" + strNaviCode + "%' and NaviGrade=" + strGrade + " order by NaviID");
DataSet ResultSet = RunQuery(sqlQuery);
if (ResultSet == null)
{
return;
}

if (ResultSet.Tables.Count > 0)
{
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
TreeNode NewNode = new TreeNode(row["NaviName"].ToString(), row["NaviCode"].ToString() + "," + row["NaviGrade"].ToString() + "," + row["NaviChild"].ToString());
NewNode.PopulateOnDemand = true;
NewNode.Checked = BoolFlag;

e.Node.ChildNodes.Add(NewNode);

//if ((int)row["ChildFlag"] == 1)
//{
// NewNode.Collapse();
//}
//else
//{
// NewNode.Expand();
//}
NewNode.ExpandAll();
}

}
ResultSet.Dispose();
}
#endregion

#region 显示信息函数
public void ShowMessage(string Message)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.alert('" + Message + "');</script>");
}
#endregion

protected void ImageButton_open_all_Click(object sender, ImageClickEventArgs e)
{
TextBox1.Text = "1";
PopulateCategories();
}
protected void ImageButton_close_all_Click(object sender, ImageClickEventArgs e)
{
TextBox1.Text = "0";
PopulateCategories();
}

protected void ImageButton_select_Click(object sender, ImageClickEventArgs e)
{
try
{
//if (TVMenu.CheckedNodes != null && TVMenu.CheckedNodes.Count > 0)
//{
string strWhere = "";
for (int i = 0; i < TVMenu.CheckedNodes.Count; i++)
{
if (TVMenu.CheckedNodes[i].Parent == null || TVMenu.CheckedNodes[i].Parent.Checked == false)
{
string[] str = TVMenu.CheckedNodes[i].Value.Split(',');
string strClassCode = str[0].ToString();
strWhere += strClassCode + "|";
}
}
if (strWhere.Length > 1)
{
strWhere = strWhere.Substring(0, strWhere.Length - 1);
}
string strRSQL = strWhere;
string strContent = this.TextBox_Content.Text.Trim();
string SchoolID = DropDownList_School.SelectedValue;
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.open('FAQslist.aspx?SQL=" + strRSQL + "&Content=" + strContent + "&Page_SchoolID=" + SchoolID + "','FAQRight');</script>");
//}
//else
//{
// ShowMessage("请选择节点");
// return;
//}
}
catch
{
ShowMessage("配置有误,请检查配置。");
return;
}
}

protected void DropDownList_School_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox_SchoolID.Text = this.DropDownList_School.SelectedValue;
string schoolid = this.TextBox_SchoolID.Text;
Response.Write("<script>window.open('FAQslist.aspx?Page_SchoolID=" + schoolid + "','FAQRight');</script>");
PopulateCategories();
}
protected void TVMenu_SelectedNodeChanged(object sender, EventArgs e)
{
try
{
string[] str = TVMenu.SelectedNode.Value.Split(',');
string strClassCode = str[0].ToString();
string SchoolID = DropDownList_School.SelectedValue;

Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.open('FAQslist.aspx?A_SQL=" + strClassCode + "&Page_SchoolID=" + SchoolID + "','FAQRight');</script>");

}
catch
{
ShowMessage("配置有误,请检查配置。");
return;
}
}
}
depluin
2010-05-27 · TA获得超过3179个赞
知道大有可为答主
回答量:3378
采纳率:75%
帮助的人:2479万
展开全部
既然你都说三层了,数据访问实现数据的保存,业务层负责对指定的TreeView做节点操作,并将结果回写数据库,UI负责现实就行了。。。。。。。

求代码这点分估计求不到。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xiangboren
2010-05-27 · 超过23用户采纳过TA的回答
知道答主
回答量:138
采纳率:0%
帮助的人:50.8万
展开全部
ASP.NET中TreeView的运用
http://www.fcbcw.com/content.aspx?id=383559953366

首先需要创建一个数据库表test,字段包括NODEID、PARENTID、NODENAME、ADDERSS、ICON、linkUrLNODEID是在数据库中作为节点的唯一标识,这里为了方便,将其设置为标识
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式