我正在创建自定义 Web 控件(文本框),它只允许文本框中的整数数据类型。如果文本框中没有数据,则在按钮提交事件上,自定义控件应设置 TextBox1.Text=0,这就是我想要的。这是我写的代码。
public class MasIntTextBox : TextBox
{
private RequiredFieldValidator req;
private RegularExpressionValidator regex;
public string ValGrp { get; set; }
public string IsRequired { get; set; }//give true/yes or false/no values only
protected override void OnInit(EventArgs e)
{
//this.CssClass = "inp-form";
if (IsRequired == "true" || IsRequired == "yes")
{
req = new RequiredFieldValidator();
req.ControlToValidate = this.ID;
req.ErrorMessage = "Enter Numeric Value";
req.Display = ValidatorDisplay.Dynamic;
if (!string.IsNullOrEmpty(ValGrp))
req.ValidationGroup = ValGrp;
Controls.Add(req);
}
regex = new RegularExpressionValidator();
regex.ControlToValidate = this.ID;
regex.ErrorMessage = "Numeric Value Only";
regex.Display = ValidatorDisplay.Dynamic;
regex.ValidationExpression = "^\\d+(\\.\\d+)?$";
if (!string.IsNullOrEmpty(ValGrp))
regex.ValidationGroup = ValGrp;
Controls.Add(regex);
//base.OnInit(e);
}
protected override void Render(HtmlTextWriter w)
{
base.Render(w);
if (IsRequired == "true" || IsRequired == "yes")
req.RenderControl(w);
regex.RenderControl(w);
}
}
我为此检查了一些事件或方法,但它们在 btnA_click 事件之后被解雇了。我是这方面的新手。我缺少任何事件或方法吗?接受任何建议。提前致谢...