Click here to Skip to main content
Licence 
First Posted 9 Sep 2003
Views 132,289
Bookmarked 36 times

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

By | 9 Sep 2003 | Article
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:
    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:
    <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

About the Author

SeaWater

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOK! Usefull. Thanx! Pinmembermkemals8:32 23 Aug '06  
GeneralGenerate TreeView using TreeNodeSrc Pinsussluisfer6:39 20 May '05  
GeneralProblem to set focus on a popup window PinmemberMindy019:23 21 Apr '05  
Generalproblem in Tree view Pinmembermtayyabasghar1:34 7 Apr '05  
GeneralRe: problem in Tree view PinmemberSeaWater1:40 7 Apr '05  
Smile | :) Hello are you using IE or Netscape? I have only tested this control in IE.
GeneralRe: problem in Tree view Pinmembermtayyabasghar18:54 7 Apr '05  
GeneralRe: problem in Tree view PinmemberSeaWater1:57 8 Apr '05  
GeneralThanx Pinmembermtayyabasghar18:48 15 Apr '05  
GeneralTree View Expand and select node PinmemberLakkisetty1:24 19 Oct '04  
GeneralRe: Tree View Expand and select node PinmemberSeaWater1:52 19 Oct '04  
GeneralRe: Tree View Expand and select node Pinmemberlakkisetty19:12 19 Oct '04  
GeneralRe: Tree View Expand and select node PinsussAnonymous22:08 5 Sep '05  
GeneralRe: Tree View Expand and select node Pinmemberjs1238937:32 10 Jan '07  
GeneralRe: Tree View Expand and select node [modified] PinmemberMerlin Rose2:34 5 May '09  
GeneralRe: Tree View Expand and select node PinmemberMerlin Rose2:42 5 May '09  
GeneralCOM Interface for WebControl Pinmembersonny-bob1:01 20 Dec '03  
GeneralDisabling direction keys from moving focus Pinmemberbracoute18:45 23 Nov '03  
GeneralRe: Disabling direction keys from moving focus Pinmembersambaraju19:45 16 Apr '04  
GeneralRe: Disabling direction keys from moving focus PinmemberVivivivivivivivivivian11:06 1 Jun '06  
GeneralThank You Pinsussjammyjarv21:20 21 Oct '03  
GeneralRe: Thank You PinmemberSeaWater2:17 27 Oct '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 10 Sep 2003
Article Copyright 2003 by SeaWater
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid