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

[Solved] custom validation, itemcreated event

2 Answers 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ivan
Top achievements
Rank 1
Ivan asked on 14 Mar 2013, 06:19 PM

In a older version of asp controls, I create a simple validation on the OnItemCreated event of radgrid and works great, but in the latest version of asp controls every time I click edit or create new record throws exception"Microsoft JScript: Sys.WebForms.PageRequestManagerServerErrorException: Editor cannot be initialized for column: [Name column]". I've the same code in several places in my website.

here is my code:

.cs
        protected void gridFases_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = e.Item as GridEditableItem;
                GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("Nombre");
                TableCell cell = (TableCell)editor.TextBoxControl.Parent;

                RequiredFieldValidator validator = new RequiredFieldValidator();
                editor.TextBoxControl.ID = "Nombre";
                validator.ControlToValidate = editor.TextBoxControl.ID;
                validator.ErrorMessage = "El nombre de la fase es obligatorio.";
                cell.Controls.Add(validator);

            }
        }

.aspx

<telerik:RadGrid ID="gridFases" runat="server"
                OnNeedDataSource="gridFases_NeedDataSource"
                OnInsertCommand="gridFases_InsertCommand" 
                OnUpdateCommand="gridFases_UpdateCommand" 
                OnItemCreated="gridFases_ItemCreated">
            <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="false" DataKeyNames="Id" InsertItemPageIndexAction="ShowItemOnCurrentPage" >
                <NoRecordsTemplate>
                    <div>
                        No hay fases asociadas
                    </div>
                </NoRecordsTemplate>
                <CommandItemSettings  AddNewRecordText="Nueva fase" RefreshText="Refrescar" ShowRefreshButton="false"  />
                <EditFormSettings>
                    <EditColumn EditText="Guardar" CancelText="Cancelar"  UpdateText="Actualizar" InsertText="Guardar"></EditColumn>
                </EditFormSettings>
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="OrdenEjecucion" SortOrder="Ascending" />
                </SortExpressions>
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton"  />
                    <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id" Visible="false" ReadOnly="true" />
                    <telerik:GridTemplateColumn DataField="OrdenEjecucion" HeaderText="Orden de ejecución" UniqueName="OrdenEjecucion">
                        <InsertItemTemplate>
                            <telerik:RadNumericTextBox ID="txtOrdenEjecucion" MinValue="1" MaxValue="100" runat="server" ShowSpinButtons="True" Value="1">
                                <NumberFormat AllowRounding="True" KeepNotRoundedValue="False" DecimalDigits="0"/>
                            </telerik:RadNumericTextBox>
                        </InsertItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadNumericTextBox ID="txtOrdenEjecucion" MinValue="1" MaxValue="100" runat="server" ShowSpinButtons="True" Value='<%#Convert.ToDecimal( Eval("OrdenEjecucion")) %>'>
                                <NumberFormat AllowRounding="True" KeepNotRoundedValue="False" DecimalDigits="0"/>
                            </telerik:RadNumericTextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <%# DataBinder.Eval(Container.DataItem, "OrdenEjecucion")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="Nombre" HeaderText="Nombre de la fase" UniqueName="Nombre" Visible="true"/>
                    <telerik:GridCheckBoxColumn DataField="Obligatorio" HeaderText="¿Fase obligatoria?" UniqueName="Obligatorio"  Visible="true"/>
                </Columns>
            </MasterTableView>
            <ValidationSettings EnableValidation="true" />
        </telerik:RadGrid>

2 Answers, 1 is accepted

Sort by
0
Keizo
Top achievements
Rank 1
answered on 19 Mar 2013, 07:23 AM
Mr. Ivan

Sorry for interrupting

Has been hit exactly the same error I also
Telerik, please be resolved soon.

Thank you.
0
Accepted
Maria Ilieva
Telerik team
answered on 19 Mar 2013, 01:05 PM
Hi,

To avoid the described exception you need to remove the line where the ID of the editor.TextBoxControl is set.

Into the latest version of the RadGrid our developers perform some changes in order to improve the performance of the columns and column editors. Into the latest version the  editor’s TextBoxControl has set ID by which on later stage of the page lifecycle the control is found. In your case when the ID is set explicitly the textbox’s ID is overridden and it cannot be found from the build in code.
Please excuse us for the temporary inconvenience.

Additionally into the latest version of the RadGrid you do not need to insert RequiredFieldValidator  manually, you can take advantage of build in validation functionality. It can be configured via ColumnValidationSettings property of each column. Live example and more information you can find here:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx
http://www.telerik.com/help/aspnet-ajax/grid-validation.html




Kind regards,
Maria Ilieva
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
Ivan
Top achievements
Rank 1
Answers by
Keizo
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or