⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frameinputtextbox.cs

📁 如果不使用IIS,请先运行 XSP.exe,待提示已侦听 8080端口后
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace DNNLite.ServerControl
{
    /// <summary>
    ///FrameInput 的摘要说明
    /// </summary>
    /// 
    [ToolboxData("FrameInputBox")]
    [Designer(typeof(System.Web.UI.Design.WebControls.CompositeControlDesigner))]
    public class FrameInputTextBox: CompositeControl  
    {
        private TextBox txtbox;
        private Image btnshowframe ;

        protected override void RecreateChildControls()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            txtbox = new TextBox();
            txtbox.Text = _text ;

            txtbox.ID  = "input";
            Controls.Add(txtbox);


            btnshowframe = new Image();
            btnshowframe.EnableViewState = false;
            btnshowframe.ImageUrl = _imageurl; 
            //btnshowframe.Text = "";
            //btnshowframe.OnClientClick = string.Format("createframe{0}(this)", this.ClientID);

            btnshowframe.Attributes.Add("onclick", string.Format("createframe(this,'{0}','{1}','{2}','{3}')",
                this.ClientID+"chtm",
                this.ClientID + "dragDiv",
                txtbox.ClientID,
                ResolveClientUrl( _contenturl )
                ));

            Controls.Add(btnshowframe);

            //base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            
            iframe= string.Format(iframe, this.ClientID,_iframewidth ,_iframeheight ,_iframewidth-2,_iframeheight-20);
            base.OnPreRender(e);
            Page.RegisterClientScriptBlock("showframe"+ this.ClientID  , jscript);
            Page.RegisterClientScriptBlock("dragdiv", dragscript);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            

            if (_buttonStyle != null)
            {
                btnshowframe.ApplyStyle(ButtonStyle);
            }

            if (_textBoxStyle != null)
            {
                txtbox.ApplyStyle(TextBoxStyle);
                
            }


            AddAttributesToRender(writer);
            writer.AddAttribute(
                HtmlTextWriterAttribute.Cellpadding,
                "0", false);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            txtbox.RenderControl(writer);
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            btnshowframe.RenderControl(writer);
            writer.RenderEndTag();

            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.Write(iframe);

            
            
        }

        private string _text = string.Empty;

        [Description("控件的值")]
        public string Text
        {
            get { return txtbox==null?"" : txtbox.Text; }
            set { _text  = value; }
        }


        private string _imageurl;

        [Category("Styles")]
        [Description("按钮的图片") ]
        [Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string ImgUrl
        {
            get { return _imageurl; }
            set { _imageurl = value; }
        }


        #region 内容链接

        private string _contenturl;

        [Editor(typeof(System.Web.UI.Design.UrlEditor ), typeof(System.Drawing.Design.UITypeEditor))]
        public string URL
        {
            get { return _contenturl; }
            set { _contenturl = value; }
        }
        #endregion


        #region Typed Style properties

        private Style _buttonStyle;
        private Style _textBoxStyle;


        [
        Category("Styles"),
        DefaultValue(null),
        DesignerSerializationVisibility(
            DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        Description(
            "The strongly typed style for the Button child control.")
        ]
        public virtual Style ButtonStyle
        {
            get
            {
                if (_buttonStyle == null)
                {
                    _buttonStyle = new Style();
                    if (IsTrackingViewState)
                    {
                        ((IStateManager)_buttonStyle).TrackViewState();
                    }
                }
                return _buttonStyle;
            }
        }

        [
        Category("Styles"),
        DefaultValue(null),
        DesignerSerializationVisibility(
            DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        Description(
            "The strongly typed style for the TextBox child control.")
        ]
        public virtual Style TextBoxStyle
        {
            get
            {
                if (_textBoxStyle == null)
                {
                    _textBoxStyle = new Style();
                    if (IsTrackingViewState)
                    {
                        ((IStateManager)_textBoxStyle).TrackViewState();
                    }
                }
                return _textBoxStyle;
            }
        }

        private int _iframewidth=600;

        [Category("Styles"),DefaultValue(600),Description("内嵌iframe的宽度")]
        public int FrameWidth
        {
            get { return _iframewidth; }
            set { _iframewidth = value; }
        }

        private int _iframeheight=400;

        [Category("Styles"), DefaultValue(400), Description("内嵌iframe的高度")]
        public int FrameHeight
        {
            get { return _iframeheight; }
            set { _iframeheight = value; }
        }


        #endregion

        #region Custom state management
        protected override void LoadViewState(object savedState)
        {
            if (savedState == null)
            {
                base.LoadViewState(null);
                return;
            }
            else
            {
                Triplet t = savedState as Triplet;

                if (t != null)
                {
                    // Always invoke LoadViewState on the base class even if 
                    // the saved state is null.
                    base.LoadViewState(t.First);

                    if ((t.Second) != null)
                    {
                        ((IStateManager)ButtonStyle).LoadViewState(t.Second);
                    }

                    if ((t.Third) != null)
                    {
                        ((IStateManager)TextBoxStyle).LoadViewState(t.Third);
                    }
                }
                else
                {
                    throw new ArgumentException("Invalid view state .");
                }
            }
        }

        protected override object SaveViewState()
        {
            object baseState = base.SaveViewState();
            object buttonStyleState = null;
            object textBoxStyleState = null;

            if (_buttonStyle != null)
            {
                buttonStyleState =
                    ((IStateManager)_buttonStyle).SaveViewState();
            }

            if (_textBoxStyle != null)
            {
                textBoxStyleState =
                    ((IStateManager)_textBoxStyle).SaveViewState();
            }

            return new Triplet(baseState,
                buttonStyleState, textBoxStyleState);

        }

        protected override void TrackViewState()
        {
            base.TrackViewState();
            if (_buttonStyle != null)
            {
                ((IStateManager)_buttonStyle).TrackViewState();
            }
            if (_textBoxStyle != null)
            {
                ((IStateManager)_textBoxStyle).TrackViewState();
            }
        }
        #endregion

        #region 控件状态
        protected override object SaveControlState()
        {
            // Invoke the base class's method and
            // get the contribution to control state
            // from the base class.
            // If the indexValue field is not zero
            // and the base class's control state is not null,
            // use Pair as a convenient data structure
            // to efficiently save 
            // (and restore in LoadControlState)
            // the two-part control state
            // and restore it in LoadControlState.

            object obj = base.SaveControlState();

            if (_contenturl != "")
            {
                if (obj != null)
                {
                    return new Pair(obj, _contenturl);
                }
                else
                {
                    return (_contenturl);
                }
            }
            else
            {
                return obj;
            }
        }

        protected override void LoadControlState(object state)
        {
            if (state != null)
            {
                Pair p = state as Pair;
                if (p != null)
                {
                    base.LoadControlState(p.First);
                    _contenturl = (string)p.Second;
                }
                else
                {
                    if (state is int)
                    {
                        _contenturl = (string )state;
                    }
                    else
                    {
                        base.LoadControlState(state);
                    }
                }
            }
        }


        #endregion


        #region 脚本字符串
        private string iframe = @"<div style=""cursor:move;width:{1}px;height:{2}px;position:absolute;  display:none   "" id=""{0}dragDiv"" 
onmousedown=""beginDrag(this,event)"" 
     ondblclick=""closeframe()""
    >
        <div style=""position: relative; width: {1}px; height: {2}px; z-index: 999; border: solid 1px black""
        >
            <div style=""position: relative; top: 0px; left: 0px; width: {1}px; height: 19px;
                background-color: Blue;"" >
                <div style=""position: absolute; top: 0px; left: 0px; width: {1}px; height: 19px;
                    background-color: Blue; z-index: 999"">
                </div>
                <iframe frameborder=""0"" width=""100%"" height=""100%"" scrolling=""no"" style=""position: absolute;
                    top: 0px; left: 0px"" ></iframe>
            </div>
            <iframe id=""{0}chtm"" width=""{3}px"" height=""{4}px"" frameborder=""0"" style=""position: absolute; top: 20px;
                left: 1px"" src=""""></iframe>
        </div>
    </div>";

        private string jscript = @"<script>
    function createframe(sender,ifrm,dgdiv,target,src) {
    
        document.getElementById(ifrm).src = src ;
        var div = document.getElementById(dgdiv);

        div.style.display = 'block';
        
        window.targetelement=document.getElementById(target);
        window.containerdiv=document.getElementById(dgdiv);
        window.sender=sender;
        
        window.closeframe = function(args) {
            if(!(args===undefined))
                {
                   document.getElementById(target).value=args;
                }
            document.getElementById(ifrm).src = '';
            document.getElementById(dgdiv).style.display = 'none';
        }
    }
</script>
 ";

        private string dragscript = @"<script>
            
            function beginDrag(me, evt) {
                evt = evt ? evt : window.event;

                

                if(me.style.left=='')
                    me.style.left = me.offsetLeft + 'px';
                if(me.style.top=='')
                    me.style.top = me.offsetTop + 'px';
                
                var detlaX = evt.clientX - parseInt(me.style.left);
                var detlaY = evt.clientY - parseInt(me.style.top);
                if (document.all) {
                    me.attachEvent('onmousemove', move);
                    me.attachEvent('onmouseup', up);
                    me.setCapture();
                } else {
                    document.addEventListener('mousemove', move, true);
                    document.addEventListener('mouseup', up, true);
                    evt.stopPropagation();
                    evt.preventDefault();
                }
                function move(evt) {
                    me.style.left = evt.clientX - detlaX + 'px'; 
                    me.style.top = evt.clientY - detlaY + 'px';
                    if (document.all) {
                        
                    } else {
                        
                        evt.stopPropagation();
                    }
                }
                function up(evt) {
                    if (document.all) {
                        
                        me.detachEvent('onmousemove', move);
                        me.detachEvent('onmouseup', up);
                        me.releaseCapture();
                    } else {
                        
                        document.removeEventListener('mousemove', move, true);
                        document.removeEventListener('mouseup', up, true);
                        evt.stopPropagation(); 
                    }
                }
            }

</script>";


        #endregion


    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -