现准备做一个继承TextBox的自定义控件,添加了一个“DataValue”的属性,现在想要实现能在客户端设置该属性值,并且在服务端能获取该值(类似TextBox的Text属性),请问大家该如何实现? 先谢了!

解决方案 »

  1.   

    写一个类继承 textboxPublic class ZMQTextBox :TextBox
    {
        private string _datavalue;
        public string DataValue
    {
         get{;}
         set{;}
    }//methods 处理datavalue
    }
      

  2.   

    以下是一个只允许输入数字的文本框的例子:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;namespace SutsComponent
    {
        public class SutsNumberBox : System.Windows.Forms.TextBox
        {
            public SutsNumberBox()
                : base()
            {
                InitializeComponent();
            }
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // SutsNumberBox
                // 
                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
                this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.SutsNumberBox_KeyPress);
                this.TextChanged += new System.EventHandler(this.SutsNumberBox_TextChanged);
                this.ResumeLayout(false);        }        private void SutsNumberBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
                if ((e.KeyChar >= '0' && e.KeyChar <= '9') ||
                    (e.KeyChar == '.' && Text.IndexOf('.') < 0) ||
                    e.KeyChar == '\b' || e.KeyChar == '\r' || e.KeyChar == '\t')
                {
                    e.Handled = false;
                }
                else
                    e.Handled = true;
            }        private void SutsNumberBox_TextChanged(object sender, EventArgs e)
            {
                if (Text == "")
                {
                    Text = "0";
                }
            }        public double GetValue()
            {
                try
                {
                    return Convert.ToDouble(Text.Trim());
                }
                catch
                {
                    return 0;
                }
            }        [Description("得到或设置文本框中的数值!"), Category("表达式")]
            public double DoubleValue
            {
                set
                {
                    Text = value.ToString();
                }
                get
                {
                    return GetValue();
                }
            }
        }
    }
      

  3.   

    感谢各位,很抱歉,我没把问题说清楚。上面说的“DataValue"属性已经添加了,但是不能和Text属性一样绑定了一个客户端的"Atrribute": Value,可通过Javascript为其赋值,我先现在想要在客户端通过Javascript为新添加的属性赋值,再问各位这个控件该如何写。 谢谢了!
      

  4.   

    楼主搜索一下关键字ASP.NET2.0服务器控件之创建自定义控件
      

  5.   


    对,就像这样,但这个继承的控件包含一个自定义的属性“DataValue”,我可以通过上面的JS给他赋值,但是页面提交后,服务端并不能读取该值。