Click here to Skip to main content
15,393,377 members
Articles / Web Development / ASP.NET
Posted 9 Sep 2003


36 bookmarked

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
This article will provide some tips on setting focus to a TreeView control.


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


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:
    Private Sub focusTreeview()
      Dim strBuilder As StringBuilder = New StringBuilder()
      strBuilder.Append("<script language='javascript'>")
      'Following line does nothing
      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:
    <iewc:treeview id="TreeView1"
    font-weight:bold;" HoverStyle="filter=none;background-color:green;">


  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.


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


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

GeneralOK! Usefull. Thanx! Pin
mkemals23-Aug-06 8:32
Membermkemals23-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
MemberMember 59181821-Apr-05 9:23 
Generalproblem in Tree view Pin
mtayyabasghar7-Apr-05 1:34
Membermtayyabasghar7-Apr-05 1:34 
GeneralRe: problem in Tree view Pin
SeaWater7-Apr-05 1:40
MemberSeaWater7-Apr-05 1:40 
GeneralRe: problem in Tree view Pin
mtayyabasghar7-Apr-05 18:54
Membermtayyabasghar7-Apr-05 18:54 
GeneralRe: problem in Tree view Pin
SeaWater8-Apr-05 1:57
MemberSeaWater8-Apr-05 1:57 
GeneralThanx Pin
mtayyabasghar15-Apr-05 18:48
Membermtayyabasghar15-Apr-05 18:48 
GeneralTree View Expand and select node Pin
Member 143249119-Oct-04 1:24
MemberMember 143249119-Oct-04 1:24 
GeneralRe: Tree View Expand and select node Pin
SeaWater19-Oct-04 1:52
MemberSeaWater19-Oct-04 1:52 
Hello, it has been more than a year since I have not used the treeview control but, have you tried setting the SelectedNodeIndex property of the treeview control in your code-behind to the node you want to expand?
GeneralRe: Tree View Expand and select node Pin
Member 143249119-Oct-04 19:12
MemberMember 143249119-Oct-04 19:12 
GeneralRe: Tree View Expand and select node Pin
Anonymous5-Sep-05 22:08
MemberAnonymous5-Sep-05 22:08 
GeneralRe: Tree View Expand and select node Pin
js12389310-Jan-07 7:32
Memberjs12389310-Jan-07 7:32 
GeneralRe: Tree View Expand and select node [modified] Pin
Merlin Rose5-May-09 2:34
MemberMerlin Rose5-May-09 2:34 
GeneralRe: Tree View Expand and select node Pin
Merlin Rose5-May-09 2:42
MemberMerlin Rose5-May-09 2:42 
GeneralCOM Interface for WebControl Pin
sobo12320-Dec-03 1:01
Membersobo12320-Dec-03 1:01 
GeneralDisabling direction keys from moving focus Pin
bracoute23-Nov-03 18:45
Memberbracoute23-Nov-03 18:45 
GeneralRe: Disabling direction keys from moving focus Pin
sambaraju16-Apr-04 19:45
Membersambaraju16-Apr-04 19:45 
GeneralRe: Disabling direction keys from moving focus Pin
Vivivivivivivivivivian1-Jun-06 11:06
MemberVivivivivivivivivivian1-Jun-06 11:06 
GeneralThank You Pin
jammyjarv21-Oct-03 21:20
Memberjammyjarv21-Oct-03 21:20 
GeneralRe: Thank You Pin
SeaWater27-Oct-03 2:17
MemberSeaWater27-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.