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.
source to share
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.
source to share