[原创] C# 服务器控件 可编辑的下拉框

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
    }
}

  此源码由周庆东编写,转载请表明出处。

转载于:https://www.cnblogs.com/daimala/articles/5193366.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值