树型控件的使用

 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;
    }
}

 

还有就是一个一个绑定,不一次全部绑定完,只改点点代码就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);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值