• 複合控件的成功典范!!!(修正)


    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Drawing;

    namespace CreateTech.Retail.ServerControl
    {
        [DefaultProperty(
    "Text")]
        [ToolboxData(
    "<{0}:BaseDataSelecter runat=\"server\"></{0}:BaseDataSelecter>")]
        
    public class BaseDataSelecter : WebControl
        {
            
    private System.Web.UI.WebControls.HiddenField _hiddenField=new HiddenField();
            
    private System.Web.UI.WebControls.TextBox _textBoxCode=new TextBox();
            
    private System.Web.UI.WebControls.TextBox _textBoxName=new TextBox();

            [Browsable(
    false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            
    //复合控件中隱藏文本框的ClientID
            public string HiddenFieldClientID
            {
                
    get
                {
                    EnsureChildControls();
                    
    return _hiddenField.ClientID;
                }
            }

            [Browsable(
    false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            
    //复合控件中CODE文本框的ClientID
            public string TextBoxCodeClientID
            {
                
    get
                {
                    EnsureChildControls();
                    
    return _textBoxCode.ClientID;
                }
            }

            [Browsable(
    false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            
    //复合控件中Name文本框的ClientID
            public string TextBoxNameClientID
            {
                
    get
                {
                    EnsureChildControls();
                    
    return _textBoxName.ClientID;
                }
            }

            
    public override ControlCollection Controls
            {
                
    get
                {
                    EnsureChildControls();
                    
    return base.Controls;
                }
            }

            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            
    public string Text
            {
                
    get
                {
                    EnsureChildControls();
                    
    return ViewState["Text"== null ? _hiddenField.Value : ViewState["Text"].ToString();
                }
                
    set
                {
                    EnsureChildControls();
                    ViewState[
    "Text"= value;
                }
            }

            [Browsable(
    false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            
    //复合控件中隱藏文本框的ID
            public string HiddenFieldID 
            {
                
    get
                {
                    EnsureChildControls();
                    
    return "hid"+this.FieldName+"Id";
                }
            }

            [Browsable(
    false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            
    //复合控件中CODE文本框的ID
            public string TextBoxCodeID
            {
                
    get
                {
                    EnsureChildControls();
                    
    return "txt" + this.FieldName + "Code";
                }
            }

            [Browsable(
    false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            
    //复合控件中Name文本框的ID
            public string TextBoxNameID
            {
                
    get
                {
                    EnsureChildControls();
                    
    return "txt" + this.FieldName + "Name";
                }
            }

            
    private string _FieldName = "";
            [Bindable(
    true)]
            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            [Localizable(
    true)]
            [Description(
    "用於生成ID的特定字符串")]
            
    public string FieldName
            {
                
    get 
                {
                    EnsureChildControls();
                    
    return _FieldName;
                }
                
    set
                {
                    EnsureChildControls();
                    _FieldName 
    = value;
                }
            }

            
    private string _BaseDataType = "";
            [Bindable(
    true)]
            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            [Localizable(
    true)]
            [Description(
    "基礎資料的類型")]
            
    public string BaseDataType
            {
                
    get
                {
                    EnsureChildControls();
                    
    return _BaseDataType;
                }
                
    set
                {
                    EnsureChildControls();
                    _BaseDataType 
    = value;
                }
            }

            
    private string _BaseDataUrl = "";
            [Bindable(
    true)]
            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            [Localizable(
    true)]
            [Description(
    "選擇基礎資料的頁面地址")]
            
    public string BaseDataUrl
            {
                
    get
                {
                    EnsureChildControls();
                    
    return _BaseDataUrl;
                }
                
    set
                {
                    EnsureChildControls();
                    _BaseDataUrl 
    = value;
                }
            }

            
    private string _BaseDataID = "";
            [Bindable(
    true)]
            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            [Localizable(
    true)]
            [Description(
    "基礎資料的ID")]
            
    public string BaseDataID
            {
                
    get
                {
                    EnsureChildControls();
                    
    if (_BaseDataID != "")
                    {
                        
    return _BaseDataID;
                    }
                    
    else
                    {
                        
    return _hiddenField.Value;
                    }
                }
                
    set
                {
                    EnsureChildControls();
                    _BaseDataID 
    = value;
                }
            }

            
    private string _BaseDataCode = "";
            [Bindable(
    true)]
            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            [Localizable(
    true)]
            [Description(
    "基礎資料的Code")]
            
    public string BaseDataCode
            {
                
    get
                {
                    EnsureChildControls();
                    
    if (_BaseDataCode != "")
                    {
                        
    return _BaseDataCode;
                    }
                    
    else
                    {
                        
    return _textBoxCode.Text;
                    }
                }
                
    set
                {
                    EnsureChildControls();
                    _BaseDataCode 
    = value;
                }
            }

            
    private string _BaseDataName = "";
            [Bindable(
    true)]
            [Category(
    "Appearance")]
            [DefaultValue(
    "")]
            [Localizable(
    true)]
            [Description(
    "基礎資料的Code")]
            
    public string BaseDataName
            {
                
    get
                {
                    EnsureChildControls();
                    
    if (_BaseDataName != "")
                    {
                        
    return _BaseDataName;
                    }
                    
    else
                    {
                        
    return _textBoxName.Text;
                    }
                }
                
    set
                {
                    EnsureChildControls();
                    _BaseDataName 
    = value;
                }
            }

            
    protected override void CreateChildControls()
            {
                Controls.Clear();
                
    //_hiddenField = new HiddenField();
                _hiddenField.ValueChanged += new EventHandler(_hiddenField_ValueChanged);
                
    //_textBoxCode = new TextBox();
                _textBoxCode.TextChanged += new EventHandler(_hiddenField_ValueChanged);
                
    //_textBoxName = new TextBox();
                
    //_hiddenField.ID = HiddenFieldID;
                
    //_textBoxCode.ID = TextBoxCodeID;
                _textBoxCode.BackColor = ColorTranslator.FromHtml("#F0F0F0");
                _textBoxCode.Attributes.Add(
    "disabled""disabled");
                _textBoxCode.Width 
    = Unit.Parse("48px");
                _textBoxCode.Height 
    = Unit.Parse("15px");
                _textBoxCode.BorderWidth 
    = Unit.Parse("0px");
                
    //_textBoxName.ID = TextBoxNameID;
                _textBoxName.BackColor = ColorTranslator.FromHtml("#F0F0F0");
                _textBoxName.Attributes.Add(
    "disabled""disabled");
                _textBoxName.Width 
    = Unit.Parse("120px");
                _textBoxName.BorderWidth 
    = Unit.Parse("0px");
                Controls.Add(_hiddenField);
                Controls.Add(_textBoxCode);
                Controls.Add(_textBoxName);
            }

            
    private void _hiddenField_ValueChanged(object sender, EventArgs e)
            {
                BaseDataID 
    = _hiddenField.Value;
            }

            
    protected override void OnInit(EventArgs e)
            {
                
    base.OnInit(e);
                _hiddenField.ID 
    = HiddenFieldID;
                _textBoxName.ID 
    = TextBoxNameID;
                _textBoxCode.ID 
    = TextBoxCodeID;
            }

            
    ///
            
    /// 将此控件呈现给指定的输出参数。
            
    ///
            
    /// 要写出到的 HTML 编写器
            protected override void Render(HtmlTextWriter output)
            {
                EnsureChildControls();
                output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, 
    "0");
                output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, 
    "0");
                output.AddAttribute(HtmlTextWriterAttribute.Border,
    "0");
                output.AddAttribute(HtmlTextWriterAttribute.Style, 
    "border:solid 1px #AECAF0;border-collapse:collapse; background:#F0F0F0;");

                output.RenderBeginTag(HtmlTextWriterTag.Table);

                output.RenderBeginTag(HtmlTextWriterTag.Tr);

                output.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, 
    "0px");
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                
    //_hiddenField.ID = HiddenFieldID;
                
    //_hiddenField.Value = BaseDataID;
                
    //_textBoxCode.ID = TextBoxCodeID;
                
    //_textBoxCode.Text = BaseDataCode;
                _hiddenField.RenderControl(output);
                _textBoxCode.RenderControl(output);
                output.RenderEndTag();

                output.AddAttribute(HtmlTextWriterAttribute.Style, 
    "border-left:solid 1px #AECAF0; border-right:solid 1px #AECAF0; background:#D4D4D4;");
                output.RenderBeginTag(HtmlTextWriterTag.Td);

                
    //output.AddAttribute(HtmlTextWriterAttribute.Id, "xxx"+HiddenFieldID);
                output.AddAttribute(HtmlTextWriterAttribute.Src, this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "CreateTech.Retail.ServerControl.Resources.base_data_select.jpg"));
                output.AddAttribute(HtmlTextWriterAttribute.Style, 
    "border:0px; 20px; height:20px; cursor:pointer;");
                output.AddAttribute(HtmlTextWriterAttribute.Onclick, 
    "if(typeof(popupURL)!=\"undefined\"){popupURL.SetContentHTML('');popupURL.SetContentUrl('" + BaseDataUrl + "?id="+HiddenFieldClientID+"&code="+TextBoxCodeClientID+"&name="+TextBoxNameClientID+"&t=" + BaseDataType + "&sid=" + FieldName + "&v='+(new Date()).getTime());popupURL.Show();}");
                output.RenderBeginTag(HtmlTextWriterTag.Img);
                output.RenderEndTag();
                output.RenderEndTag();

                output.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, 
    "0px");
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                
    //_textBoxName.ID = TextBoxNameID;
                _textBoxName.Text = BaseDataName;
                _textBoxName.RenderControl(output);
                output.RenderEndTag();

                output.RenderEndTag();

                output.RenderEndTag();
            }
        }
    }
    <cc1:BaseDataSelecter ID="txtBrand" runat="server" FieldName="Brand" BaseDataType="10000" BaseDataUrl="PopupControl/SelectBaseData.aspx" />
    <%@ Register Assembly="CreateTech.Retail.ServerControl" Namespace="CreateTech.Retail.ServerControl"
        TagPrefix
    ="cc1" %>
  • 相关阅读:
    Python命名规范
    安装pywin32模块
    深度学习框架
    更快速的学习掌握新知识
    使用 Docker + SSH代理 来实现访问内网网站
    magento Too many arguments, expected arguments "command".
    使用 Prestissimo 提高 composer 下载速度
    使用Lebab 将 Javascript ES5 转 ES6
    分享网络上学习英语的方法或技巧
    使用 Expo 的错误 WebSocket connection to 'ws://localhost:19002/debugger-proxy?role=debugger&name=Chrome' failed: Error during WebSocket handshake: Unexpected response code: 400
  • 原文地址:https://www.cnblogs.com/luoyaoquan/p/2030357.html
Copyright © 2020-2023  润新知