.net 3.5+VS 2008
页面:AutoCompleted.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AutoCompleted.ascx.cs"
Inherits="ExpenseManager.Common.Controls.AutoCompleted" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
<div id="listPlacement" runat="server" style="height: 150px; overflow: auto; display: none;">
</div>
<input id="txtValue" type="text" runat="server" style="display: none;" />
<div style="display: none;">
<cc1:AutoCompleteExtender ID="txtInput_AutoCompleteExtender" runat="server" DelimiterCharacters=""
Enabled="True" ServicePath="AjaxHelper.asmx" CompletionSetCount="20" MinimumPrefixLength="2"
CompletionListElementID="listPlacement" CompletionInterval="200" ServiceMethod="Get"
TargetControlID="txtInput">
</cc1:AutoCompleteExtender>
</div>
代码: AutoCompleted.ascx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
namespace ExpenseManager.Common.Controls
{
public partial class AutoCompleted : System.Web.UI.UserControl, IPostBackEventHandler
{
protected static object SELECTED = new object();
public event EventHandler<AotoCompletedEventArgs> Selected
{
add
{
this.Events.AddHandler(SELECTED, value);
}
remove
{
this.Events.RemoveHandler(SELECTED, value);
}
}
protected void OnSelected(AotoCompletedEventArgs e)
{
EventHandler<AotoCompletedEventArgs> temp = this.Events[SELECTED] as EventHandler<AotoCompletedEventArgs>;
if(temp != null)
temp(this, e);
}
public string ServicePath
{
get
{
return this.txtInput_AutoCompleteExtender.ServicePath;
}
set
{
this.txtInput_AutoCompleteExtender.ServicePath = value;
}
}
public string ServiceMethod
{
get
{
return this.txtInput_AutoCompleteExtender.ServiceMethod;
}
set
{
this.txtInput_AutoCompleteExtender.ServiceMethod = value;
}
}
public int CompletionInterval
{
get
{
return this.txtInput_AutoCompleteExtender.CompletionInterval;
}
set
{
this.txtInput_AutoCompleteExtender.CompletionInterval = value;
}
}
public int CompletionSetCount
{
get
{
return this.txtInput_AutoCompleteExtender.CompletionSetCount;
}
set
{
this.txtInput_AutoCompleteExtender.CompletionSetCount = value;
}
}
public virtual void RaisePostBackEvent(string eventArgument)
{
AotoCompletedEventArgs e = new AotoCompletedEventArgs();
e.Data = new KeyValuePair<string, string>(this.txtInput.Text, this.txtValue.Value);
this.OnSelected(e);
}
protected override void OnPreRender(EventArgs e)
{
EventHandler<AotoCompletedEventArgs> temp = this.Events[SELECTED] as EventHandler<AotoCompletedEventArgs>;
string clientFunction = string.Format("{0}OnItemSelected", this.ClientID);
if(temp != null)
{
this.txtInput_AutoCompleteExtender.OnClientItemSelected = clientFunction;
string script = string.Format(@"function {0}(source, eventArgs)
{{
document.getElementById('{1}').value=eventArgs.get_value();
__doPostBack('{2}','SELECTED');
}}", clientFunction, txtValue.ClientID, this.ClientID);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), clientFunction, script, true);
}
}
/// <summary>
/// 合作单位名称绑定
/// </summary>
public string CropUnitName
{
set
{
this.txtInput.Text = value;
}
}
/// <summary>
/// 合作单位能否编辑
/// </summary>
public bool CropUnitEnabled
{
set
{
this.txtInput.Enabled = value;
}
}
/// <summary>
/// 控件宽度
/// </summary>
public Unit Width
{
get
{
return this.txtInput.Width;
}
set
{
this.txtInput.Width = value;
}
}
}
public class AotoCompletedEventArgs : EventArgs
{
public KeyValuePair<string, string> Data
{
get;
set;
}
}
}
为控件实例添加选中项事件:
protected void Page_Load(object sender, EventArgs e)
{
this.CCDEPaymentUnit.Selected += new EventHandler<ExpenseManager.Common.Controls.AotoCompletedEventArgs> (CCPaymentUnit_Selected);
}
void CCPaymentUnit_Selected(object sender, ExpenseManager.Common.Controls.AotoCompletedEventArgs e)
{
//得到选中项的值
string strValue = e.Data.Value;
}