Click here to Skip to main content
Licence 
First Posted 9 Sep 2003
Views 130,741
Bookmarked 35 times

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

By SeaWater | 9 Sep 2003
This article will provide some tips on setting focus to a TreeView control.
1 vote, 7.7%
1

2
1 vote, 7.7%
3
4 votes, 30.8%
4
7 votes, 53.8%
5
4.43/5 - 13 votes
1 removed
μ 3.74, σa 2.13 [?]

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! Pinmembermkemals9:32 23 Aug '06  
GeneralGenerate TreeView using TreeNodeSrc Pinsussluisfer7:39 20 May '05  
GeneralProblem to set focus on a popup window PinmemberMindy0110:23 21 Apr '05  
Generalproblem in Tree view Pinmembermtayyabasghar2:34 7 Apr '05  
GeneralRe: problem in Tree view PinmemberSeaWater2:40 7 Apr '05  
GeneralRe: problem in Tree view Pinmembermtayyabasghar19:54 7 Apr '05  
GeneralRe: problem in Tree view PinmemberSeaWater2:57 8 Apr '05  
GeneralThanx Pinmembermtayyabasghar19:48 15 Apr '05  
GeneralTree View Expand and select node PinmemberLakkisetty2:24 19 Oct '04  
GeneralRe: Tree View Expand and select node PinmemberSeaWater2:52 19 Oct '04  
GeneralRe: Tree View Expand and select node Pinmemberlakkisetty20:12 19 Oct '04  
GeneralRe: Tree View Expand and select node PinsussAnonymous23:08 5 Sep '05  
GeneralRe: Tree View Expand and select node Pinmemberjs1238938:32 10 Jan '07  
GeneralRe: Tree View Expand and select node [modified] PinmemberMerlin Rose3:34 5 May '09  
GeneralRe: Tree View Expand and select node PinmemberMerlin Rose3:42 5 May '09  
GeneralCOM Interface for WebControl Pinmembersonny-bob2:01 20 Dec '03  
GeneralDisabling direction keys from moving focus Pinmemberbracoute19:45 23 Nov '03  
GeneralRe: Disabling direction keys from moving focus Pinmembersambaraju20:45 16 Apr '04  
GeneralRe: Disabling direction keys from moving focus PinmemberVivivivivivivivivivian12:06 1 Jun '06  
GeneralThank You Pinsussjammyjarv22:20 21 Oct '03  
GeneralRe: Thank You PinmemberSeaWater3: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
Web04 | 2.5.120209.1 | Last Updated 10 Sep 2003
Article Copyright 2003 by SeaWater
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid