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

ValidationGroups dont work within RadGrid

3 Answers 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 17 Aug 2012, 09:28 AM
I have been adding validation to a RadGrid, whenever i try to add any Validation groups it causes the RequiredFieldValidator to not work at all

Is there any work around for this? I would like to have a summary of errors listed

<telerik:RadGrid ID="RadGrid2" runat="server" Width="100%"
                    AllowAutomaticInserts="True"
                    AllowAutomaticUpdates="True"
                    AllowPaging="True"
                    AllowMultiRowEdit="false"
                    AllowMultiRowSelection="false"
                    AutoGenerateColumns="False"
                    DataSourceID="SqlDataSource2"
                    EnableLinqExpressions="false"
                    GridLines="None"
                    OnItemCreated="RadGrid2_ItemCreated"
                    OnItemDataBound="RadGrid2_ItemDataBound"
                    OnItemUpdated="RadGrid2_ItemUpdated"
                    OnItemInserted="RadGrid2_ItemInserted"
                    OnDataBound="RadGrid2_DataBound"
                    OnItemCommand="RadGrid2_ItemCommand"
                    OnPreRender="RadGrid2_PreRender">
                    <PagerStyle Mode="NextPrevAndNumeric" />
                    <MasterTableView Width="100%" CommandItemDisplay="Bottom" EditMode="InPlace"
                        DataSourceID="SqlDataSource2" HorizontalAlign="NotSet" AutoGenerateColumns="false">
                        <Columns>
                            <telerik:GridBoundColumn DataField="ProductTypeId" HeaderText="ProductTypeId" SortExpression="ProductTypeId" UniqueName="ProductTypeId" Visible="false">
                            </telerik:GridBoundColumn>
 
                            <telerik:GridBoundColumn DataField="ProductAttributeDefKeyOri" HeaderText="ProductAttributeDefKeyOri" SortExpression="ProductAttributeDefKeyOri" UniqueName="ProductAttributeDefKeyOri" Visible="false">
                            </telerik:GridBoundColumn>
                             
                            <telerik:GridDropDownColumn ListTextField="Name" ListValueField="ProductAttributeDefKey" DataSourceID="SqlDataSource3" HeaderText="Attribute" DataField="ProductAttributeDefKey" UniqueName="ProductAttributeDefKey" DropDownControlType="DropDownList" EnableEmptyListItem="true" EmptyListItemText="Select an attribute..." EmptyListItemValue="">
                            </telerik:GridDropDownColumn>
 
                            <telerik:GridTemplateColumn DefaultInsertValue="1" DataField="SortOrder" HeaderText="Sort Order" SortExpression="SortOrder" UniqueName="SortOrder"  ForceExtractValue="Always" >
                                <EditItemTemplate>
                                    <telerik:RadNumericTextBox runat="server" ID="tbSortOrder" DbValue='<%# Bind("SortOrder") %>' ShowSpinButtons="true" SpinDownCssClass="" SpinUpCssClass="" Width="50px" MinValue="1" MaxValue="999" NumberFormat-DecimalDigits="0" CssClass="textbox_increment">
                                    </telerik:RadNumericTextBox>
                                    <asp:RequiredFieldValidator runat="server" ControlToValidate="tbSortOrder" ErrorMessage="*" ValidationGroup="grid2"></asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <%# Eval("SortOrder")%>
                                </ItemTemplate>
                                <ItemStyle CssClass="MyImageButton" />
                            </telerik:GridTemplateColumn>
 
                            <telerik:GridCheckBoxColumn DataField="Enabled" HeaderText="Enabled" SortExpression="Enabled" UniqueName="Enabled" DefaultInsertValue="Checked"  >
                            </telerik:GridCheckBoxColumn>
 
                            <telerik:GridCheckBoxColumn DataField="Required" HeaderText="Required" SortExpression="Required" UniqueName="Required" DefaultInsertValue="1">
                            </telerik:GridCheckBoxColumn>
 
                            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                                <ItemStyle CssClass="MyImageButton" />
                            </telerik:GridEditCommandColumn>
                        </Columns>
                        <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit Attribute" InsertCaption="Add Attribute">
                            <FormTableItemStyle Wrap="true"></FormTableItemStyle>
                            <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
                            <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="0" BackColor="White" Width="100%" />
                            <FormTableStyle CellSpacing="0" CellPadding="1" BackColor="White" />
                            <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
                            <PopUpSettings Modal="true" />
                            <EditColumn ButtonType="ImageButton" InsertText="Add Attribute" UpdateText="Update Attribute" UniqueName="EditCommandColumn1" CancelText="Cancel Edit"></EditColumn>
                            <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
                        </EditFormSettings>
                        <CommandItemSettings AddNewRecordText="Add Attribute" />
                    </MasterTableView>
                </telerik:RadGrid>

protected void RadGrid2_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //attribute validation
                GridEditableItem item = e.Item as GridEditableItem;
                DropDownList dropDownList1 = item["ProductAttributeDefKey"].Controls[0] as DropDownList;
                GridDropDownColumnEditor editor = (GridDropDownColumnEditor)item.EditManager.GetColumnEditor("ProductAttributeDefKey");
                TableCell cell = (TableCell)editor.ContainerControl;
 
                RequiredFieldValidator validator = new RequiredFieldValidator();
                validator.ControlToValidate = dropDownList1.ID;
                validator.ErrorMessage = "*";
                validator.ValidationGroup = "grid2";
                cell.Controls.Add(validator);
            }
             
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                if (e.Item.OwnerTableView.IsItemInserted)
                {
                    //set required and enabled default values for inserting
                    GridEditableItem edititem = (GridEditableItem)e.Item;
                    CheckBox chkbx = (CheckBox)edititem["Enabled"].Controls[0];
                    chkbx.Checked = true;
                    chkbx = (CheckBox)edititem["Required"].Controls[0];
                    chkbx.Checked = true;
                }
            }
        }

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Aug 2012, 05:35 PM
Hello,

Please also set for validationGroup for Insert/Update Button.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode && e.Item is GridEditableItem)
            {
                if (e.Item is GridDataInsertItem)
                {
                    GridEditableItem editItem = (GridEditableItem)e.Item;
                    LinkButton InsertButton = (LinkButton)editItem.FindControl("PerformInsertButton");
                    InsertButton.ValidationGroup = "YourValidationGroup";
                }
                else
                {
                    GridEditableItem editItem = (GridEditableItem)e.Item;
                    LinkButton updateButton = (LinkButton)editItem.FindControl("UpdateButton");
                    updateButton.ValidationGroup = "YourValidationGroup";
                }
            }
        }


Thanks,
Jayesh Goyani
0
Craig
Top achievements
Rank 1
answered on 20 Aug 2012, 08:47 AM
Grid still refuses to enter edit/insert mode if i enter the above code or add any validation groups within the grid
0
Marin
Telerik team
answered on 22 Aug 2012, 07:51 AM
Hello,

 You should also add validation settings to the grid specifying which commands you want to validate:

<ValidationSettings CommandsToValidate="PerformInsert,Update" />
as shown in this demo

Regards,
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.
Tags
Grid
Asked by
Craig
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Craig
Top achievements
Rank 1
Marin
Telerik team
Share this question
or