-1

I have a TreeView in my webform and I need it to keep its focus after being selected. Is there anyway I can achieve this?

<asp:TreeView ID="ReportList" runat="server" BorderWidth="0px" BorderColor="0"
  Font-Names="Arial" Font-Size="Small" ForeColor="Blue" SelectedNodeStyle-ForeColor="Red"
  SelectedNodeStyle-VerticalPadding="0" ExpandDepth="0" ImageSet="Arrows" EnableClientScript="False"
  PopulateNodesFromClient="false" OnSelectedNodeChanged="TreeView_Select_Change"
  OnTreeNodePopulate="PopulateNode" EnableViewState="true" BorderStyle="Solid" Height="600px">
    <LevelStyles>
        <asp:TreeNodeStyle ChildNodesPadding="10" Font-Bold="true" Font-Size="9pt" ForeColor="LightBlue" />
        <asp:TreeNodeStyle ChildNodesPadding="5" Font-Bold="true" Font-Size="8pt" />
        <asp:TreeNodeStyle ChildNodesPadding="5" Font-Underline="true" Font-Size="8pt" />
        <asp:TreeNodeStyle ChildNodesPadding="10" Font-Size="8pt" />
    </LevelStyles>
    <Nodes>
        <asp:TreeNode Text="Keyon Reports" SelectAction="Expand" PopulateOnDemand="true" />
    </Nodes>
</asp:TreeView>
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
Lyle
  • 419
  • 1
  • 6
  • 26
  • Hi Lyle! Try to get in the habit of marking an answer as correct or helpful... There's a checkmark next to each response that you can click if you wish to mark it as such. You will earn reputation points for marking one, and the answerer will earn points for helping. Thanks! – James King Jan 17 '12 at 22:13

1 Answers1

2

You're losing the focus because of the postback from the OnSelectedNodeChanged event... while the values in the treeview are retained, properties like the focused control aren't carried in the viewstate.

In your page load event, try this:

if (IsPostBack) {
    ReportList.Focus();
}


Edit:

The treeview does have a viewstate; the page doesn't track which control has focus.

If you're only worried about keeping the selected node in view, there are ways to scroll an element into view in javascript. See here or here.

If you want to avoid issues with the postback, you can work with the treeview client-side using javascript. Some excellent examples are here.

Community
  • 1
  • 1
James King
  • 6,233
  • 5
  • 42
  • 63
  • Are you looking to give the focus to a specific node on the tree? Do you mean you want to select something, or give focus to the control? – James King Nov 29 '10 at 16:52
  • If the treeview is populated with alot of items, when you select the leaf node it scrolls back to the top of the treeview upon postback. I'd like it to keep focus on the selected node. – Lyle Nov 30 '10 at 16:31
  • I've set the EnableViewState to true?? What good is that if the treeview doesn't work with the ViewState? – Lyle Nov 30 '10 at 16:35