zoukankan      html  css  js  c++  java
  • 複合控件的成功典范!!!(修正)

    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" %>
  • 相关阅读:
    8月7号的练习:HDU 1069&&POJ 1636&&HDU 1031&&HDU 1051&&HDU 1551
    8月8号的线段树:HDU 1754&&POJ 3264&&HDU1166
    8月6号的题目:HDU 1003&& POJ 1050&&HDU 1800&&HDU 2036&& POJ 1088(记忆化搜索)
    HDU 1052
    背包问题九讲:
    一个人的旅行 HDU 2066 &&HDU Today HDU 2112
    8月3号的LCS,LIS,LICS:Longest Ordered Subsequence&&Common Subsequence&&Greatest Common Increasing Subsequence
    那些操蛋的搜索题目:逃离迷宫&&哈密顿绕行世界问题
    C语言栈调用机制初探
    linux0.11改进之四 基于内核栈的进程切换
  • 原文地址:https://www.cnblogs.com/luoyaoquan/p/2030357.html
Copyright © 2011-2022 走看看