好久沒更新了,發個有意思的,可編輯dropdownlist 控制項,感覺自己寫的挺牛的呵呵。跟大家共用下
代碼 public class ComboBox:System.Web.UI.WebControls.CompositeControl
{
private TextBox input;
private DropDownList select;
public TextBox TextBox {
get { this.EnsureChildControls(); return input; }
}
public DropDownList DropDownList {
get { this.EnsureChildControls(); return select; }
}
protected override void CreateChildControls()
{
//base.CreateChildControls();
this.Controls.Clear();
input = new TextBox();
input.ID = "input";
select = new DropDownList();
select.ID = "select";
this.Controls.Add(select);
this.Controls.Add(input);
this.ChildControlsCreated = true;
}
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
this.EnsureChildControls();
// <select id="test1" style="width:100px;height:20px;">
//<option>中國</option>
//<option>aaaaa</option>
//<option>ccccc</option>
//<option>香港</option>
//</select>
//<div style="width:80px; height:20px; position:absolute;" >
//<iframe frameborder="0" style="width:100%; height:100%;"></iframe>
//<input type="text" style="width:78px;height:17px;position:absolute; padding:0;" />
//</div>
select.Width = 100;
select.Height = 20;
select.RenderControl(writer);
//div
writer.AddStyleAttribute("width", "80px");
writer.AddStyleAttribute("height", "20px");
writer.AddStyleAttribute("position", "absolute");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
//iframe
writer.AddStyleAttribute("width", "100%");
writer.AddStyleAttribute("height", "100%");
writer.AddAttribute("frameborder", "0");
writer.RenderBeginTag(HtmlTextWriterTag.Iframe);
writer.RenderEndTag();
//input
input.Width = 78;
input.Height = 15;
input.Style.Add(HtmlTextWriterStyle.Position, "absolute");
input.RenderControl(writer);
//end div
writer.RenderEndTag();
// <script type="text/javascript">
// $(function () {
// var select = $('#test1');
// var offset = select.offset();
// select.change(function () { $(this).next().find('input').val($(this).val()); })
// .next().css({ left: offset.left, top: offset.top, zindex: 1000 })
// .find('input').css({ left: 0, top: 0, zindex: 1001 });
// })
//</script>
writer.AddAttribute("type", "text/javascript");
writer.RenderBeginTag(HtmlTextWriterTag.Script);
writer.Write("$(function () {var select = $('#" + this.ID + "_" + this.select.ID + "'); var offset = select.offset();");
writer.Write("select.change(function () { $(this).next().find('input').val($(this).find(':selected').text()); })");
writer.Write(".next().css({ left: offset.left, top: offset.top, zindex: 1000 })");
writer.Write(".find('input').css({ left: 0, top: 0, zindex: 1001 });})");
writer.RenderEndTag();
}
}
實現方法比較簡單,用的是群組控制項。裡麵包著TextBox和DropDownList。技巧在於用戶端。使用js和css將input正好定位到下拉框上面,遮住下拉框的顯示地區,只留下下箭頭地區。ie6下還得使用iframe來遮擋select.經測試在ie6先顯示完美。ie8,擷取顯示有點小問題。仔細校對下可以修改過了,主要原因在於不同瀏覽器select下拉框的下箭頭地區大小不一致。
使用時候也比較簡單,直接操作控制項的DropDownList屬性和TextBox屬性即可。
代碼 ComboBox1.DropDownList.DataSource = new List<object>() { new { Text = "111", value = 1 }, new { Text = "222", value = 2 } };
ComboBox1.DropDownList.DataTextField = "Text";
ComboBox1.DropDownList.DataValueField = "Value";
ComboBox1.DropDownList.DataBind();