[此贴子已经被作者于2006-7-8 17:51:11编辑过]
public partial class left : System.Web.UI.Page
{
DataSet ds = new DataSet();
protected void Page_Load(object sender, System.EventArgs e)
{
if(Session["UserName"].ToString().Trim()==""||Session["UserName"].ToString().Trim()==null)
{
Page.RegisterClientScriptBlock("error","<script language=javascript>alert('用户连接已超时,请重新登录!');window.location.href='logon.aspx';</script>");
}
if (!IsPostBack) buildTree();
}
private void buildTree()
{
string sql;
SqlConnection myConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnStr"]);
//SqlCommand cmd = new SqlCommand("readtree", myConn);
//SqlParameter userid = new SqlParameter("@UserID", SqlDbType.Char, 10);
//userid.Value = Session["U_No"].ToString().Trim();
//cmd.Parameters.Add(userid);
sql = "select * from treeview";
SqlCommand cmd = new SqlCommand(sql, myConn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
try
{
myConn.Open();
adapter.Fill(ds, "treeview");
InitTree(TreeView1.Nodes, "0");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
myConn.Close();
}
}
protected void InitTree(TreeNodeCollection nds, string parentID)
{
TreeNode tmpNd;
DataRow[] rows = ds.Tables[0].Select("ParentNodeId='" + parentID + "'");
foreach (DataRow drw in rows)
{
tmpNd = new TreeNode();
tmpNd.Value = drw["NodeId"].ToString().Trim();
tmpNd.Text = drw["Text"].ToString().Trim();
tmpNd.ImageUrl = drw["ImageUrl"].ToString().Trim();
tmpNd.NavigateUrl = drw["NavigateUrl"].ToString().Trim();
tmpNd.Target = drw["target"].ToString().Trim();
nds.Add(tmpNd);
InitTree(tmpNd.ChildNodes, tmpNd.Value);
}
}