自定義DropDownList控制元件的實現

yuzhangqi發表於2008-11-26

需求:要求DropDownList控制元件支援文字輸入方式快速定位選擇項。輸入文字後按Tab鍵或Enter鍵觸發SelectedIndexChanged事件。

Code:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Configuration;

namespace Com.Elong.Commission.WebControl
{
///


/// DropDownListExtend 的摘要說明。
///

[DefaultProperty("Text"),
ToolboxData("{0}:DropDownListExtend>")]
public class DropDownListExtend : System.Web.UI.WebControls.DropDownList,INamingContainer
{
private string NEWLINE = System.Environment.NewLine;

public TextBox _TextBox;

public DropDownListExtend()
{
_TextBox = new TextBox();
}


[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return _TextBox.Text;
}

set
{
_TextBox.Text = value;
}
}


///


/// 將此控制元件呈現給指定的輸出引數。
///

/// 要寫出到的 HTML 編寫器
protected override void Render(HtmlTextWriter output)
{
int iWidth = Convert.ToInt32(base.Width.Value);
if(iWidth == 0)
{
iWidth = 102;
base.Width = Unit.Parse("102px");
}

int sWidth = iWidth + 0;
int spanWidth = sWidth - 18;

output.Write("

");


_TextBox.ID = base.ID +"_TextBox";
_TextBox.EnableViewState = true;
_TextBox.AutoPostBack = true;
_TextBox.Width = Unit.Parse(iWidth.ToString() + "px");
_TextBox.Style.Add("left", "0px");
_TextBox.Style.Add("POSITION", "absolute");
_TextBox.Attributes.Add("onchange","doSelect('" + base.ID + "'); ");


if(base.Items.Count == 0)
{
ListItem item = new ListItem();
item.Value = "";
item.Text = " --請選擇-- ";
base.Items.Add(item);
base.SelectedIndex = 0;
_TextBox.Text = base.Items[0].Text;
}

if (base.SelectedItem != null)
{
_TextBox.Text = base.SelectedItem.Text;
}

_TextBox.RenderControl(output);

output.Write("");

base.Style.Clear();
base.Width = Unit.Parse(sWidth.ToString() + "px");
base.Style.Add("MARGIN-LEFT", "-" + spanWidth.ToString() + "px");
base.Attributes.Add("onchange", "document.all('" + this._TextBox.UniqueID + "').value=this.options[this.selectedIndex].text; ");

base.Render(output);
output.Write("");

output.Write("

");

WriteJSFunc(output);
}

private void WriteJSFunc(HtmlTextWriter output)
{
string jsFileURL = System.Configuration.ConfigurationSettings.AppSettings["WebControlJS"];
output.Write("
}

////////////////採用外部javascript 檔案連線,以增強可用性和維護性////////
///
// private void WriteJSFunc(HtmlTextWriter output)
// {
// output.Write("
// }

// private void WriteKeyDownFunc(HtmlTextWriter output)
// {
// output.Write("function document.onkeydown() " + NEWLINE);
// output.Write("{" + NEWLINE);
// output.Write("if (event.keyCode == 13) " + NEWLINE);
// output.Write("{" + NEWLINE);
// output.Write("var e = event.srcElement; " + NEWLINE);
// output.Write(" if (e.tagName == "INPUT" && e.type == "text") " + NEWLINE);
// output.Write("event.keyCode = 9; " + NEWLINE);
// output.Write("} " + NEWLINE);
// output.Write("}" + NEWLINE);
// }

// private void WriteDoSelect(HtmlTextWriter output)
// {
// output.Write("function doSelect() " + NEWLINE);
// output.Write("{ " + NEWLINE);
// output.Write("var ddl = document.getElementById("" + base.UniqueID + "");" + NEWLINE);
// output.Write("var count = ddl.options.length;" + NEWLINE);
// output.Write("var selValue = document.all('" + this._TextBox.UniqueID +"').value;" + NEWLINE);
// output.Write("for (var i=0;i// output.Write(" {" + NEWLINE);
// output.Write("if (ddl.options[i].text == selValue) " + NEWLINE);
// output.Write(" {" + NEWLINE);
// output.Write("ddl.options[i].selected = true;" + NEWLINE);
// output.Write("return;" + NEWLINE);
// output.Write(" }" + NEWLINE);
// output.Write(" }" + NEWLINE);
// output.Write(" alert('沒找到查詢項');" + NEWLINE);
// output.Write(" document.all('" + this._TextBox.UniqueID +"').value = ddl.options[0].text;" + NEWLINE);
// output.Write("}" + NEWLINE);
// }

}
}

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1013858/,如需轉載,請註明出處,否則將追究法律責任。

相關文章