Set Focus Controls - AutoPostBack = true
Every time user’s complaints me about page scroll issues.
For example: If a lengthy page having a dropdown list with AutoPostBack = true at bottom of page. After the selection page gets reloaded and focus will be on top of the page. User needs to again scroll down the page for next key in. Here a sample solution for help...
c#
public void SetFocus(Page sPage)
{
string[] sCtrl = null;
string sCtrlId = null;
Control sCtrlFound = default(Control);
string sCtrlClientId = null;
string sScript = null;
sCtrl = sPage.Request.Form.GetValues("__EVENTTARGET");
if ((sCtrl != null))
{
sCtrlId = sCtrl[0];
sCtrlFound = sPage.FindControl(sCtrlId);
if ((sCtrlFound != null))
{
sCtrlClientId = sCtrlFound.ClientID;
sScript = "<SCRIPT language='javascript'>document.getElementById('" + sCtrlClientId + "').focus(); if (document.getElementById('" + sCtrlClientId + "').scrollIntoView(false)) {document.getElementById('" + sCtrlClientId + "').scrollIntoView(true)} </SCRIPT>";
sPage.ClientScript.RegisterStartupScript(typeof(string), "controlFocus", sScript);
}
}
}
Call this function on page load
Example: SetFocus(this);
vb.net
Public Sub SetFocus(ByRef sPage As Page)
Dim sCtrl() As String
Dim sCtrlId As String
Dim sCtrlFound As Control
Dim sCtrlClientId As String
Dim sScript As String
sCtrl = sPage.Request.Form.GetValues("__EVENTTARGET")
If Not IsNothing(sCtrl) Then
sCtrlId = sCtrl(0)
sCtrlFound = sPage.FindControl(sCtrlId)
If Not IsNothing(sCtrlFound) Then
sCtrlClientId = sCtrlFound.ClientID
sScript = "<SCRIPT language='javascript'>document.getElementById('" + sCtrlClientId + "').focus(); if (document.getElementById('" + sCtrlClientId + "').scrollIntoView(false)) {document.getElementById('" + sCtrlClientId + "').scrollIntoView(true)} </SCRIPT>"
sPage.RegisterStartupScript("controlFocus", sScript)
End If
End If
End Sub
Example: Me.SetFocus(Page)
This will set focus on last used dropdown list.
Revision number 4, Thursday, September 03, 2009 6:46:34 PM by suthish nair
You must Login to comment.
|
Fri, Jun 26, 2009 11:52 AM
by ahillyer
|
Thank you this helped! However, I was having issues integrating it with MS Ajax and implemented the following solution effectively (with the control being inside an UpdatePanel) Protected Sub MySetFocus(ByVal p As Page) Dim strCtrl() As String = Nothing Dim strCtrlId As String = Nothing Dim strCtrlFound As Control = Nothing Dim strCtrlClientID As String = Nothing Dim strScript As String = Nothing strCtrl = p.Request.Form.GetValues("__EVENTTARGET") If strCtrl IsNot Nothing Then strCtrlId = strCtrl(0) strCtrlFound = p.FindControl(strCtrlId) If strCtrlFound IsNot Nothing Then Dim strID As String = strCtrlFound.ID strCtrlClientID = strCtrlFound.ClientID ScriptManager1.SetFocus(strCtrlClientID) End If End If End Sub -Aaron Hillyer, CITRMS
|
|
Wed, Jul 15, 2009 3:08 PM
by Grady Christie
|
Nice technique. Thank you for submitting. Grady Christie.
|
|
Fri, Jul 31, 2009 7:49 AM
by hajan
|
very nice idea rtpHarry - Thanks
|
|
Wed, Aug 26, 2009 10:05 AM
by suthish nair
|
Harry, Thanks for spell correction :-)
|
New
Fri, Dec 19, 2008 8:35 PM
by
|
Preprocessor Directives - Design Practice
What is a Preprocessor Directive? A preprocessor directive isa piece of code that is meant explicitly for the compiler. This offers a programmer to focus compilation for a specific environment at compile time instead of runtime. How do i identify a Preprocessor
|
New
Sat, Feb 7, 2009 1:22 PM
by
|
ASP Image Control Enchanced
ASP Image Control Enchanced with help of ScriptControl and javascript.When I've started working as a ASP.NET webdeveloper I was troubled by the limitations of HTML. One of such limitation that emerged as a problem later in my work was how to correctly
|
Revision #3
Fri, Jun 6, 2008 4:41 AM
by
|
Windows Communication Foundation
Windows Communication FoundationWindows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment for your services, enabling you to expose CLR types as services, and to consume other services
|