When building an application you want to maintain the position and the focus of a control on the screen when a postback occures. Microsoft gives you the opportunity to use SmartNavigation. The problem with SmartNavigation is that is does not always work very well.
This article describes an alternative method to maintain to position and the focus of a control by using some code. The solution is very simple. De code provided in this article is in C# code.
We have to put some code in the Global.asax so that it is accessible easily in your application.
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 );
}
}
}
To use the code displayed above in a page you need to call the SetFocus method. You have to call this method when a postback occures. You need to call this function in the Page_Load event.
private void Page_Load(object sender, System.EventArgs e)
{
if(IsPostBack)
{
Global.SetFocus(this);
}
}
29 June 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment