我正在使用C#.Net,我想知道 如何使用javascript突出显示选定的树视图节点。
答案 0 :(得分: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();
}