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

Calling Custom Validator at the time of Updating or adding

2 Answers 228 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prateep
Top achievements
Rank 1
Prateep asked on 07 Dec 2012, 07:27 AM
Hi ,

I have a rad grid in which I want to do a custom validation at the time of adding or editing. But the custom validator server side code is not firing. Any kind of suggestion or help would be appreciated. Please find my code below.
<telerik:RadGrid ID="radgrdWell" OnItemCreated="RadgrdPropertySetup_ItemCreated" BorderColor="#009933" BorderWidth="1px"
                                    FilterItemStyle-BackColor="#F1F7EE" OnItemDataBound="PropertySetup_ItemDataBoundCommand"
                                    OnUpdateCommand="PropertySetup_UpdateGridRow" OnInsertCommand="PropertySetup_InsertGridRow"
                                    Skin="Hay" EnableEmbeddedSkins="false" runat="server" AllowPaging="True" AllowSorting="True"
                                    AllowFilteringByColumn="true" GridLines="None" ShowGroupPanel="false" AutoGenerateColumns="false"
                                    OnNeedDataSource="PropertySetup_Needdatasource">
                                    <GroupingSettings CaseSensitive="false" />
                                    <ClientSettings ClientMessages-DragToGroupOrReorder="DragToReorder" AllowColumnsReorder="True"
                                        EnablePostBackOnRowClick="true">
                                        <Selecting AllowRowSelect="True" />
                                        <Resizing AllowColumnResize="false" />
                                        <ClientEvents OnPopUpShowing="PopUpShowing" />
                                    </ClientSettings>
                                    <PagerStyle Mode="NextPrevNumericAndAdvanced" AlwaysVisible="True" ForeColor="#009900"
                                        Font-Names="verdana" Font-Size="8pt" />
                                    <MasterTableView EditMode="PopUp" CommandItemDisplay="Top" EnableHeaderContextMenu="true"
                                        CommandItemSettings-AddNewRecordText="Add">
                                        <CommandItemTemplate>
                                            <table width="100%" cellpadding="4px">
                                                <tr>
                                                    <td width="40%">
                                                        <asp:Button ID="lbtnAddNew" runat="server" Text="Add" CommandName="InitInsert" CssClass="buttonlarge"
                                                            CausesValidation="false" />
                                                        <asp:Button ID="lbtnRefresh" runat="server" Text="Refresh Data" CommandName="RebindGrid"
                                                            CssClass="buttonlarge" CausesValidation="false" />
                                                    </td>
                                                    <td align="left">
                                                        <%-- <asp:Label ID="lblHeading" runat="server" CssClass="SuccessLabel" Text="Studies" />--%>
                                                    </td>
                                                </tr>
                                            </table>
                                        </CommandItemTemplate>
                                        <EditFormSettings PopUpSettings-Modal="true"
                                            EditColumn-ButtonType="ImageButton" EditColumn-InsertText="Add" FormTableButtonRowStyle-HorizontalAlign="Right"
                                            EditColumn-ItemStyle-HorizontalAlign="Center" EditColumn-CancelImageUrl="../../wpresources/IFDB/Images/Cancel-60.png"
                                            EditColumn-InsertImageUrl="../../wpresources/IFDB/Images/Add-60.png" EditColumn-UpdateImageUrl="../../wpresources/IFDB/Images/Update-60.png">
                                            <FormMainTableStyle GridLines="Horizontal" Width="100%" CssClass="Spacing" HorizontalAlign="Center" />
                                            <FormTableStyle Width="100%" CssClass="Spacing" HorizontalAlign="Center" />
                                            <FormStyle Width="100%"></FormStyle>
                                            <FormCaptionStyle ForeColor="#ececd8" />
                                            <PopUpSettings Width="450px" />
                                        </EditFormSettings>
                                        <Columns>
                                            <telerik:GridEditCommandColumn UniqueName="EditColumn" ButtonType="ImageButton" UpdateText="Save"
                                                HeaderText="" EditImageUrl="~/wpresources/IFDB/Images/Edit.png" InsertImageUrl="~/wpresources/IFDB/Images/Update.gif"
                                                UpdateImageUrl="~/wpresources/IFDB/Images/Update.gif" CancelImageUrl="~/wpresources/IFDB/Images/Cancel.gif">
                                                <HeaderStyle Width="6%"></HeaderStyle>
                                                <ItemStyle VerticalAlign="Top" />
                                            </telerik:GridEditCommandColumn>
                                            <telerik:GridTemplateColumn UniqueName="Description" HeaderText="Description *" SortExpression="Description"
                                                DataField="Description" ItemStyle-Wrap="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblDescription" runat="server" Width="120" CssClass="gridlabel" DataFormatString="{0:d}"
                                                        Text='<%#Eval("Description") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtDescription" CssClass="NormalTextBox" runat="server" Width="120"
                                                        Text='<%# Eval("Description") %>'>
                                                    </asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="rfdDescription" runat="server" ControlToValidate="txtDescription"
                                                        ValidationGroup="vgPropertyValueSetup" Text="*" Display="Dynamic"></asp:RequiredFieldValidator>
                                                    <asp:Label ID="lblOrganisationid" runat="server" Text='<%#Eval("Organisationid") %>'
                                                        CssClass="gridlabel" Visible="false"></asp:Label>
                                                    <asp:Label ID="lblParentPropertyValueID" runat="server" Text='<%#Eval("FkOrganisationHierarchyID") %>'
                                                        CssClass="gridlabel" Visible="false"></asp:Label>
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="ParentPropertyValueName" UniqueName="Type"
                                                ItemStyle-Wrap="false" HeaderText="Parent Property Name *" SortExpression="ParentPropertyValueName" HeaderStyle-Wrap="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblType" CssClass="gridlabel" Text='<%# Eval("ParentPropertyValueName") %>'
                                                        runat="server">
                                                    </asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:DropDownList ID="drpdwnParentType" DataTextField="Name" DataValueField="pkOrganisationHierarchyValueID"
                                                        CssClass="NormalDDL" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="rfvParentType" ControlToValidate="drpdwnParentType"
                                                        ValidationGroup="vgPropertyValueSetup" Text="*" InitialValue="-1" runat="server"
                                                        Display="Dynamic" />
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="UniqueWellID" ItemStyle-Wrap="false" HeaderText="Unique Well Identifier *"
                                                SortExpression="UniqueWellID" HeaderStyle-Wrap="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblUWID" runat="server" CssClass="gridlabel" Text='<%# Eval("UniqueWellID") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtUWID" CssClass="NormalTextBox" runat="server" Width="120" Text='<%# Eval("UniqueWellID") %>'>
                                                    </asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="rfdUWID" runat="server" ControlToValidate="txtUWID"
                                                        ValidationGroup="vgPropertyValueSetup" Text="*" Display="Dynamic"></asp:RequiredFieldValidator>
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="Latitude" ItemStyle-Wrap="false" HeaderText="Latitude"
                                                SortExpression="Latitude">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblLatitude" runat="server" CssClass="gridlabel" Text='<%# Eval("Latitude") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtLatitude" CssClass="NormalTextBox" runat="server" Width="120"
                                                        Text='<%# Eval("Latitude") %>'>
                                                    </asp:TextBox
                                                    <asp:CustomValidator ID="cvLatitude" runat="server" ControlToValidate="txtLatitude" OnServerValidate="ServerValidate"
                                                    ErrorMessage="You Must Fill" ValidationGroup="vgPropertyValueSetup"></asp:CustomValidator>
                                                    <asp:Label ID="lblvalError" runat="server" CssClass="ErrorLabel"></asp:Label>                                                 
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="Longitude" ItemStyle-Wrap="false" HeaderText="Longitude *"
                                                SortExpression="Longitude" >
                                                <ItemTemplate>
                                                    <asp:Label ID="lblLongitude" runat="server" CssClass="gridlabel" Text='<%# Eval("Longitude") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtLongitude" CssClass="NormalTextBox" runat="server" Width="120"
                                                        Text='<%# Eval("Longitude") %>'>
                                                    </asp:TextBox>
                                                    <asp:CustomValidator ID="cvLongitude" runat="server" ControlToValidate="txtLongitude" OnServerValidate="ServerValidate"
                                                    ErrorMessage="You Must Fill" ValidationGroup="vgPropertyValueSetup"></asp:CustomValidator>                                                   
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="UTMZone" ItemStyle-Wrap="false" HeaderText="UTMZone"
                                                SortExpression="UTMZone" >
                                                <ItemTemplate>
                                                    <asp:Label ID="lblUTMZone" runat="server" CssClass="gridlabel" Text='<%# Eval("UTMZone") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtUTMZone" CssClass="NormalTextBox" runat="server" Width="120"
                                                        Text='<%# Eval("UTMZone") %>'>
                                                    </asp:TextBox>                                                   
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="UTMXCoordinate" ItemStyle-Wrap="false" HeaderText="UTM X coordinate (wellhead)"
                                                SortExpression="UTMXCoordinate" HeaderStyle-Wrap="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblUTMXCoordinate" runat="server" CssClass="gridlabel" Text='<%# Eval("UTMXCoordinate") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtUTMXCoordinate" CssClass="NormalTextBox" runat="server" Width="120"
                                                        Text='<%# Eval("UTMXCoordinate") %>'>
                                                    </asp:TextBox>
                                                    <asp:CustomValidator ID="cvUTMXCoordinate" runat="server" ControlToValidate="txtUTMXCoordinate" OnServerValidate="ServerValidate"
                                                    ErrorMessage="You Must Fill" ValidationGroup="vgPropertyValueSetup"></asp:CustomValidator>                                                   
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="UTMYCoordinate" ItemStyle-Wrap="false" HeaderText="UTM Y coordinate (wellhead)"
                                                SortExpression="UTMYCoordinate" HeaderStyle-Wrap="false" >
                                                <ItemTemplate>
                                                    <asp:Label ID="lblUTMYCoordinate" runat="server" CssClass="gridlabel" Text='<%# Eval("UTMYCoordinate") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtUTMYCoordinate" CssClass="NormalTextBox" runat="server" Width="120"
                                                        Text='<%# Eval("UTMYCoordinate") %>'>
                                                    </asp:TextBox>
                                                    <asp:CustomValidator ID="cvUTMYCoordinate" runat="server" ControlToValidate="txtUTMYCoordinate" OnServerValidate="ServerValidate"
                                                    ErrorMessage="You Must Fill" ValidationGroup="vgPropertyValueSetup"></asp:CustomValidator>
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>                                           
                                            <telerik:GridTemplateColumn DataField="LatLonUnits" ItemStyle-Wrap="false" HeaderText="LatLonUnits"
                                                SortExpression="LatLonUnits">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblLatLonUnits" runat="server" CssClass="gridlabel" Text='<%# Eval("LatLonsystem") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                     <asp:DropDownList ID="ddlLatLonUnits" DataTextField="LookUpType" DataValueField="LookupValue"
                                                        CssClass="NormalDDL" runat="server">                                                       
                                                    </asp:DropDownList>                                                   
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="EffectiveDate" UniqueName="EffectiveDate"
                                                ItemStyle-Wrap="false" HeaderText="Effective Date *" SortExpression="EffectiveDate">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblEffectiveDate" runat="server" Width="120" CssClass="gridlabel"
                                                        DataFormatString="{0:d}" Text='<%# Eval("EffectiveDate") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <telerik:RadDatePicker ID="radEffectiveDate" runat="server" Width="120" MinDate="1/1/0001"
                                                        MaxDate="12/31/9999" DbSelectedDate='<%# Bind("EffectiveDate") %>'>
                                                    </telerik:RadDatePicker>
                                                    <asp:RequiredFieldValidator ID="rfvEffectiveDate" ControlToValidate="radEffectiveDate"
                                                        ValidationGroup="vgPropertyValueSetup" Text="*" InitialValue="" runat="server"
                                                        Display="Dynamic" />
                                                    <asp:CompareValidator ID="CVEffectiveWithExpiry" ControlToCompare="radEffectiveUntil"
                                                        ValidationGroup="vgPropertyValueSetup" ControlToValidate="radEffectiveDate" Type="Date"
                                                        Operator="LessThanEqual" Display="None" ErrorMessage="Effective Date can not exceed Expiry Date"
                                                        runat="server"></asp:CompareValidator>
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="EffectiveUntil" UniqueName="EffectiveUntil"
                                                ItemStyle-Wrap="false" HeaderText="Effective Until *" SortExpression="EffectiveUntil">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblEffectiveUntil" runat="server" CssClass="gridlabel" Width="120"
                                                        DataFormatString="{0:d}" Text='<%# Eval("EffectiveUntil") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <telerik:RadDatePicker ID="radEffectiveUntil" Width="120" runat="server" MinDate="1/1/0001"
                                                        MaxDate="12/31/9999" DbSelectedDate='<%# Bind("EffectiveUntil") %>'>
                                                    </telerik:RadDatePicker>
                                                    <asp:RequiredFieldValidator ID="rfvEffectiveUntil" ControlToValidate="radEffectiveUntil"
                                                        ValidationGroup="vgPropertyValueSetup" Text="*" InitialValue="" runat="server"
                                                        Display="Dynamic" />
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn DataField="AddDate" UniqueName="AddDate" HeaderText="Add Date *"
                                                SortExpression="AddDate" ItemStyle-Wrap="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblAddDate" Width="120" runat="server" CssClass="gridlabel" DataFormatString="{0:d}"
                                                        Text='<%# Eval("AddDate") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <telerik:RadDatePicker Width="120" ID="radAddDate" runat="server" MinDate="1/1/0001"
                                                        MaxDate="12/31/9999" DbSelectedDate='<%# Bind("AddDate") %>'>
                                                    </telerik:RadDatePicker>
                                                    <asp:RequiredFieldValidator ID="rfvAddDate" ControlToValidate="radAddDate" Text="*"
                                                        ValidationGroup="vgPropertyValueSetup" InitialValue="" runat="server" Display="Dynamic" />
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn UniqueName="Status" HeaderText="Status *" DataField="StatusName"
                                                SortExpression="StatusName" ItemStyle-Wrap="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblStatus" CssClass="gridlabel" Text='<%# Eval("StatusName") %>' runat="server">
                                                    </asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:DropDownList ID="drpdwnStatus" DataSource='<%#FetchStatusValues() %>' DataTextField="CategoryName"
                                                        DataValueField="CategoryUID" CssClass="NormalDDL" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="rfvStatus" ControlToValidate="drpdwnStatus" Text="*"
                                                        ValidationGroup="vgPropertyValueSetup" InitialValue="-1" runat="server" Display="Dynamic" />
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                        </Columns>
                                    </MasterTableView></telerik:RadGrid>
Code for Custom validator.
protected void ServerValidate(object sender, ServerValidateEventArgs args)
        {
            CustomValidator custValidator = (CustomValidator)sender;
            GridEditableItem eventargs = (GridEditableItem)custValidator.NamingContainer;
            string Latitude = (eventargs.FindControl("txtLatitude") as TextBox).Text;
            string Longitude = (eventargs.FindControl("txtLongitude") as TextBox).Text;
            string UTMXCoordinate = (eventargs.FindControl("txtUTMXCoordinate") as TextBox).Text;
            string UTMYCoordinate = (eventargs.FindControl("txtUTMYCoordinate") as TextBox).Text;
 
            if ((string.IsNullOrEmpty(Latitude) || string.IsNullOrEmpty(Longitude)) && (string.IsNullOrEmpty(UTMXCoordinate) || string.IsNullOrEmpty(UTMYCoordinate)))
                args.IsValid = false;
            else if ((!string.IsNullOrEmpty(Latitude) && !string.IsNullOrEmpty(Longitude)) || (!string.IsNullOrEmpty(UTMXCoordinate) && !string.IsNullOrEmpty(UTMYCoordinate)))
                args.IsValid = true;
            else
                args.IsValid = true;
        }

2 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 12 Dec 2012, 08:38 AM
Hello,

 You should use the Validation settings in the MasterTableView to specify which commands should cause validation:

<ValidationSettings CommandsToValidate="PerformInsert,Update"></ValidationSettings>

Further information can be found in this help topic.

All the best,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Prateep
Top achievements
Rank 1
answered on 12 Dec 2012, 09:32 AM
Yeah.. This is what I was looking for. Was not able to find how to add causevalidation..
Thanks.. solved the problem.
Tags
Grid
Asked by
Prateep
Top achievements
Rank 1
Answers by
Marin
Telerik team
Prateep
Top achievements
Rank 1
Share this question
or