using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly: TagPrefix("ZControlLibrary", "zAsp")]
namespace ZControlLibrary
{
/// <summary>
/// 可编辑的下拉列表
/// </summary>
[ToolboxData("<{0}:EditableDropDownList runat=server></{0}:EditableDropDownList>")]
public class EditableDropDownList : GroupDropDownList
{
#region 获取或设置文本值
/// <summary>
/// 获取或设置文本值
/// </summary>
public string Value
{
get
{
return
string.IsNullOrEmpty(base.Text) ?
this.Page.Request["txt_" + base.ID] ?? (Convert.ToString(ViewState["Value"]) ?? string.Empty) :
base.Text;
}
set
{
ViewState["Value"] =
string.IsNullOrEmpty(base.Text) ?
value :
base.Text;
base.Text = this.Value;
}
}
#endregion
public EditableDropDownList() { }
#region 获取文本编辑框
/// <summary>
/// 获取文本编辑框
/// </summary>
/// <returns></returns>
private string GetTextBox()
{
StringBuilder input = new StringBuilder();
input.Append(" <input id=\"txt_" + base.ID + "\" name=\"txt_" + base.ID + "\" type=\"text\"");
if (!string.IsNullOrEmpty(this.Value))
{
if (!string.IsNullOrEmpty(this.Text))
{
input.Append(" disabled=\"disabled\"");
}
input.Append(" value=\"" + this.Value + "\"");
}
else
{
input.Append(" autofocus=\"autofocus\"");
}
input.Append(" />");
return input.ToString();
}
#endregion
#region 重组控件内容
protected override void Render(HtmlTextWriter writer)
{
string input = GetTextBox();
StringWriter html = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(html);
base.Render(tw);
string outhtml = html.ToString() + input;
if (!base.AutoPostBack && outhtml.IndexOf("onchange") < 0)
{
outhtml = outhtml.Insert(outhtml.IndexOf('>'), " οnchange=\"var obj=document.getElementById('txt_" + base.ID + "');obj.value=this.options[this.selectedIndex].value;if(obj.value!=null&&obj.value!=''&&obj.value!=undefined){obj.setAttribute('disabled','disabled')}else{obj.removeAttribute('disabled');obj.focus()};\"");
}
writer.Write(outhtml);
}
#endregion
}
}
此源码由周庆东编写,转载请表明出处。