This is a migrated thread and some comments may be shown as answers.

[Solved] Validation RadGrid Template Fields

2 Answers 199 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SA water
Top achievements
Rank 1
SA water asked on 07 Apr 2009, 06:02 AM
Hello,
I have a number of template fields in a RadGrid which I wish to validate.
I've managed most validation using the Client side "RequiredFieldValidator" and server side validation using the CustomValidator.
I now want to validate one control based on the value of another control in the template.

eg
In the template field I have a Radiobuttonlist with 3 options (ie "Yes", "No", "NA"). If the user selects "No" then they have to enter text in a Textbox or the update should not proceed. If the answer is Yes they dont need to complete the Textbox.

Previously I achieved this in Gridview through something like the following. Its clunky but it works. It checks the Update data when the user tries to update. If it fails the validation they get a message and the update is cancelled.
    Private Sub GridView2_RowUpdating(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating  
        Dim InCompliance As String = e.NewValues("Incompliance")  ' this is the value of the selected Radiobutton item
        ' test if there are any comments
        If e.NewValues("Comments"Is System.DBNull.Value Or e.NewValues("Comments") = "" Then 'no comments in textbox
            If ((Trim(InCompliance) <> "Yes")) Then ' User has selected No or NA from Radiobuttonlist
                MessageBox("You must enter a comment if you have entered a No or N/A answer")  
                e.Cancel = True 'Cancel update
            End If 
        End If 
 
    End Sub 
How do I achieve the same in the Radgrid?

Cheers
Robert

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 07 Apr 2009, 08:32 AM
Hello Robert,

You can try out the following code using RadGrid in its UpdateCommand event to achieve the same scenario:
VB:
Protected Sub RadGrid1_UpdateCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
    Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem) 
    Dim strtxt As String = DirectCast(editItem.FindControl("RadioButtonList1"), RadioButtonList).SelectedItem.Text 'the value of the selected Radiobutton item 
 
    If DirectCast(editItem.FindControl("TextBox1"), System.Web.UI.WebControls.TextBox).Text = "" Then ' to check if no comments in textbox 
 
        If strtxt <> "YES" Then ' User has selected No or NA from Radiobuttonlist 
            
          MessageBox.Show("You must enter a comment if you have entered a No or N/A answer")                 
          e.Canceled = True 
        End If 
    End If 
End Sub  

Thanks
Princy.
0
SA water
Top achievements
Rank 1
answered on 09 Apr 2009, 02:01 AM
Thanks a lot Princy.
With a few minor changes it  worked perfectly.
The GrideditableItem object was the bit in the logic that I was missing.

    Protected Sub RadGrid1_UpdateCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand  
        Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
        Dim strtxt As String = DirectCast(editItem.FindControl("RadioButtonList1"), RadioButtonList).SelectedItem.Text 'the value of the selected Radiobutton item    
        If DirectCast(editItem.FindControl("TextBox1"), System.Web.UI.WebControls.TextBox).Text = "" Then ' to check if no comments in textbox    
            If strtxt <> "YES" Then ' User has selected No or NA from Radiobuttonlist    
                'MessageBox("You must enter a comment if you have entered a No or N/A answer")  
                RadAjaxManager1.Alert("You must enter a comment if you have entered a No or N/A answer")  
                e.Canceled = True 
            End If 
        End If 
    End Sub 
This forum and the people in it, such as yourself, are excellent value.
Once I build up my technical skills, which are only a few weeks old at the moment, I hope I can contribute as well.

Cheers
Robert
Tags
Grid
Asked by
SA water
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
SA water
Top achievements
Rank 1
Share this question
or