views:

611

answers:

4

I'm trying to add a CSS class to the Control that will get the focus, once a page is rendered. While the SetFocus() method of the Page class lets me set the Control, there is no corresponding GetFocus() method.

According to the .Net sources, the information is stored in the private member _focusedControl of the Page class. The property FocusedControl is marked internal.

Is there a way to get the value of the private member or internal property by using Reflection?

Any help would be greatly appreciated.

Clarification: Here is why I want to add a CssClass server side: I'm trying to apply the following JQuery script, that changes the background of the focused element:

$(document).ready(function() {
  var elements = jQuery("textarea, select, multi-select, :text, :password, :file");

  elements.bind
  (
    'focus',
    function() {
      jQuery(this).addClass('highlightinput');
    }
  );

  elements.bind
  (
    'blur',
    function() {
      jQuery(this).removeClass('highlightinput');
    }
  );
})

This works fine as long as I don't specifically set a focused control in my aspx.vb. If I do set a focused control (I think due to a timing issue), the focus is set before my handlers are attached to the input fields and thus, the input is not highlighted. So my approach would be to add the highlightinput class to the focused control before rendering the page.

+1  A: 

The control with focus may have changed between post-backs, which is why I don't think you can find it easily. It is probably too expensive to keep the entire state of each control in ViewState.

Maybe you can track which control has focus in a hidden field on the client using javascript, and read it on the server.

Jason
That is correct. However, I don't need the information to survive a postback. I just need to know the last control, that was passed to the SetFocus method.
+2  A: 

If you are looking for a css solution for highlighting the focused element I believe you can use the ':focus' selector. I haven't tried this, but I believe this is a valid selector. You would use it like so in your css file:

:focus{ background-color: yellow;}
Yobi21
Thanks Yobi21, this works fine in Firefox but it's not supported by IE
A: 

Why not just do it all via JavaScript? Something like:

body.onLoad = function() { document.activeElement.style.color = '#ff0000'}

That may be buggy but I think it's a good start.

Shawn Simon
Thanks Shawn, I tried it but I couldn't get it to work. I would rather know server side which control will receive the focus.
A: 

I don't know if i understood correctly your question...

Couldn't you just add a cssclass to the control your setting focus to on the serverside?

controlObj).CssClass = "highlightinput"
Page.SetFocus(controlObj)
vitorsilva
The problem is, that the Page.SetFocus is called by a component I have no control over. So I would like to figure out, what control will be focused, once the page is rendered so I can set the additional CssClass. Also, I would like to have a generic approach, that I could add to a base-class