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

[Solved] Client-Side asp:CustomValidator - e-mail required if box checked - in an EditTemplate

1 Answer 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 16 Apr 2009, 09:20 PM
Hi All,

Been a little while but I'm doing UI work again, and fixing some bugs.  I am up against a wall on this one.  Essentially, I need to have a dual-validation - if a checkbox is checked <t:GridCheckBoxColumn...> then I need to perform both a regexp validator for e-mail address format and a custom validator to make sure it is not blank if the checkbox is checked.  This occurs in a <t:GridTemplateColumn><EditItemTemplate>

I would first of all like to know if I can get the actual row being edited out of the arguments passed to the validator - it seems not.  If that is not possible, an index to the row being edited will do - where can I get that?  Maybe there is a way to do a FindControl() on a row? Finally, I am willing to iterate all rows if I must and ask "are you being edited?" (assuming that I can.)

I can get the *old* values of the columns using getDataKey(), in my validator JavaScript, but I don't know how to get the new value of the checkbox.checked or the TextBox.value.

Here is the markup for the columns of interest:

<telerik:GridCheckBoxColumn DataField="SendNotification"  
  DataType="System.Boolean"  
  meta:resourcekey="GridCheckBoxColumnResource1"  
  SortExpression="SendNotification" 
  UniqueName="SendNotification"
</telerik:GridCheckBoxColumn> 
<telerik:GridTemplateColumn DataField="NotificationAddress"  
  meta:resourcekey="GridTemplateColumnResource11" 
  SortExpression="NotificationAddress" UniqueName="NotificationAddress"
  <EditItemTemplate> 
    <asp:TextBox ID="NotificationAddressTextBox" runat="server"  
      CausesValidation="True"  
      meta:resourcekey="NotificationAddressTextBoxResource1"  
      Text='<%# Bind("NotificationAddress") %>' MaxLength="1024"
    </asp:TextBox> 
    <asp:RegularExpressionValidator ID="NotificationAddressTextBoxValidator" 
      runat="server" ControlToValidate="NotificationAddressTextBox"  
      CssClass="ValidationError" Display="Dynamic" 
      ErrorMessage="Invalid e-mail address"  
      meta:resourcekey="NotificationAddressTextBoxValidatorResource1" 
      ValidationExpression="..."
    </asp:RegularExpressionValidator> 
    <asp:CustomValidator runat="server" ID="SendNotificationToValidator" 
      EnableClientScript="true" 
      ControlToValidate="NotificationAddressTextBox" 
      ClientValidationFunction="CheckEmailRequired"  
      Display="Dynamic" ErrorMessage="e-mail address required." 
      ValidateEmptyText="true" />                                                 
  </EditItemTemplate> 
  <ItemTemplate> 
    <%# Eval("NotificationAddress") %>&nbsp; 
  </ItemTemplate> 
</telerik:GridTemplateColumn> 
 

And the related JavaScript function:

function CheckEmailRequired(source, args) { 
  var grid = $find("<%=AlertConfigurationRadGrid.ClientID %>"); 
  var MasterTable = grid.get_masterTableView(); 
 
  // Can I get the row or item from source or args? 
 
  var selectedRows = MasterTable.get_dataItems(); 
 
  // Can I ask each row if it is being edited? 
 
  for (var i = 0; i < selectedRows.length; i++) { 
    var row = selectedRows[i]; 
    var oldSendNotification = row.getDataKeyValue("SendNotification"); 
    var oldNotificationAddress = row.getDataKeyValue("NotificationAddress"); 
  } 
  args.IsValid = true
  return; 
 


Thanks for any ideas you can offer!

Tim

1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 22 Apr 2009, 07:26 AM
Hello Tim,

To get the indexes of the rows being edited, you can use the _editIndexes collection of the grid on the client.

To find a particular control of the edit form in the client functioin of the validator (taking into consideration that the validator itself is part of the edit form), you should get the container element of the validator and search for the control in it:

var notificationBox = $telerik.findElement(source.parentElement,"NotificationAddressTextBox"

I hope this information helps.

Best Regards,

Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or