I have almost 20 textboxes and to check all i have to call validate event 20 times each using errorprovider. Is there any efficient way other than that.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);
this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);
// And so on for the 20 boxes.
private void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox textbox = (TextBox)sender;
// Do whatever yo need to do with textbox here.
}
回答2:
Create validators (RequiredFieldValidator or whatnot) for each of them, then assign them all to the same ValidationGroup. You can enforce validation of all the controls in that group at once.
http://msdn.microsoft.com/en-us/library/ms227424.aspx