背景:
阅读新闻

Scroll to Control on Page without SmartNavigation

[日期:2005-05-30] 来源:eggheadcafe.com  作者: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.  

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);
收藏 推荐 打印 | 录入:木鸟 | 阅读:
相关新闻      
本文评论   [发表评论]   全部评论 (0)
热门评论