Click here to Skip to main content
Click here to Skip to main content

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

, 9 Sep 2003
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

SeaWater
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralOK! Usefull. Thanx! Pinmembermkemals23-Aug-06 8:32 
GeneralGenerate TreeView using TreeNodeSrc Pinsussluisfer20-May-05 6:39 
GeneralProblem to set focus on a popup window PinmemberMindy0121-Apr-05 9:23 
Generalproblem in Tree view Pinmembermtayyabasghar7-Apr-05 1:34 
GeneralRe: problem in Tree view PinmemberSeaWater7-Apr-05 1:40 
GeneralRe: problem in Tree view Pinmembermtayyabasghar7-Apr-05 18:54 
GeneralRe: problem in Tree view PinmemberSeaWater8-Apr-05 1:57 
GeneralThanx Pinmembermtayyabasghar15-Apr-05 18:48 
GeneralTree View Expand and select node PinmemberLakkisetty19-Oct-04 1:24 
GeneralRe: Tree View Expand and select node PinmemberSeaWater19-Oct-04 1:52 
GeneralRe: Tree View Expand and select node Pinmemberlakkisetty19-Oct-04 19:12 
GeneralRe: Tree View Expand and select node PinsussAnonymous5-Sep-05 22:08 
GeneralRe: Tree View Expand and select node Pinmemberjs12389310-Jan-07 7:32 
GeneralRe: Tree View Expand and select node [modified] PinmemberMerlin Rose5-May-09 2:34 
GeneralRe: Tree View Expand and select node PinmemberMerlin Rose5-May-09 2:42 
GeneralCOM Interface for WebControl Pinmembersonny-bob20-Dec-03 1:01 
GeneralDisabling direction keys from moving focus Pinmemberbracoute23-Nov-03 18:45 
GeneralRe: Disabling direction keys from moving focus Pinmembersambaraju16-Apr-04 19:45 
GeneralRe: Disabling direction keys from moving focus PinmemberVivivivivivivivivivian1-Jun-06 11:06 
GeneralThank You Pinsussjammyjarv21-Oct-03 21:20 
GeneralRe: Thank You PinmemberSeaWater27-Oct-03 2:17 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 10 Sep 2003
Article Copyright 2003 by SeaWater
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid