TreeView数据控件 添加新的节点

w809026418 2009-05-06 07:18:27
我的treeview控件已经从数据库中读取和绑定好了,但是我要添加新的节点到数据库
我在重新进行Treeview进行数据绑定的时候,treeview进行重新的刷新,但是我的问题是我原来tree打开
的节点,就会关闭了。我不想让他关闭,还显示原来打开的Tree的节点,但是我新插入的节点,也要显示
...全文
119 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xu54647265 2009-05-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 w809026418 的回复:]
可是当你打开的节点并不是只有一个呢,那我要做多少个变量啊。
[/Quote]
……你就不会变通下啊,用LIST啊
tangwei5233 2009-05-06
  • 打赏
  • 举报
回复

List<SysFun> list;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
list = SysFunManager.GetSysFuns();
TreeNode root = new TreeNode("管理员控制面板");
//root.NavigateUrl = "~/Admin/Index.aspx";
tvwAdmin.Nodes.Add(root);
FillTemp(root, 0);
}
}
public void FillTemp(TreeNode root, int parentNodeId)
{
foreach (SysFun sysFun in list)
{
if (sysFun.ParentNodeId == parentNodeId)
{
TreeNode node = new TreeNode();
node.Value = sysFun.NodeId.ToString();
node.Text = sysFun.DisplayName;
node.NavigateUrl = sysFun.NavigateUrl;
FillTemp(node, int.Parse(node.Value));
root.ChildNodes.Add(node);
}
}
}
tangwei5233 2009-05-06
  • 打赏
  • 举报
回复

public void FillTemp(TreeNode root, int parentNodeId)
{
foreach (SysFun sysFun in list)
{
if (sysFun.ParentNodeId == parentNodeId)
{
TreeNode node = new TreeNode();
node.Value = sysFun.NodeId.ToString();
node.Text = sysFun.DisplayName;
node.NavigateUrl = sysFun.NavigateUrl;
FillTemp(node, int.Parse(node.Value));
root.ChildNodes.Add(node);
}
}
}
emoheshang 2009-05-06
  • 打赏
  • 举报
回复
还有就是一个一个绑定,不一次全部绑定完,只改点点代码就ol
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = DictionaryDAL.GetDictionaryDataSet();
TreeBind(ds, "0", TreeView1.Nodes);
}

}
public static void TreeBind(DataSet ds,
string ParentId, TreeNodeCollection nodes)
{
DataView dv = new DataView();
TreeNode tmpNd;
string strId;

dv.Table = ds.Tables[0];
dv.RowFilter = "ParentId='" + ParentId + "'";

foreach (DataRowView objRow in dv)
{
tmpNd = new TreeNode();
strId = objRow["Id"].ToString() ;
tmpNd.Value = strId;
tmpNd.Text = objRow["Title"].ToString();
nodes.Add(tmpNd);
//TreeBind(ds,strId, nodes[nodes.Count - 1].ChildNodes);
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
//TextBox1.Text = TreeView1.SelectedNode.Text;
DataSet ds = DictionaryDAL.GetDictionaryDataSet();
if (TreeView1.SelectedNode.ChildNodes.Count <= 0)
{
TreeBind(ds, TreeView1.SelectedNode.Value, TreeView1.SelectedNode.ChildNodes);
}
}
emoheshang 2009-05-06
  • 打赏
  • 举报
回复
希望对你有用
emoheshang 2009-05-06
  • 打赏
  • 举报
回复
DictionaryDAL.cs里的代码
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Data;
using System;

#region DictionaryDAL
/// <summary>
/// This object represents the properties and methods of a Dictionary.
/// </summary>
public class DictionaryDAL
{
public static void AddDictionary(DictionaryInfo DInfo)
{
string Sql = "Insert into Dictionary(ParentId,Title,DirValue)values(@ParentId,@Title,@DirValue)";

SqlParameter[] arParms = new SqlParameter[3];

arParms[0] = new SqlParameter("@ParentId", SqlDbType.Int);
arParms[0].Value = DInfo.ParentId;
arParms[1] = new SqlParameter("@Title", SqlDbType.VarChar, 50);
arParms[1].Value = DInfo.Title;
arParms[2] = new SqlParameter("@DirValue", SqlDbType.VarChar, 10);
arParms[2].Value = DInfo.DirValue;
using (SqlConnection sqlconn = DALUtil.GetConnection())
{
SqlHelper.ExecuteNonQuery(sqlconn, CommandType.Text, Sql, arParms);
}
}

public static void EditDictionary(DictionaryInfo DInfo)
{
string Sql = "Update Dictionary set ParentId = @ParentId,Title = @Title,DirValue = @DirValue Where Id = @Id";

SqlParameter[] arParms = new SqlParameter[5];
arParms[0] = new SqlParameter("@Id", SqlDbType.Int);
arParms[0].Value = DInfo.Id;
arParms[1] = new SqlParameter("@ParentId", SqlDbType.Int);
arParms[1].Value = DInfo.ParentId;
arParms[2] = new SqlParameter("@Title", SqlDbType.VarChar, 50);
arParms[2].Value = DInfo.Title;
arParms[3] = new SqlParameter("@DirValue", SqlDbType.VarChar, 10);
arParms[3].Value = DInfo.DirValue;
using (SqlConnection sqlconn = DALUtil.GetConnection())
{
SqlHelper.ExecuteNonQuery(sqlconn, CommandType.Text, Sql, arParms);
}
}

public static void DelDictionary(int Id)
{
string Sql = "Delete from Dictionary Where Id = @Id";

SqlParameter[] arParms = new SqlParameter[1];
arParms[0] = new SqlParameter("@Id", SqlDbType.Int);
arParms[0].Value = Id;
using (SqlConnection sqlconn = DALUtil.GetConnection())
{
SqlHelper.ExecuteNonQuery(sqlconn, CommandType.Text, Sql, arParms);
}
}

public static DataSet GetDictionaryDataSet()
{
string Sql = "Select * from Dictionary";

DataSet ds = new DataSet();
using (SqlConnection sqlconn = DALUtil.GetConnection())
{
ds = SqlHelper.ExecuteDataset(sqlconn, CommandType.Text, Sql);
return ds;
}
}

private static DictionaryInfo GetDictionaryInfoByReader(SqlDataReader reader)
{
DictionaryInfo DInfo = new DictionaryInfo();
if (reader != null && !reader.IsClosed && reader.Read())
{
if (reader["Id"] != DBNull.Value)
{
DInfo.Id = (int)reader["Id"];
}

if (reader["ParentId"] != DBNull.Value)
{
DInfo.ParentId = (int)reader["ParentId"];
}

if (reader["Title"] != DBNull.Value)
{
DInfo.Title = (string)reader["Title"];
}

if (reader["DirValue"] != DBNull.Value)
{
DInfo.DirValue = (string)reader["DirValue"];
}

}
else
{
throw new Exception("DataAccess Errors");
}
reader.Close();
return DInfo;
}

public static DictionaryInfo GetDictionaryInfoById(int Id)
{
string Sql = "Select * from Dictionary where Id = @Id";

SqlParameter[] arParms = new SqlParameter[1];
arParms[0] = new SqlParameter("@Id", SqlDbType.Int);
arParms[0].Value = Id;

SqlDataReader sReader = null;
using (SqlConnection sqlconn = DALUtil.GetConnection())
{
sReader = SqlHelper.ExecuteReader(sqlconn, CommandType.Text, Sql, arParms);
return GetDictionaryInfoByReader(sReader);
}
}
}
#endregion
Default.aspx.cs里面的代码
using System;
using System.Data;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = DictionaryDAL.GetDictionaryDataSet();
TreeBind(ds, "0", TreeView1.Nodes);
}

}
public static void TreeBind(DataSet ds,
string ParentId, TreeNodeCollection nodes)
{
DataView dv = new DataView();
TreeNode tmpNd;
string strId;

dv.Table = ds.Tables[0];
dv.RowFilter = "ParentId='" + ParentId + "'";

foreach (DataRowView objRow in dv)
{
tmpNd = new TreeNode();
strId = objRow["Id"].ToString() ;
tmpNd.Value = strId;
tmpNd.Text = objRow["Title"].ToString();
nodes.Add(tmpNd);
TreeBind(ds,strId, nodes[nodes.Count - 1].ChildNodes);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string Title = TextBox1.Text;
//Response.Write(FormateString(Name,10,'0'));
SearchNode(Title, TreeView1.Nodes[0]);
}

private void SearchNode(string SearchName, TreeNode node)
{
if (node.Text.IndexOf(SearchName) >= 0)
{
node.Checked = true;
}
for (int i = 0; i < node.ChildNodes.Count; i++)
{
SearchNode(SearchName, node.ChildNodes[i]);
}

}

private string FormateString(string NoFormateString, int FormatedLong, char Fill)
{
int Strlen = NoFormateString.Length;
if (Strlen >= FormatedLong) return NoFormateString;
for (int i = 0; i < FormatedLong - Strlen; i++)
{
NoFormateString = Fill.ToString() + NoFormateString;
}
return NoFormateString;
}
protected void Button2_Click(object sender, EventArgs e)
{
int ParentId = TreeView1.SelectedNode == null ? 0 : Convert.ToInt32(TreeView1.SelectedNode.Value);

DictionaryInfo dInfo = new DictionaryInfo();
dInfo.Title = TextBox1.Text;
dInfo.ParentId = ParentId;
dInfo.DirValue = "";

try
{
DictionaryDAL.AddDictionary(dInfo);
Page.ClientScript.RegisterClientScriptBlock(GetType(), "aaa", "<script>alert('Success!!');</script>");
}
catch
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "aaa", "<script>alert('Fail!!!');</script>");
}

TreeView1.Nodes.Clear();
TreeView1.ExpandDepth =2 ;
DataSet ds = DictionaryDAL.GetDictionaryDataSet();
TreeBind(ds, "0", TreeView1.Nodes);

}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
TextBox1.Text = TreeView1.SelectedNode.Text;
}
}
wuyq11 2009-05-06
  • 打赏
  • 举报
回复
保存所选值
重新绑定数据后,展开treeview
TreeView.SelectedNode.Parent.Expand()
w809026418 2009-05-06
  • 打赏
  • 举报
回复
我的QQ:809026418
emoheshang 2009-05-06
  • 打赏
  • 举报
回复
等会给你代码,记得给我分哦
emoheshang 2009-05-06
  • 打赏
  • 举报
回复
一个节点,一个节点的绑定上去,不要一下子就全部绑定上去了塞
w809026418 2009-05-06
  • 打赏
  • 举报
回复
可是当你打开的节点并不是只有一个呢,那我要做多少个变量啊。
xu54647265 2009-05-06
  • 打赏
  • 举报
回复
在程序加个变量,专门存储当前打开的节点位置,为空的时候关闭全部节点。
llsen 2009-05-06
  • 打赏
  • 举报
回复
up
gongsun 2009-05-06
  • 打赏
  • 举报
回复
额...

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧