📄 autosuggestbox.cs
字号:
using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;
[assembly: System.Web.UI.WebResource("Anthem.includes.AutoSuggestBox.css", "text/css")]
[assembly: System.Web.UI.WebResource("Anthem.includes.AutoSuggestBox.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("Anthem.includes.Blank.html", "text/html")]
namespace Anthem
{
[ParseChildren(true)]
public class AutoSuggestBox : Control, INamingContainer, IUpdatableControl, ICallBackControl
{
#region Template & DataBound Properties
private ITemplate _itemTemplate = null;
private ITemplate _headerTemplate = null;
private IEnumerable _dataSource = null;
public IEnumerable DataSource
{
get
{
return _dataSource;
}
set
{
_dataSource = value;
}
}
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
TemplateContainer(typeof(AutoSuggestItem))]
public ITemplate ItemTemplate
{
get
{
return _itemTemplate;
}
set
{
_itemTemplate = value;
}
}
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TemplateContainer(typeof(AutoSuggestItem))]
public ITemplate HeaderTemplate
{
get
{
return _headerTemplate;
}
set
{
_headerTemplate = value;
}
}
#endregion
#region Inner Controls
private TextBox _textBox;
private TextBox _hiddenTextBox;
private CustomValidator _validator;
#endregion
#region Create Child Controls
protected override void CreateChildControls()
{
_textBox = new TextBox();
_textBox.ID = this.TextBoxID;
_textBox.Width = this.Width;
_textBox.UpdateAfterCallBack = _updateAfterCallBack;
_hiddenTextBox = new TextBox();
_hiddenTextBox.ID = this.TextBoxID + "_SelectedValue";
_hiddenTextBox.UpdateAfterCallBack = _updateAfterCallBack;
_hiddenTextBox.Style.Add("display", "none");
this.Controls.Add(_textBox);
this.Controls.Add(_hiddenTextBox);
if (Required)
{
_validator = new CustomValidator();
_validator.ID = "val" + this.ID;
_validator.ControlToValidate = _hiddenTextBox.ID;
_validator.ErrorMessage = this.ErrorMessage;
_validator.ValidationGroup = this.ValidationGroup;
_validator.Display = ValidatorDisplay.Dynamic;
_validator.ClientValidationFunction = string.Format("{0}ClientValidate", this.ID);
_validator.ValidateEmptyText = true;
_validator.ServerValidate += new ServerValidateEventHandler(_validator_ServerValidate);
Literal literal = new Literal();
literal.Text = " ";
this.Controls.Add(literal);
this.Controls.Add(_validator);
}
}
void _validator_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (args.Value != "0" && args.Value != "");
}
#endregion
#region Inicializa玢o
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!DesignMode)
RegisterClientScript();
}
protected override void OnLoad(EventArgs e)
{
//access the textboxes values to keep it after callback
string s = this.Text;
s = this.SelectedValue;
base.OnLoad(e);
Anthem.Manager.Register(this);
}
#endregion
#region IUpdatableControl implementation
[DefaultValue(false)]
public bool AutoUpdateAfterCallBack
{
get
{
if (ViewState["AutoUpdateAfterCallBack"] == null)
return false;
else
return (bool)ViewState["AutoUpdateAfterCallBack"];
}
set
{
if (value) UpdateAfterCallBack = true;
ViewState["AutoUpdateAfterCallBack"] = value;
}
}
private bool _updateAfterCallBack = false;
[Browsable(false), DefaultValue(false)]
public bool UpdateAfterCallBack
{
get { return _updateAfterCallBack; }
set { _updateAfterCallBack = value; }
}
[
Category("Misc"),
Description("Fires before the control is rendered with updated values.")
]
public event EventHandler PreUpdate
{
add { Events.AddHandler(EventPreUpdateKey, value); }
remove { Events.RemoveHandler(EventPreUpdateKey, value); }
}
private static readonly object EventPreUpdateKey = new object();
public virtual void OnPreUpdate()
{
EventHandler EditHandler = (EventHandler)Events[EventPreUpdateKey];
if (EditHandler != null)
EditHandler(this, EventArgs.Empty);
}
#endregion
#region ICallBackControl implementation
[DefaultValue("")]
public string CallBackCancelledFunction
{
get
{
if (null == ViewState["CallBackCancelledFunction"])
return string.Empty;
else
return (string)ViewState["CallBackCancelledFunction"];
}
set { ViewState["CallBackCancelledFunction"] = value; }
}
[DefaultValue(true)]
public bool EnableCallBack
{
get
{
if (ViewState["EnableCallBack"] == null)
return true;
else
return (bool)ViewState["EnableCallBack"];
}
set
{
ViewState["EnableCallBack"] = value;
}
}
[DefaultValue(true)]
public bool EnabledDuringCallBack
{
get
{
if (null == ViewState["EnabledDuringCallBack"])
return true;
else
return (bool)ViewState["EnabledDuringCallBack"];
}
set { ViewState["EnabledDuringCallBack"] = value; }
}
[DefaultValue("")]
public string PostCallBackFunction
{
get
{
if (null == ViewState["PostCallBackFunction"])
return string.Empty;
else
return (string)ViewState["PostCallBackFunction"];
}
set { ViewState["PostCallBackFunction"] = value; }
}
[DefaultValue("")]
public string PreCallBackFunction
{
get
{
if (null == ViewState["PreCallBackFunction"])
return string.Empty;
else
return (string)ViewState["PreCallBackFunction"];
}
set { ViewState["PreCallBackFunction"] = value; }
}
[DefaultValue("")]
public string TextDuringCallBack
{
get
{
if (null == ViewState["TextDuringCallBack"])
return string.Empty;
else
return (string)ViewState["TextDuringCallBack"];
}
set { ViewState["TextDuringCallBack"] = value; }
}
#endregion
#region Anthem Methods
[DefaultValue(false)]
public bool AutoCallBack
{
get
{
if (null == ViewState["AutoCallBack"])
return false;
else
return (bool)ViewState["AutoCallBack"];
}
set { ViewState["AutoCallBack"] = value; }
}
public override bool Visible
{
get
{
return Anthem.Manager.GetControlVisible(this, ViewState, DesignMode);
}
set { Anthem.Manager.SetControlVisible(ViewState, value); }
}
public override void RenderControl(HtmlTextWriter writer)
{
base.Visible = true;
base.RenderControl(writer);
}
#endregion
#region Register Resources
private void RegisterClientScript()
{
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("asb"))
{
#if DEBUG
Stream stream = typeof(Anthem.AutoSuggestBox).Assembly.GetManifestResourceStream("Anthem.includes.AutoSuggestBox.js");
StreamReader sr = new StreamReader(stream);
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "asb",
@"<script type=""text/javascript"">
//![CDATA[
" + sr.ReadToEnd() + @"
//]]>
</script>");
#else
this.Page.ClientScript.RegisterClientScriptInclude(
this.GetType(), "asb",
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"Anthem.includes.AutoSuggestBox.js"));
#endif
string csslink = "<link href='" +
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"Anthem.includes.AutoSuggestBox.css")
+ "' rel='stylesheet' type='text/css' />";
if (Page.Header == null)
throw new Exception("The page head is null. Add the runat=\"server\" attribute to it.");
else
{
LiteralControl include = new LiteralControl(csslink);
Page.Header.Controls.Add(include);
}
}
}
#endregion
#region Contructor
/// <summary>Initializes new instance of AutoSuggestBox/// </summary>
/// <remarks>Wire the events so the control can participate in them</remarks>
public AutoSuggestBox()
{
}
#endregion
#region Properties
public string Text
{
get
{
this.EnsureChildControls();
return _textBox.Text;
}
set
{
this.EnsureChildControls();
_textBox.Text = value;
}
}
public string SelectedValue
{
get
{
this.EnsureChildControls();
return _hiddenTextBox.Text;
}
set
{
this.EnsureChildControls();
_hiddenTextBox.Text = value;
}
}
/// <summary>
/// The message that will be displayed in case the datasource's Count property equals zero
/// </summary>
[Description("The message that will be displayed in case the datasource's Count property equals zero")]
public string ItemNotFoundMessage
{
get { return ViewState["ItemNotFoundMessage"] == null ? "" : ViewState["ItemNotFoundMessage"].ToString(); }
set { ViewState["ItemNotFoundMessage"] = value; }
}
/// <summary>
/// After the search string reaches a determined length, the control will not fire the callback
/// </summary>
[Description("After the search string reaches a determined length, the control will not fire the callback")]
public int MaxSuggestChars
{
get { return ViewState["MaxSuggestChars"] == null ? 20 : Convert.ToInt32(ViewState["MaxSuggestChars"]); }
set { ViewState["MaxSuggestChars"] = value; }
}
/// <summary>
/// The time interval, in miliseconds, that the callback is executed after the user presses a key
/// </summary>
[Description("The time interval, in miliseconds, that the callback is executed after the user presses a key")]
public int KeyPressDelay
{
get { return ViewState["KeyPressDelay"] == null ? 300 : Convert.ToInt32(ViewState["KeyPressDelay"]); }
set { ViewState["KeyPressDelay"] = value; }
}
public string MenuCSSClass
{
get { return ViewState["MenuCSSClass"] == null ? "asbMenu" : ViewState["MenuCSSClass"].ToString(); }
set { ViewState["MenuCSSClass"] = value; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -