如何在 自定义控件中 添加 Attribute 属性 现准备做一个继承TextBox的自定义控件,添加了一个“DataValue”的属性,现在想要实现能在客户端设置该属性值,并且在服务端能获取该值(类似TextBox的Text属性),请问大家该如何实现? 先谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写一个类继承 textboxPublic class ZMQTextBox :TextBox{ private string _datavalue; public string DataValue{ get{;} set{;}}//methods 处理datavalue} 以下是一个只允许输入数字的文本框的例子: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(); } } }} 感谢各位,很抱歉,我没把问题说清楚。上面说的“DataValue"属性已经添加了,但是不能和Text属性一样绑定了一个客户端的"Atrribute": Value,可通过Javascript为其赋值,我先现在想要在客户端通过Javascript为新添加的属性赋值,再问各位这个控件该如何写。 谢谢了! 楼主搜索一下关键字ASP.NET2.0服务器控件之创建自定义控件 对,就像这样,但这个继承的控件包含一个自定义的属性“DataValue”,我可以通过上面的JS给他赋值,但是页面提交后,服务端并不能读取该值。 SQL提示列名无效! .net的网页在IE中无法打开 一些面试的题目 很牛逼哦 高手请进 自定义控件中属性接收数据绑定 ★★★一个关于datagrid的问题,紧急,请教大家 为什么我每次打开报表都要我输入数据库的用户名和密码呢? data中加一个“序号”的列怎么加? 怎么弹出对话框 请问为什么上传中文名子的图片在本机浏览没问题,上传到服务器就不能显示了 网站移植问题请教 基于角色的权限系统 请教
{
private string _datavalue;
public string DataValue
{
get{;}
set{;}
}//methods 处理datavalue
}
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();
}
}
}
}
对,就像这样,但这个继承的控件包含一个自定义的属性“DataValue”,我可以通过上面的JS给他赋值,但是页面提交后,服务端并不能读取该值。