Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET
Article

How to set focus to a MS ASP.NET TreeView on form load

Rate me:
Please Sign up or sign in to vote.
4.40/5 (13 votes)
9 Sep 20032 min read 161.7K   36   21
This article will provide some tips on setting focus to a TreeView control.

Introduction

People have been having problems using the TreeView control from Microsoft, for ASP.NET:

Microsoft.Web.UI.WebControls.TreeView

This control can be download for free from the Microsoft website. This page contains an overview of the free web controls (not supported by MS).

We will cover the focus issues for this control in this article. The reason I chose to create this article is that there was not much information after searching multiple sites. Basically this article will explain the two main problems: trying to get a TreeView control to have focus and setting the colors properly, for hover and selected items.

Main information

Here is a list of problems trying to set focus to the Microsoft ASP.NET TreeView control:

  1. Even though you place the TreeView inside a form, you have to use the document object instead of the form object to set the focus. Here is the final code:
    VB
    Private Sub focusTreeview()
      Dim strBuilder As StringBuilder = New StringBuilder()
      strBuilder.Append("<script language='javascript'>")
      strBuilder.Append("document.getElementById('TreeView1').focus();")
       
      'Following line does nothing
      'strBuilder.Append("document.getElementById('TreeView1').select();") 
      strBuilder.Append("</script>")
      RegisterStartupScript("FocusTreeview", strBuilder.ToString)
    End Sub

    After you use the code above, the TreeView will be selected on form load and user can properly select any item from the TreeView.

  2. Once you use the document's focus method, the colors stay in "light" mode no matter what you do, but, the TreeView is actually selected correctly.
  3. The "select" statement does not have any effect on the TreeView. So if you un-comment the line above, it will have no effect on the TreeView.
  4. The way to make the colors not be in "light" mode is by using the filter style attribute in the SelectedStyle and HoverStyle as follows:
    HTML
    <iewc:treeview id="TreeView1"
    runat="server"
    DefaultStyle="font-family:arial;font-size:8pt;"
    SelectedStyle="filter=none;background-color:orange;
    font-weight:bold;" HoverStyle="filter=none;background-color:green;">
    </iewc:treeview>

Notes

  1. The filter=none has to go before the other style keywords, if not you will just get a grey color.
  2. Be sure to add Imports System.Text to the top of your code in order to use the StringBuilder object.
  3. The features described in this article only apply to MS Internet Explorer 6.x.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralOK! Usefull. Thanx! Pin
mkemals23-Aug-06 8:32
mkemals23-Aug-06 8:32 
GeneralGenerate TreeView using TreeNodeSrc Pin
luisfer20-May-05 6:39
sussluisfer20-May-05 6:39 
GeneralProblem to set focus on a popup window Pin
Member 59181821-Apr-05 9:23
Member 59181821-Apr-05 9:23 
Generalproblem in Tree view Pin
mtayyabasghar7-Apr-05 1:34
mtayyabasghar7-Apr-05 1:34 
GeneralRe: problem in Tree view Pin
SeaWater7-Apr-05 1:40
SeaWater7-Apr-05 1:40 
GeneralRe: problem in Tree view Pin
mtayyabasghar7-Apr-05 18:54
mtayyabasghar7-Apr-05 18:54 
GeneralRe: problem in Tree view Pin
SeaWater8-Apr-05 1:57
SeaWater8-Apr-05 1:57 
Smile | :) Hello here is a sample of adding static nodes. First make sure you also add this import:

Imports Microsoft.Web.UI.WebControls

Here is a sample that will add two departments with two employees.

Private Sub setupEmployees()

Dim tn As New TreeNode()
Dim tnChild As New TreeNode()

' Add static elements

' First lets add the accounting department
tn = New TreeNode()
tn.Text = "Accounting"

' next add an employee
tnChild = New TreeNode()
tnChild.Type = "Employee"
tnChild.Text = "Bob Smith"
tn.Nodes.Add(tnChild)

' next add another employee
tnChild = New TreeNode()
tnChild.Type = "Employee"
tnChild.Text = "Joe Smoe"
tn.Nodes.Add(tnChild)

' Add this department to the treeview
TreeView1.Nodes.Add(tn)

' Second lets add the personnel department
tn = New TreeNode()
tn.Text = "Personnel"

' next add an employee
tnChild = New TreeNode()
tnChild.Type = "Employee"
tnChild.Text = "Albert Boz"
tn.Nodes.Add(tnChild)

' next add another employee
tnChild = New TreeNode()
tnChild.Type = "Employee"
tnChild.Text = "Fred Anderson"
tn.Nodes.Add(tnChild)

' Add this department to the treeview
TreeView1.Nodes.Add(tn)

End Sub

GeneralThanx Pin
mtayyabasghar15-Apr-05 18:48
mtayyabasghar15-Apr-05 18:48 
GeneralTree View Expand and select node Pin
Member 143249119-Oct-04 1:24
Member 143249119-Oct-04 1:24 
GeneralRe: Tree View Expand and select node Pin
SeaWater19-Oct-04 1:52
SeaWater19-Oct-04 1:52 
GeneralRe: Tree View Expand and select node Pin
Member 143249119-Oct-04 19:12
Member 143249119-Oct-04 19:12 
GeneralRe: Tree View Expand and select node Pin
Anonymous5-Sep-05 22:08
Anonymous5-Sep-05 22:08 
GeneralRe: Tree View Expand and select node Pin
js12389310-Jan-07 7:32
js12389310-Jan-07 7:32 
GeneralRe: Tree View Expand and select node [modified] Pin
Merlin Rose5-May-09 2:34
Merlin Rose5-May-09 2:34 
GeneralRe: Tree View Expand and select node Pin
Merlin Rose5-May-09 2:42
Merlin Rose5-May-09 2:42 
GeneralCOM Interface for WebControl Pin
sobo12320-Dec-03 1:01
sobo12320-Dec-03 1:01 
GeneralDisabling direction keys from moving focus Pin
bracoute23-Nov-03 18:45
bracoute23-Nov-03 18:45 
GeneralRe: Disabling direction keys from moving focus Pin
sambaraju16-Apr-04 19:45
sambaraju16-Apr-04 19:45 
GeneralRe: Disabling direction keys from moving focus Pin
Vivivivivivivivivivian1-Jun-06 11:06
Vivivivivivivivivivian1-Jun-06 11:06 
GeneralThank You Pin
jammyjarv21-Oct-03 21:20
jammyjarv21-Oct-03 21:20 
GeneralRe: Thank You Pin
SeaWater27-Oct-03 2:17
SeaWater27-Oct-03 2:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.