自动设置 ASP.NET 自动回发后的控件焦点






2.33/5 (5投票s)
2005年6月28日
2分钟阅读

162909

1166
自动设置 ASP.NET 自动回发后的控件焦点。
引言
如果 ASP.NET 能够自动设置自动回发后 Web 控件的焦点,那该多好?本文将向您展示一种实现此目的的相对简单的方法。
解决方案
- 下载上面的源代码并编译它。
- 将编译后的程序集作为引用包含在您的 ASP.NET 解决方案中。
- 在您的 ASP.NET 代码隐藏文件中,您会看到如下一行代码
public class WebForm1 : System.Web.UI.Page {
将其更改为
public class WebForm1 : WebControls.Page {
- 测试您的 ASP.NET 解决方案。您会发现,当您的控件自动回发时,它们现在会在页面重新加载时将焦点设置回自身。
这是如何工作的?
代码的工作方式是继承 System.Web.UI.Page
类并重写两个关键方法
OnInit()
OnPreRender()
当页面初始化时,它会递归搜索任何特定类型的控件(请参阅下面的代码)。如果找到这些控件,它将添加在自动回发期间调用的事件处理程序。当事件触发时,它会将焦点控件设置为事件发送者。
在预呈现期间,如果某个控件被设置为在加载时获取焦点,则会将 JavaScript 写入客户端,以自动将焦点设置到该控件。
新的 Page
类还具有在生命周期的任何时间设置焦点的机制。(例如,在 Page_Load
上,可以给默认控件提供初始焦点。)
public class Page : System.Web.UI.Page
{
internal System.Web.UI.WebControls.WebControl setFocusControl = null;
private void LoadControlEvents(System.Web.UI.ControlCollection controls)
{
foreach (System.Web.UI.Control control in controls)
{
if (control.Controls != null)
LoadControlEvents(control.Controls);
if (control is TextBox)
{
((TextBox)control).TextChanged +=
new EventHandler(Control_Event);
}
else if (control is Button)
{
((Button)control).Click += new EventHandler(Control_Event);
}
else if (control is LinkButton)
{
((LinkButton)control).Click +=
new EventHandler(Control_Event);
}
else if (control is ImageButton)
{
((ImageButton)control).Click += new
System.Web.UI.ImageClickEventHandler(ImageButtonHandler);
}
else if (control is DropDownList)
{
((DropDownList)control).SelectedIndexChanged +=
new EventHandler(Control_Event);
}
else if (control is ListBox)
{
((ListBox)control).SelectedIndexChanged +=
new EventHandler(Control_Event);
}
else if (control is CheckBox)
{
((CheckBox)control).CheckedChanged +=
new EventHandler(Control_Event);
}
else if (control is CheckBoxList)
{
//Not Tested
((CheckBoxList)control).SelectedIndexChanged +=
new EventHandler(Control_Event);
}
else if (control is RadioButton)
{
//Not Tested
((RadioButton)control).CheckedChanged +=
new EventHandler(Control_Event);
}
else if (control is RadioButtonList)
{
//Not Tested
((RadioButtonList)control).SelectedIndexChanged +=
new EventHandler(Control_Event);
}
else if (control is Calendar)
{
//Not Tested
((Calendar)control).SelectionChanged +=
new EventHandler(Control_Event);
}
}
}
protected override void OnInit(EventArgs e)
{
LoadControlEvents(this.Controls);
base.OnInit (e);
}
private void Control_Event(object sender, System.EventArgs e)
{
SetFocusControl(sender);
}
private void ImageButtonHandler(object sender,
System.Web.UI.ImageClickEventArgs e)
{
SetFocusControl(sender);
}
private void SetFocusControl(object sender)
{
if ((setFocusControl == null) &&
(sender is System.Web.UI.WebControls.WebControl))
setFocusControl = (System.Web.UI.WebControls.WebControl)sender;
}
public void SetFocus(System.Web.UI.WebControls.WebControl control)
{
setFocusControl = control;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
//Setfocus script
if ((setFocusControl != null) &&
(!this.IsStartupScriptRegistered("SetControlFocus")))
this.RegisterStartupScript(
"SetControlFocus",
string.Format("<script language="'\""JavaScript\"'>document" +
".getElementById('{0}').focus();</script>",
setFocusControl.ClientID));
}
}
值得考虑的要点
这段代码应该适用于继承自基本 System.Web.UI.WebControls
的任何页面。上面的代码也可以修改为将焦点设置到任何自定义控件。
我没有在 IE 6.0 以外的浏览器中测试过这段代码,但我认为任何问题都应该可以通过修改生成的 JavaScript 轻松修复。
请通过电子邮件向我发送您对该类进行的任何改进,我将保持本文的更新。