2011-06-20 11 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(); 
} 
関連する問題