Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在自定义控件中设置默认值_C#_Asp.net_Visual Studio 2008_Custom Controls - Fatal编程技术网

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

C# 在自定义控件中设置默认值,c#,asp.net,visual-studio-2008,custom-controls,C#,Asp.net,Visual Studio 2008,Custom Controls,我正在创建自定义web控件(textbox),它只允许textbox中的整数数据类型。 在按钮提交事件中,如果textbox中没有数据,自定义控件应该设置TextBox1.Text=0,这就是我想要的。这是我写的代码 public class MasIntTextBox : TextBox { private RequiredFieldValidator req; private RegularExpressionValidator regex; public strin

我正在创建自定义web控件(textbox),它只允许textbox中的整数数据类型。 在按钮提交事件中,如果textbox中没有数据,自定义控件应该设置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事件后被触发。 我是这方面的新手。 我遗漏了什么事件或方法?接受任何建议。
提前感谢…

您应该尝试设置TextBox.LostFocus事件的默认值。此事件在控件(TextBox)失去焦点后立即激发。

我想在自定义控件类文件中添加代码,如果用户不关注TextBox,则不会激发lostfocus/onblur。然后在初始化用户控件时向TextBox.text=“0”添加默认值。如果文本框值在其GotFocus事件上为“0”(默认值),则将其清空为空字符串(“”)。在LostFocus事件中,检查该值,如果该值为空,则应用默认值。focus选项正在工作,但正在使用0进行初始化,而不是以代码的形式工作。其文本属性为“”而不是“0”。我正在写if(string.IsnullorEmpty(this.text)){this.text=“0”;}。这是真的吗。。。?在OnInit事件中写入此代码无效,因为如果我没有输入任何内容,则无法获取click事件。文本“0”,仅获取“0”。。。