Figure out focused control

I am trying to add a CSS class to a control that will receive focus as soon as the page is rendered. While the SetFocus () method of the page class allows me to set the control, there is no corresponding GetFocus () method.

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

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

Any help would be greatly appreciated.

Clarification: This is why I want to add a CssClass server side: I am 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 until I specifically configured the focused control in my aspx.vb. If I set the focused control (I think due to a timing issue), the focus is set before my handlers attach to the input fields and hence the input is not highlighted. So my approach would be to add a highlightinput class to the focused control before rendering the page.

+1


source to share


4 answers


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



:focus{ background-color: yellow;}
      

+2


source


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



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

+1


source


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

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

This may be a mistake, but I think this is a good start.

0


source


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

Could you just add a cssclass to control the focus of the settings on the server?

controlObj).CssClass = "highlightinput"
Page.SetFocus(controlObj)

      

0


source







All Articles