/// <summary> /// Adds Javascript code to safely set focus on the specified control. /// </summary> public static void Focus(this Web.UI.Control control, bool safe) { if (!safe) { control.Focus(); } var script = @"setTimeout(function() { var fn = function() { var control = $get('#CLIENT_ID#'); if (control && control.focus) { try { control.focus(); } catch (err) { } } try{ Sys.Application.remove_load(fn); } catch (err) { } }; Sys.Application.add_load(fn); fn(); }, 10);".Replace("#CLIENT_ID#", control.ClientID); ScriptManager.RegisterStartupScript(control.Page, control.GetType(), control.ClientID + "_SetFocusOnLoad", script, addScriptTags: true); }
/// <summary>Applies editor modifications after creating the control.</summary> /// <param name="editor">The editor to modify.</param> public virtual void Modify(Control editor) { if (this.DataBind) editor.DataBind(); if (this.Focus) editor.Focus(); }
/// <summary> /// 验证表单项 /// </summary> /// <param name="field"></param> /// <param name="control"></param> /// <returns></returns> protected virtual Boolean ValidFormItem(FieldItem field, Control control) { // 必填项 if (!field.IsNullable) { if (field.Type == typeof(String)) { if (String.IsNullOrEmpty((String)Entity[field.Name])) { WebHelper.Alert(String.Format("{0}不能为空!", field.DisplayName)); control.Focus(); return false; } } else if (field.Type == typeof(DateTime)) { DateTime d = (DateTime)Entity[field.Name]; if (d == DateTime.MinValue || d == DateTime.MaxValue) { WebHelper.Alert(String.Format("{0}不能为空!", field.DisplayName)); control.Focus(); return false; } } } return true; }
/// <summary>检查控件值是否为空,若为空,显示错误信息,并聚焦到控件上</summary> /// <param name="control">要检查的控件</param> /// <param name="errmsg">错误信息。若为空,将使用ToolTip</param> /// <returns></returns> public static Boolean CheckEmptyAndFocus(Control control, String errmsg) { if (control == null) throw new ArgumentNullException("control"); if (control is WebControl && String.IsNullOrEmpty(errmsg)) errmsg = (control as WebControl).ToolTip; if (control is TextBox) { TextBox box = control as TextBox; if (!String.IsNullOrEmpty(box.Text)) return true; } else if (control is ListControl) { ListControl box = control as ListControl; if (!String.IsNullOrEmpty(box.Text)) return true; } else throw new XException("暂时不支持{0}控件!", control.GetType()); control.Focus(); if (!String.IsNullOrEmpty(errmsg)) Alert(errmsg); return false; }
public void Focus_Deny_Unrestricted () { Control c = new Control (); page.Controls.Add (c); c.Focus (); // normal, no forms on page }