The following bit of code lets you use a combination of JavaScript and Code behind C# to set up a button to reset the pages focus. If the desired focus location is not visible the page posts back and the location is set to visible. otherwise the JavaScript makes a smooth jump to the desired focus location without the post back.
Scenario 1
If the destination control is not visible the JavaScript fails and the OnClick event fires
The OnClick function will then set the destination control's visible property to true and calls the focus() method.
Note that the focus method can only be called on certain controls [see this msdn articls]
Scenario 2
If the destination control is visible, the JavaScript runs and sets the focus WITHOUT a postback
The OnClick event never fires.
How to set up the JavaScript from the code behind (C# + JavaScript)
btnHold.OnClientClick = "document.getElementById('" + txtHoldReason.ClientID + "').focus();";
note: I have left out the C# OnClick event code. If you need this, send me an email or comment on this post and I will add it.
Thursday, March 6, 2008
Changing Focus() Using JavaScript and C#
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment