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

Validator on Edit vs. Insert

2 Answers 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marc Lee
Top achievements
Rank 2
Marc Lee asked on 19 Jun 2010, 01:23 AM
I currently have a RadGrid with an EditItemTemplate that contains, among other things, a checkbox, a RadTextBox and a RequiredFieldValidator.  If the checkbox is checked, then the RadTextBox is a required field.  If it is unchecked, then the RadTextBox can be empty.  This works perfectly when you edit a record.  When you insert a new record, however, the validator stops the form from submitting, but the message ("Email is a required field') does not appear.  Anyone have any suggestions?  My code is below:

RadGrid:
            <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" 
              AutoGenerateColumns="False" GridLines="None" AutoGenerateDeleteColumn="False" 
              AutoGenerateEditColumn="False" Width="100%" Skin="Office2007"
              <AlternatingItemStyle BackColor="#EAF2FF" /> 
              <MasterTableView CommandItemDisplay="Top" DataKeyNames="BranchID" OverrideDataSourceControlSorting="true"
                <RowIndicatorColumn> 
                  <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                  <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <CommandItemSettings AddNewRecordText="Add new Branch" /> 
                <Columns> 
                  <telerik:GridTemplateColumn DataField="NotifyFlag" HeaderText="Notify?" SortExpression="NotifyFlag" 
                    UniqueName="NotifyFlag" Visible="False"
                    <EditItemTemplate> 
                      <asp:CheckBox runat="server" ID="cbNotify" Checked='<%#convertBool(DataBinder.Eval(Container.DataItem, "Notifyflag"))%>' /> 
                    </EditItemTemplate> 
                    <ItemStyle HorizontalAlign="Left" /> 
                  </telerik:GridTemplateColumn> 
                  <telerik:GridTemplateColumn DataField="Email" HeaderText="Email" SortExpression="Email" 
                    UniqueName="Email" Visible="false"
                    <ItemTemplate> 
                      <%#DataBinder.Eval(Container.DataItem, "Email")%> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                      <telerik:RadTextBox runat="server" ID="txtEmail" Width="280px" Text='<%#DataBinder.Eval(Container.DataItem, "Email")%>' /> 
                      <asp:RequiredFieldValidator runat="server" ID="rfvEmail" ControlToValidate="txtEmail"  
                        Display="Dynamic" ErrorMessage="Email address required" Enabled="false"></asp:RequiredFieldValidator> 
                      <asp:RegularExpressionValidator runat="server" ID="revEmail" Display="Dynamic" ControlToValidate="txtEmail"  
                        ErrorMessage="Valid email required" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" /> 
                    </EditItemTemplate> 
                    <ItemStyle HorizontalAlign="Left" /> 
                  </telerik:GridTemplateColumn> 
                  <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Edit" Text="Edit" 
                    UniqueName="EditColumn"
                    <ItemStyle HorizontalAlign="Center" /> 
                  </telerik:GridButtonColumn> 
                  <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Delete" Text="Delete" 
                    ConfirmDialogType="RadWindow" ConfirmText="Are you sure you want to delete this Branch?" /> 
                </Columns> 
                <EditFormSettings ColumnNumber="1" CaptionFormatString="" FormTableItemStyle-HorizontalAlign="Left"  
          FormTableAlternatingItemStyle-HorizontalAlign="Left"
                  <EditColumn ButtonType="PushButton" CancelText=" Cancel" UpdateText=" Save" 
                    InsertText=" Save" UniqueName="EditCommandColumn1"
                  </EditColumn> 
                  <FormMainTableStyle CellPadding="3" CellSpacing="0" BackColor="White"></FormMainTableStyle> 
                  <FormCaptionStyle></FormCaptionStyle
                  <FormTableItemStyle Wrap="False"></FormTableItemStyle> 
                  <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                  <FormTableButtonRowStyle HorizontalAlign="Right"></FormTableButtonRowStyle> 
                </EditFormSettings> 
              </MasterTableView> 
            </telerik:RadGrid> 
 


Javascript:
  <script type="text/javascript"
 
    function emailChanged(obj) { 
      if (obj.checked != true) { 
        var hiddenBox = document.getElementById("hdnRFV"); 
        var theRFV = document.getElementById(hiddenBox.value); 
        alert("Here1: " + hiddenBox.value); 
        ValidatorEnable(theRFV, false); 
      } else { 
        var hiddenBox = document.getElementById("hdnRFV"); 
        var theRFV = document.getElementById(hiddenBox.value); 
        alert("Here1: " + hiddenBox.value); 
        ValidatorEnable(theRFV, true); 
      } 
    }  
 
  </script> 


The Code-Behind:
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound 
        ' sets the status of the validator based on the value of the NotifyFlag column 
        If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then 
            Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem) 
            Dim myCheckBox As CheckBox = DirectCast(edititem("NotifyFlag").Controls(1), CheckBox) 
            myCheckBox.Attributes.Add("onclick""emailChanged(this);"
 
            Dim cntrl As HiddenField = DirectCast(FindControl("hdnRFV"), HiddenField) 
            cntrl.Value = DirectCast(edititem("Email").Controls(3), RequiredFieldValidator).ClientID 
        End If 
    End Subhelp


Thanks in advance for any helpl!

Marc



2 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 24 Jun 2010, 08:27 AM
Hello Marc,

I suspect that the cause for your script not running when in insert mode, it that the logic ItemDataBound event is not executed for the GridInsertItem. Therefore you should modify the if expression by adding a check if the item is of type IGridInsertItem too.

Greetings,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Marc Lee
Top achievements
Rank 2
answered on 24 Jun 2010, 11:33 PM
It turns out the reason it was not working for the insert action was because the hidden field (hdnRFV) was not setup as an updated control for RadGrid1 in the RadAjaxManager, so the value of hdnRFV was not getting updated.  It was working for the edit action because the required field validator (rfvEmail) clientID was hard-coded in hdnRFV.Value.
Tags
Grid
Asked by
Marc Lee
Top achievements
Rank 2
Answers by
Rosen
Telerik team
Marc Lee
Top achievements
Rank 2
Share this question
or