Radgrid Insert Method Keep Editor Open on Validation Failure

2 posts, 0 answers
  1. Angelo
    Angelo avatar
    4 posts
    Member since:
    May 2016

    Posted 15 Aug 2018 Link to this post

    I have a Radgrid where I am doing some extra validation on the InsertMethod. If that validation fails, I need to make sure that the editor stays open so that the user can still perform editing on the form. 

     

    My radgrid is declared as follows:

    <telerik:RadGrid
                    ID="rgRuleExemptions"
                    runat="server"
                    AutoGenerateColumns="false"
                    AllowPaging="true"
                    AllowSorting="true"
                    ClientSettings-EnableRowHoverStyle="true"
                    EnableEmbeddedSkins="False"
                    HeaderStyle-Wrap="False"
                    RenderMode="Lightweight"
                    HeaderStyle-ForeColor="White"
                    PageSize="10"
                    OnItemDataBound="RadGridRuleExemptions_ItemDataBound">
     
                    <PagerStyle Mode="NextPrevAndNumeric" />
     
                    <MasterTableView
                        AutoGenerateColumns="false"
                        DataKeyNames="Id"
                        SelectMethod="GetRuleExemptions"
                        InsertMethod="AddRuleExemption"
                        UpdateMethod="UpdateRuleExemption"
                        ItemType="RuleExemptions"
                        CommandItemDisplay="Top">
     
                        <CommandItemSettings />
     
                        <CommandItemTemplate>
                            <div class="text-right" style="background-color: transparent; padding: 5px 5px 5px 5px;">
                                <asp:Button
                                    ID="btnAddRuleExemption"
                                    runat="server"
                                    CssClass="button button-teal button-medium"
                                    Text="Add New Record"
                                    CausesValidation="False"
                                    CommandName="InitInsert" />
                            </div>
                        </CommandItemTemplate>
     
                        <Columns>
                            <telerik:GridButtonColumn
                                UniqueName="EditItem"
                                CommandName="Edit"
                                ItemStyle-Width="40"
                                ItemStyle-HorizontalAlign="Center"
                                ItemStyle-ForeColor="White"
                                Text="Edit"
                                ButtonType="LinkButton"
                                ButtonCssClass="button button-teal button-small">
                                <HeaderStyle Font-Bold="true" />
                            </telerik:GridButtonColumn>
                            <telerik:GridTemplateColumn UniqueName="Id" HeaderText="Id">
                                <ItemTemplate>
                                    <asp:Label ID="lblId" runat="server" Text='<%# Item.Id %>'></asp:Label>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn UniqueName="Category" HeaderText="Category">
                                <ItemTemplate>
                                    <asp:Label ID="lblCategory" runat="server" Text='<%# Item.Category %>'></asp:Label>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn UniqueName="RuleName" HeaderText="Rule Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblRuleName" runat="server" Text='<%# Item.RuleName %>'></asp:Label>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn UniqueName="Version" HeaderText="Version">
                                <ItemTemplate>
                                    <asp:Label ID="lblVersion" runat="server" Text='<%# Item.Version %>'></asp:Label>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn UniqueName="NetworkId" HeaderText="Network Id">
                                <ItemTemplate>
                                    <asp:Label ID="lblNetworkId" runat="server" Text='<%# Item.NetworkId %>'></asp:Label>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn UniqueName="OperatorId" HeaderText="Operator Id">
                                <ItemTemplate>
                                    <asp:Label ID="lblOperatorId" runat="server" Text='<%# Item.OperatorId %>'></asp:Label>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn UniqueName="Justification" HeaderText="Justification">
                                <ItemTemplate>
                                    <asp:Label ID="lblJustification" runat="server" Text='<%# Item.Justification %>'></asp:Label>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                        </Columns>
     
                        <EditFormSettings EditFormType="Template">
                            <FormStyle />
     
                            <FormTemplate>
                                <div class="form-horizontal" style="margin-top: 15px;">
                                    <%--Category--%>
                                    <div class="form-group">
                                        <label for="radDropDownCategory" class="col-md-2 control-label">Category <em class="requiredIndicator">*</em></label>
                                        <div class="col-md-5">
                                            <telerik:RadDropDownList runat="server"
                                                ID="radDropDownCategory"
                                                DataTextField="TextDescription"
                                                DataValueField="Value"
                                                RenderMode="Lightweight"
                                                SelectMethod="GetCategories"
                                                SelectedValue='<%# BindItem.Category %>'>
                                            </telerik:RadDropDownList>
                                        </div>
                                    </div>
     
                                    <%--Rule Name--%>
                                    <div class="form-group">
                                        <label for="txtRuleName" class="col-md-2 control-label">Rule Name <em class="requiredIndicator">*</em></label>
                                        <div class="col-md-5">
                                            <asp:TextBox ID="txtRuleName" runat="server" ClientIDMode="Static" Text='<%# BindItem.RuleName %>'
                                                MaxLength="100"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="rfvRuleName" runat="server" ClientIDMode="Static" ControlToValidate="txtRuleName"
                                                ErrorMessage="Rule Name is Required" ValidationGroup="RuleExemptionGroup" CssClass="text-error" Display="Dynamic">
                                            </asp:RequiredFieldValidator>
                                        </div>
                                    </div>
     
                                    <%--Version--%>
                                    <div class="form-group">
                                        <label for="txtVersion" class="col-md-2 control-label">Version <em class="requiredIndicator">*</em></label>
                                        <div class="col-md-3">
                                            <asp:TextBox ID="txtVersion" runat="server" ClientIDMode="Static" Text='<%# BindItem.Version %>'
                                                MaxLength="32"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="rfvVersion" runat="server" ClientIDMode="Static" ControlToValidate="txtVersion"
                                                ErrorMessage="Version is Required" ValidationGroup="RuleExemptionGroup" CssClass="text-error" Display="Dynamic">
                                            </asp:RequiredFieldValidator>
                                        </div>
                                    </div>
     
                                    <%--Network Id--%>
                                    <div class="form-group">
                                        <label for="txtNetworkId" class="col-md-2 control-label">Network Id</label>
                                        <div class="col-md-3">
                                            <asp:TextBox ID="txtNetworkId" runat="server" ClientIDMode="Static" Text='<%# BindItem.NetworkId %>'
                                                MaxLength="32"></asp:TextBox>
                                        </div>
                                    </div>
     
                                    <%--Operator Id--%>
                                    <div class="form-group">
                                        <label for="txtOperatorId" class="col-md-2 control-label">Operator Id</label>
                                        <div class="col-md-3">
                                            <asp:TextBox ID="txtOperatorId" runat="server" ClientIDMode="Static" Text='<%# BindItem.OperatorId %>'
                                                MaxLength="32"></asp:TextBox>
                                        </div>
                                    </div>
     
                                    <%--Justification--%>
                                    <div class="form-group">
                                        <label for="txtJustification" class="col-md-2 control-label">Justification <em class="requiredIndicator">*</em></label>
                                        <div class="col-md-5">
                                            <asp:TextBox ID="txtJustification" runat="server" ClientIDMode="Static" Text='<%# BindItem.Justification %>'
                                                MaxLength="5000" TextMode="MultiLine" Rows="4"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="rfvJustification" runat="server" ClientIDMode="Static" ControlToValidate="txtJustification"
                                                ErrorMessage="Justification is Required" ValidationGroup="RuleExemptionGroup" CssClass="text-error" Display="Dynamic">
                                            </asp:RequiredFieldValidator>
                                        </div>
                                    </div>
                                </div>
     
                                <div class="form-horizontal" style="margin-top: 15px;">
                                    <%--Form Buttons--%>
                                    <div class="form-group">
                                        <label class="col-md-2 control-label"></label>
                                        <div class="col-md-6">
                                            <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="button button-blue btn-md" CommandName="PerformInsert" CausesValidation="true" Visible="false" ValidationGroup="RuleExemptionGroup" />
                                            <asp:Button ID="btnUpdate" runat="server" Text="Update" CssClass="button button-blue btn-md" CommandName="Update" CausesValidation="true" Visible="false" ValidationGroup="RuleExemptionGroup" />
                                            <asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="button button-orange btn-md" CommandName="Cancel" CausesValidation="false" />
                                        </div>
                                    </div>
                                </div>
                            </FormTemplate>
                        </EditFormSettings>
                    </MasterTableView>
                </telerik:RadGrid>

     

    Code behind:

    public void AddRuleExemption()
    {
                var item = new RuleExemptions();
                var context = new DomainContext();
                var domainObject = new RuleExemptions(context);
     
                TryUpdateModel(item);
                var add = domainObject.AddRuleExemption(item);
     
                if (!add.IsOperationSuccessful)
                {
                    pnlErrorMessage.Visible = true;
                    lblErrorMessage.Text = add.OperationReturnMessage;
                    // I WANT TO KEEP THE EDITOR OPEN HERE...
                }
                else
                {
                    lblErrorMessage.Text = "Operation Successful";
                }
            }

     

    How can I keep the editor open the add operation was not successful? (Note the line // I WANT TO KEEP THE EDITOR OPEN HERE... above).

    Please note that I am using Model Binding in this instance. 

    Thanks

     

     

     

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4656 posts

    Posted 20 Aug 2018 Link to this post

    Hello Angelo,

    Could you try checking for the validation of the model state before performing the actual insert operation? If you don't perform the operation and there is a model state validation error, it would be shown in a validation summary or ModelErrorMessage control that you can provide. Here is an example of this: https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-binding/model-binding-and-strongly-typed-data-controls-support#modelbinding-validation

    I am also attaching below a very simple example of how this works as expected (model-binding-validation-sample.zip). It does not have any actual CRUD operations logic, but it will suffice to show the validation approach.


    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top