Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Determining which control has the focus on my web page

Reply
Thread Tools

Determining which control has the focus on my web page

 
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      04-25-2006
I'm developing a simple ASP.Net web page in Visual Web Developer 2005
Express. I know in my VB.Net code I can call the SetFocus method of my
page to set the focus to any control. But is there a way of determining
which web control currently has the focus? GetFocus would seem a nice
choice for the name but it doesn't exist.

I've scoured the .Net Framework Class Library documentation and the
internet and I've not found any way of doing what I want. Of course,
it's always possible I've missed something obvious.

I think the question is simple enough that the above makes it clear
(without, e.g., code examples) but if that's not enough information,
please let me know what clarification is needed.

Gavin Deane

 
Reply With Quote
 
 
 
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      04-25-2006
hi Gavin, I see some built in functionality in .net already that should be
detecting the last focus element since when setting focus on an element I
find it including a hidden field in my form as :
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />

and some clientside code is also injected into the page :
<script
src="/WebSite/AsyncUIHttp1/WebResource.axd?d=bTjtrHm1YZ_wUfC9XukExg2&amp;t=63 2512453832015000"
type="text/javascript"></script>

<script
src="/WebSite/AsyncUIHttp1/WebResource.axd?d=yZJctic0_pbyWkwvRJrkqg2&amp;t=63 2512453832015000"
type="text/javascript"></script>

In effect these two resource files have some clientside js methods that set
__LASTFOCUS, you can either try to call these js methods in the resource
files or simply do it yourself with minimum code.

there are many things you can do like for eg. you could add the onfocus
attribute to a text box and specify some
JavaScript code to configure what happens when the text box gets focus in
the page. something like mytextbox.Attributes.Add("onfocus",
"document.getElementById('__LASTFOCUS').value=this .id");

This should set the id of the current textbox with focus into the
__LASTFOCUS hidden field available on the page, which you can later lookup
serverside. This means you must set it on every textbox or anyother control
for whom you wish to know later in serverside code, this is the simplest
way you can do it i guess.

On the server side you can check for the __LASTFOCUS hiddenfield and findout
what value is stored in there(obviously it will be the textbox who received
the focus last)

protected void Page_Load(object sender, EventArgs e)
{

TextBox1.Focus();
TextBox1.Attributes.Add("onfocus",
"document.getElementById('__LASTFOCUS').value=this .id");
TextBox2.Attributes.Add("onfocus",
"document.getElementById('__LASTFOCUS').value=this .id");
if (Request.Form["__LASTFOCUS"] != null)
Response.Write(Request.Form["__LASTFOCUS"]);
}

Have a fine day,
Alessandro Zifiglio
<> ha scritto nel messaggio
news: oups.com...
> I'm developing a simple ASP.Net web page in Visual Web Developer 2005
> Express. I know in my VB.Net code I can call the SetFocus method of my
> page to set the focus to any control. But is there a way of determining
> which web control currently has the focus? GetFocus would seem a nice
> choice for the name but it doesn't exist.
>
> I've scoured the .Net Framework Class Library documentation and the
> internet and I've not found any way of doing what I want. Of course,
> it's always possible I've missed something obvious.
>
> I think the question is simple enough that the above makes it clear
> (without, e.g., code examples) but if that's not enough information,
> please let me know what clarification is needed.
>
> Gavin Deane
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining from which web page a cgi script is invoked? davidgould@davidgould.com Python 4 03-11-2009 03:08 AM
emulating window.focus in Body onload() event and setting focus to a control on same page Jason ASP .Net 4 05-07-2007 05:54 PM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
Is there any way to determine which control has the focus in the codebehind? José Joye ASP .Net 2 06-24-2006 11:59 AM
Determining the focus point on a page MattC ASP .Net Web Controls 0 04-19-2004 09:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57