实现可编辑下拉框的ComboBox asp.net控件方法
2010-10-17 14:32:09 来源:WEB开发网核心提示:好久没更新了,发个有意思的,实现可编辑下拉框的ComboBox asp.net控件方法,可编辑dropdownlist 控件,感觉自己写的挺牛的呵呵
好久没更新了,发个有意思的,可编辑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(); } }
更多精彩
赞助商链接