2011-06-20 40 views

回答

0

當填充樹狀你只需要添加一個JavaScript方法給每個節點,然後JavaScript方法顯示節點文本到文本框:

protected void Page_Load(object sender, EventArgs e) 
{ 
    SqlConnection sqlconn = new SqlConnection("--Your sql connection--"); 
    sqlconn.Open(); 

    DataSet ds = new DataSet(); 
    SqlDataAdapter cmd = new SqlDataAdapter("SELECT * FROM --The table with your info--",sqlconn); 
    cmd.Fill(ds, "region"); 
    DataTable dt = ds.Tables[0]; 

    PopulateNodes(dt, TreeView1.Nodes); 
} 

private void PopulateNodes(DataTable dt, TreeNodeCollection nodes) 
{ 
    string temp = ""; 

    foreach (DataRow dr in dt.Rows) 
    { 
     TreeNode tn = new TreeNode(); 

     tn.Text = dr["Location"].ToString(); 
     tn.Value = dr["Location"].ToString(); 
     if (temp != tn.Text) 
     { 
      temp = tn.Text; 
      //tn.SelectAction = TreeNodeSelectAction.Expand; 
      tn.NavigateUrl = "javascript:tree('" + dr["--Column--"].ToString() + "')"; 
      nodes.Add(tn); 
      AddChildNodes(dt, tn.ChildNodes, dr["--Column--"].ToString()); 
     } 
    } 
} 

private void AddChildNodes(DataTable dt, TreeNodeCollection node, string p) 
{ 
    foreach (DataRow dr in dt.Rows) 
    { 
     TreeNode tn = new TreeNode(); 

     if ((string)dr["Location"] == p) 
     { 
      tn.Text = dr["SubLocation"].ToString(); 
      tn.Value = dr["SubLocation"].ToString(); 
      tn.NavigateUrl = "javascript:tree('" + dr["--Column--"].ToString() + "')"; 
      node.Add(tn); 
     } 
    } 
} 

的Javascript:

function tree(text) { 
    document.getElementById('<%=TextBox1.ClientID %>').value = text.valueOf(); 
}