Javascript validation for DataGrid
-
Hi, I have a DataGrid where in each row has couple of text boxes and an update button. Each row is dynamically generated as the number of rows are not known ahead of time. When the user clicks the update button, I do a postback to capture the data entered. However I dont want to do a postback when the textboxes are empty. How do I prevent this using Javascript? My main problem is with ClientIDs. This is a common problem and if it has been beaten to death, please bear with me as I am new to Web Development. Thanks in advance for any tips or pointers. Kris.
-
Hi, I have a DataGrid where in each row has couple of text boxes and an update button. Each row is dynamically generated as the number of rows are not known ahead of time. When the user clicks the update button, I do a postback to capture the data entered. However I dont want to do a postback when the textboxes are empty. How do I prevent this using Javascript? My main problem is with ClientIDs. This is a common problem and if it has been beaten to death, please bear with me as I am new to Web Development. Thanks in advance for any tips or pointers. Kris.
-
Hi Kris, You can add a RequiredFieldValidator[^] to the column as you normally would with the textbox, and set the
ControlToValidate
property to theid
of the textbox, not theclientID
property, and the ASP.NET does the rest for you.The problem I have is that it is okay to have one of the textboxes to be empty while the other one is filled. It is invalid only if both the textboxes are emtpy. I should prevent this condition. I thought RequiredFieldValidator works with a single control. How can I use to check if both the controls are empty and only then flag the condition. Besides does this result in a Postback - if it does, then it defeats the purpose. I am trying to do client side validation. Thanks.
-
The problem I have is that it is okay to have one of the textboxes to be empty while the other one is filled. It is invalid only if both the textboxes are emtpy. I should prevent this condition. I thought RequiredFieldValidator works with a single control. How can I use to check if both the controls are empty and only then flag the condition. Besides does this result in a Postback - if it does, then it defeats the purpose. I am trying to do client side validation. Thanks.
Yes, the RequiredFieldValidator should only service a single control. IMHO, you can either go for a customvalidator as this validator does not require you to specify the ControlToValidate property and you can set the your client side function in the ClientValidationFunction property, or you can register your client side validation when the update button is clicked. The clientid of the textbox should be in the format
<datagrid_id>$_ctl<row_number>$_<control_id>
, if the id of the textbox is not specified, the datagrid will automatically add the suffix to the place of thecontrol_id
. If you are not familiar with the clientid value, you can also emit the clientid value of the textbox in the web page to a client side variable so that you can use the variable later in validation at the client side.