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

Setting CssClass in UpdateCommand of RADGrid

2 Answers 27 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jürg
Top achievements
Rank 1
Jürg asked on 10 Sep 2013, 01:23 PM
Hi

I have an ASP.NET page containing a RADGrid with an EditFormTemplate. In the UpdateCommand of the grid I do some validation stuff and I would like to assign a CssClass to all the fields of the EditFormTemplate containing invalid values.

The validation is working properly. However, I can't manage to assign the CssClass to the controls. My UpdateCommand reads as follows:
Protected Sub CompanyGrid_UpdateCommand(sender As Object, e As GridCommandEventArgs) Handles CompanyGrid.UpdateCommand
  Dim objEditedItem As GridEditableItem = CType(e.Item, GridEditableItem)
  Dim objTextBox As RadTextBox
 
  objTextBox = CType(objEditedItem.FindControl("txtCompanyname"), RadTextBox)
  If objTextBox.Text.Trim() = "" Then
    objTextBox.CssClass = "MandatoryField"
    e.Canceled = True
    Exit Sub
  End If
 
End Sub

The CSS file contains the following definition for the class MandatoryField:
.MandatoryField
{
    margin-left: 0em;
    border:1px solid #397D47;
    background-color:#F77A80;
    font-size: 1.0em;
}

After exiting the UpdateCommand function, the page is reloaded but the CssClass is not set to the fields in question. How can I achieve a visual marking of the fields with invalid values?

Kind regards

Jürg

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 10 Sep 2013, 01:41 PM
Hi Jurg,

Please try the following code snippet which works fine at my end.

VB:
Protected Sub RadGrid1_UpdateCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
    If TypeOf e.Item Is GridEditableItem Then
        Dim objEditedItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim objTextBox As RadTextBox = DirectCast(objEditedItem.FindControl("txtCompanyname"), RadTextBox)
        If String.IsNullOrEmpty(objTextBox.Text.Trim()) Then
            objTextBox.CssClass = "MandatoryField"
            e.Canceled = True
            Return
        End If
    End If
End Sub


CSS:
.MandatoryField
      {
          margin-left: 0em;
          border: 1px solid #397D47;
          background-color: #F77A80 !important;
          font-size: 1.0em;
      }

Thanks,
Princy
0
Jürg
Top achievements
Rank 1
answered on 10 Sep 2013, 02:20 PM
Hi Princy

Thank you for your support. Works like a charm now :-)

Kind regards

Jürg
Tags
Grid
Asked by
Jürg
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Jürg
Top achievements
Rank 1
Share this question
or