司灵、李欣和张晓燕:asp.net中,CauseValidation是什么意思??

来源:百度文库 编辑:高校问答 时间:2024/05/09 10:33:48
如题

Control.CausesValidation 属性

获取或设置一个值,该值指示控件是否会引起在任何需要在接收焦点时执行验证的控件上执行验证。

下面的代码示例创建一个 Windows 窗体,该窗体添加在文本框中输入的数字。在显示结果之前会先验证文本框。

private void AddHandlers()
{
// Add the Validating and Validated handlers for textboxes.
myTextBox1.Validating +=
new System.ComponentModel.CancelEventHandler(myTextBox1_Validating);
myTextBox1.Validated +=
new System.EventHandler(myTextBox1_Validated);
myTextBox2.Validating +=
new System.ComponentModel.CancelEventHandler(myTextBox2_Validating);
myTextBox2.Validated +=
new System.EventHandler(myTextBox2_Validated);
myTextBox1.CausesValidationChanged +=
new System.EventHandler(myTextBox1_CausesValidationChanged);
myTextBox2.CausesValidationChanged +=
new System.EventHandler(myTextBox2_CausesValidationChanged);
if(myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true)
{
button1.Text = "Disable Validation";
myLabel.Text = "Validation Enabled";
this.Focus();
}
}
private void myTextBox1_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
if(!CheckIfTextBoxNumeric(myTextBox1))
{
myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
}
private void myTextBox1_Validated(object sender,System.EventArgs e)
{
myLabel.Text = "Validated first control";
}
private void myTextBox2_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
if(!CheckIfTextBoxNumeric(myTextBox2))
{
myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
}
private void myTextBox2_Validated(object sender,System.EventArgs e)
{
myLabel.Text = "Validated second control";
}
private void myTextBox1_CausesValidationChanged(object sender,System.EventArgs e)
{
myLabel.Text = "CausesValidation property was changed for First Textbox";
}
private void myTextBox2_CausesValidationChanged(object sender,System.EventArgs e)
{
myLabel.Text = "CausesValidation property was changed for Second Textbox";
}
private bool CheckIfTextBoxNumeric(TextBox myTextBox1)
{
bool isValid = true;
if(myTextBox1.Text == "")
{
isValid = false;
}
else
{
for(int i=0; i< myTextBox1.Text.Length;i++)
{
if(!(System.Char.IsNumber(myTextBox1.Text[i])))
{
myTextBox1.Text = "";
isValid = false;
break;
}
}
}
return isValid;
}
private void myButtonAdd_Click(object sender, System.EventArgs e)
{
try
{
int result = Convert.ToInt32(myTextBox1.Text) + Convert.ToInt32(myTextBox2.Text);
myLabel.Text = result.ToString();
}
catch(Exception myException)
{
myLabel.Text = "Exception : " + myException.Message;
}
}

private void button1_Click(object sender, System.EventArgs e)
{
if(myTextBox1.CausesValidation == false && myTextBox2.CausesValidation == false)
{
myTextBox1.CausesValidation = true;
myTextBox2.CausesValidation = true;
button1.Text = "Disable Validation";
myLabel.Text = "Validation Enabled";
}
else if(myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true)
{
myTextBox1.CausesValidation = false;
myTextBox2.CausesValidation = false;
button1.Text = "Enable Validation";
myLabel.Text = "Validation Disabled";
}
}