简体   繁体   English

在自定义控件中设置默认值

[英]Set default value in custom control

I am creating custom web control(textbox) which allows only integer data type in textbox. 我正在创建自定义Web控件(文本框),该控件仅允许在文本框中输入整数数据类型。 On button submit event if there is no data in textbox, custom control should set TextBox1.Text=0, this is what i want. 在按钮提交事件上,如果文本框中没有数据,则自定义控件应设置TextBox1.Text = 0,这就是我想要的。 This is code which i wrote. 这是我写的代码。

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);
    }
}

I checked few events or methods for this but they got fired after btnA_click event. 我为此检查了几个事件或方法,但在btnA_click事件之后将其解雇。 I am kind of newbie in this. 在这方面,我是新手。 Any event or method i am missing? 我缺少任何事件或方法吗? Any suggestion accepted. 接受任何建议。 Thanks in Advance... 提前致谢...

You should try to set the default value on TextBox.LostFocus event. 您应该尝试在TextBox.LostFocus事件上设置默认值。 This event fires immediately after focus is lost from the control (TextBox). 控件(TextBox)失去焦点后立即触发此事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM