Scroll to Control on Page without SmartNavigation
By Peter Bromberg
We get a number of posts and requests on how to make a page scroll to the position of a control deep down on the page that issued a postback.
We get a number of posts and requests on how to make a page scroll to the position
of a control deep down on the page that issued a postback.
As we
all know, SmartNavigation is not always appropriate. Here is a very easy way
to accomplish this. We put it in Global.asax so that it is accessible easily
as a utility method from any page in our application:
//
in Global.asax.cs:
public class Global : System.Web.HttpApplication
{
// insert static SetFocus method here, just below the
class Global declaration:
public static void SetFocus(System.Web.UI.Page
webPage)
{
string[] pbCtrl = webPage.Page.Request.Form.GetValues("__EVENTTARGET");
if (pbCtrl != null && pbCtrl.Length > 0)
{
string
ctrlId;
ctrlId = pbCtrl[0];
System.Web.UI.Control ctrlFound = webPage.Page.FindControl(ctrlId);
if ((ctrlFound != null) &&
(
ctrlFound is System.Web.UI.WebControls.DropDownList
||
ctrlFound is System.Web.UI.WebControls.TextBox ||
ctrlFound
is System.Web.UI.WebControls.RadioButton ||
ctrlFound is System.Web.UI.WebControls.RadioButtonList))
{
string ctrlClientId;
ctrlClientId = ctrlFound.ClientID;
string strScript;
strScript = "<SCRIPT language=\"javascript\">
document.getElementById('" + ctrlClientId + "').focus(); document.getElementById('"
+ ctrlClientId + "').scrollIntoView(true) </SCRIPT>";
webPage.Page.RegisterStartupScript("controlFocus",strScript
);
}
}
}
// In your Page_Load handler for
(any page:
private void Page_Load(object sender, System.EventArgs e)
{
// insert this conditional call to the SetFocus Method:
if(IsPostBack)
Global.SetFocus(this);
Submission Date: 9/23/2005
3:15:25 PM
Submitted By: Peter Bromberg
My Home Page: http://www.eggheadcafe.com
Popularity (203 Views)