Wednesday, March 28, 2012

Validate Datagrid inputs?

I'm trying to validate data when updating a datagrid.

I'm using a RegularExpressionValidator and a Validiationsummary.
In this case I fist tried to create the Validator in code at runtime but I couldn't get it to work!

Then I took a RegularExpressionValidator control from the toolbox and set the errorMessage and ControlToValidate properties in the code, and tried to get the control to validate from the update code:


TextBox tbtel;
tbtel = (TextBox)(e.Item.Cells[3].Controls[0]);

RegularExpressionValidator2.ControlToValidate="tbtel"; //error!!
RegularExpressionValidator2.ErrorMessage="Wrong format!";
RegularExpressionValidator2.Validate();

This didn't work!

How do I validate a control like this, that is created "in code" at runtime?
I think that it has something to do with the ID of the control!?
How do I get the ID?

It works when I have a default Textbox and validates it !
But then the validiationSummaryControl.messagebox won't get executed until I press update twise or presses a button with no code behind!?
I think it has something to do with updating the page...

Code:


TextBox tbtel;
tbtel = (TextBox)(e.Item.Cells[3].Controls[0]);
TextBox1.Text=tbtel.Text;

RegularExpressionValidator2.ControlToValidate="TextBox1";
RegularExpressionValidator2.ErrorMessage="Wrong format!";
RegularExpressionValidator2.Validate();

Is there an easy way to do this??

/Regards
AndersAs far as I know, you cannot validate datagrid inputs.

No comments:

Post a Comment