Cascading RadComboBox inside RadGrid with editformtype="Template"

2 posts, 0 answers
  1. Melvin
    Melvin avatar
    14 posts
    Member since:
    Apr 2015

    Posted 06 Jun 2015 Link to this post

    Hello,

     I have a problem with Radcombox that is inside form template, I need that the first radcombo 

     

    This is the code aspx 

    <telerik:RadGrid runat="server" Id="rgDirecciones" AutoGenerateColumns="False"
                                        OnDeleteCommand="rgDirecciones_DeleteCommand"
                                        OnInsertCommand="rgDirecciones_InsertCommand"
                                        OnItemDataBound="rgDirecciones_ItemDataBound"
                                        OnUpdateCommand="rgDirecciones_UpdateCommand" CellSpacing="-1" GroupPanelPosition="Top" >
                                        <mastertableview datakeynames="IdDireccion,IdSucursal,IdEmpresa">
                                            <rowindicatorcolumn visible="False">
                                            </rowindicatorcolumn>
                                            <expandcollapsecolumn created="True">
                                            </expandcollapsecolumn>
                                            <Columns>
                                                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="ImageButton"
                                                    EditImageUrl="../imagenes/Edit.png" InsertImageUrl="../imagenes/Edit.png" Exportable="False">
                                                    <HeaderStyle Width="30px" />
                                                </telerik:GridEditCommandColumn>                           
                                                <telerik:GridBoundColumn DataField="IdDireccion" UniqueName="IdDireccion" HeaderText="No Dirección">
                                                    <HeaderStyle Width="80px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="TipoDireccion" UniqueName="TipoDireccion" HeaderText="Tipo">
                                                    <HeaderStyle Width="80px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="Direccion" UniqueName="Direccion" HeaderText="Dirección">
                                                    <HeaderStyle Width="200px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="Referencia" UniqueName="Referencia" HeaderText="Referencia">
                                                    <HeaderStyle Width="200px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="Organizacion" UniqueName="Organizacion" HeaderText="Organización">
                                                    <HeaderStyle Width="200px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="NombreContacto" UniqueName="NombreContacto" HeaderText="Nombre de Contacto">
                                                    <HeaderStyle Width="200px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="CodigoPostal" UniqueName="CodigoPostal" HeaderText="Código Postal">
                                                    <HeaderStyle Width="130px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="RowVersion" UniqueName="RowVersion" Display="False">
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="IdSucursal" UniqueName="IdSucursal" HeaderText="Sucursal" Display="False">
                                                    <HeaderStyle Width="150px" />
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="Pais" UniqueName="Pais" Display="False">
                                                </telerik:GridBoundColumn>
                                                <telerik:GridBoundColumn DataField="DivisionTerritorial" UniqueName="DivisionTerritorial" Display="False">
                                                </telerik:GridBoundColumn>
                                                <telerik:GridButtonColumn ConfirmText="Desea eliminar este registro?" ConfirmDialogType="RadWindow" ConfirmTitle="Eliminar" ButtonType="ImageButton" CommandName="Delete" ImageUrl="../imagenes/Delete.png" UniqueName="DeleteColumn" Text="Eliminar" FilterControlAltText="Filter DeleteColumn column"  Exportable="False">
                                                    <HeaderStyle Width="30px" />
                                                </telerik:GridButtonColumn>
                                            </Columns>
                                            <editformsettings editformtype="Template">
                                                <editcolumn filtercontrolalttext="Filter EditCommandColumn1 column" uniquename="EditCommandColumn1">
                                                </editcolumn>
                                                <formtemplate>
                                                    <table class="table table-condensed">
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbIdDireccion" runat="server" Text="No Dirección"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadNumericTextBox ID="txtIdDireccion"
                                                                    runat="server"
                                                                    Enabled="<%# (Container is GridEditFormInsertItem) ? true : false %>"
                                                                    MaxLength="2"
                                                                    MaxValue="99"
                                                                    MinValue="0"
                                                                    Text='<%# Bind("IdDireccion") %>'
                                                                    Width="80px">
                                                                    <numberformat decimaldigits="0" groupseparator="" />
                                                                </telerik:RadNumericTextBox>
                                                                <asp:RequiredFieldValidator ID="rfvIdDireccion" runat="server"
                                                                    ControlToValidate="txtIdDireccion" Display="Dynamic"
                                                                    ErrorMessage="Campo Obligatorio" ForeColor="Red">
                                                                </asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbPaisDire" runat="server" Text="País"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadComboBox ID="cmbPais" Runat="server" Width="350px" Height="150px"
                                                                    AutoPostBack="True" OnSelectedIndexChanged="combo_SelectedIndexChanged">
                                                                </telerik:RadComboBox>
                                                                <asp:RequiredFieldValidator ID="rfvcmbPais" runat="server" 
                                                                    Display="Dynamic" ForeColor="Red"
                                                                    ControlToValidate="cmbPais" ErrorMessage="Campo Obligatorio">
                                                                </asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbDivisionTerritorial" runat="server" Text="División Territorial"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadComboBox ID="cmbDivisionTerritorial" Runat="server" Width="350px">
                                                                </telerik:RadComboBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbDireccion" runat="server" Text="Direccion"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadTextBox ID="txtDireccion" Runat="server" MaxLength="200" Text='<%# Bind("Direccion") %>' TextMode="MultiLine" Width="350px">
                                                                    <ClientEvents OnValueChanging="removerMasDeUnEspacio" />
                                                                </telerik:RadTextBox>
                                                                <asp:RequiredFieldValidator ID="rfvtxtDireccion" runat="server" 
                                                                    Display="Dynamic" ForeColor="Red"
                                                                    ControlToValidate="txtDireccion" ErrorMessage="Campo Obligatorio">
                                                                </asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbNombreContacto" runat="server" Text="NombreContacto"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadTextBox ID="txtNombreContacto" Runat="server"
                                                                    Text='<%# Bind("NombreContacto") %>' MaxLength="100" Width="350px">
                                                                    <ClientEvents OnValueChanging="removerMasDeUnEspacio" />
                                                                </telerik:RadTextBox>
                                                                <asp:RequiredFieldValidator ID="rfvtxtNombreContacto" runat="server" 
                                                                    Display="Dynamic" ForeColor="Red"
                                                                    ControlToValidate="txtNombreContacto" ErrorMessage="Campo Obligatorio">
                                                                </asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbOrganizacion" runat="server" CssClass="opcionales" Text="Organización"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadTextBox ID="txtOrganizacion" Runat="server" Text='<%# Bind("Organizacion") %>'
                                                                    MaxLength="100" Width="350px">
                                                                    <ClientEvents OnValueChanging="removerMasDeUnEspacio" />
                                                                </telerik:RadTextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbReferencia" runat="server" CssClass="opcionales" Text="Referencia"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadTextBox ID="txtReferencia" Runat="server" Text='<%# Bind("Referencia") %>'
                                                                     MaxLength="100" Width="350px">
                                                                    <ClientEvents OnValueChanging="removerMasDeUnEspacio" />
                                                                </telerik:RadTextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="width: 150px">
                                                                <asp:Label ID="lbCodigoPostal" runat="server" CssClass="opcionales" Text="CodigoPostal"></asp:Label>
                                                            </td>
                                                            <td>
                                                                <telerik:RadTextBox ID="txtCodigoPostal" Runat="server" Text='<%# Bind("CodigoPostal") %>'
                                                                    MaxLength="10" Width="80px">
                                                                    <ClientEvents OnValueChanging="removerMasDeUnEspacio" />
                                                                </telerik:RadTextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td colspan="2">
                                                                <telerik:RadButton ID="RadButton1" runat="server" causesvalidation="true" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' Text='<%# (Container is GridEditFormInsertItem) ? "Agregar" : "Guardar" %>' validationgroup="Textbox">
                                                                    <icon primaryiconurl="../imagenes/save_16x16.png" />
                                                                </telerik:RadButton>
                                                                    
                                                                <telerik:RadButton ID="RadButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancelar">
                                                                    <icon primaryiconurl="../imagenes/cancel.png" />
                                                                </telerik:RadButton>
                                                                       
                                                                <asp:Label ID="Label2" runat="server" CssClass="opcionales" Text="* Opcionales"></asp:Label>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </formtemplate>
                                            </EditFormSettings>
                                            <CommandItemTemplate>
                                                <div class="col-xs-6">
                                                    <asp:LinkButton ID="lbAgregarDirecciones" runat="server" CommandName="InitInsert" Visible='<%# !rgDirecciones.MasterTableView.IsItemInserted %>'><img style="border:2px;vertical-align:middle;" alt="" src="../imagenes/AddRecord.png"/> Agregar</asp:LinkButton>  
                                                    <asp:LinkButton ID="lbRefrescarDirecciones" runat="server">
                                                        <img style="border:2px;vertical-align:middle;" alt="" src="../imagenes/Refresh.png"/> Refrescar
                                                    </asp:LinkButton>  
                                                </div>
                                                <uc1:opcionesimportaciongrid runat="server" id="OpcionesImportacionGrid3" Grid="rgDirecciones" WebForm='<%# Page.Request.RawUrl.Substring(Page.Request.RawUrl.LastIndexOf("/") + 1) %>'  NombreArchivo="Direcciones"/>
                                            </CommandItemTemplate>
                                        </mastertableview>
                                    </telerik:RadGrid>

    Code  

    protected void rgDirecciones_ItemDataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//editform
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    GridEditFormItem editItem = (GridEditFormItem)e.Item;
     
                    if (!(e.Item is IGridInsertItem))
                    {
                        RadComboBox comboTipoDirecciones = (RadComboBox)editItem.FindControl("cmbTipoDireccion");
                        comboTipoDirecciones.SelectedValue = editItem["TipoDireccion"].Text;
     
                        RadComboBox comboPais = (RadComboBox)editItem.FindControl("cmbPais");
                        comboPais.SelectedValue = editItem["Pais"].Text;
                    }
                }           
            }
     
    protected void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
            {
                GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
                GridEditFormItem item = (sender as RadComboBox).NamingContainer as GridEditFormItem;
     
                RadComboBox comboPaises = sender as RadComboBox;
                RadComboBox comboDivisionTerritorial = (RadComboBox)editedItem.FindControl("cmbDivisionTerritorial");
     
                var parametrosSQL = new string[] { null, e.Value };
                DataSet ds = Clases.MetodosGlobales.ObtenerInformacionObjeto("ADMMDivisionesTerritoriales_get", parametrosSQL);
                 
                comboDivisionTerritorial.Text = null;
                comboDivisionTerritorial.EmptyMessage = "- Seleccione un valor -";
                comboDivisionTerritorial.Filter = RadComboBoxFilter.StartsWith;
                comboDivisionTerritorial.DataSource = ds.Tables[0];
                comboDivisionTerritorial.DataTextField = "Descripcion";
                comboDivisionTerritorial.DataValueField = "Divisionterritorial";
                comboDivisionTerritorial.DataBind();
            }

    The problem is that when I choose value of first radcombo. I fill the second radcombo but all the page load again and I lost the value of first combo and the second combo does not show the information. All the form is blank as if I did nothing

     I do not understand what is the problem.

    Regards,

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 10 Jun 2015 Link to this post

    Hello Melvin,

    From the provided code snippets it is not clear how you are binding your RadGrid. Can you please ensure that you are not using Simple Data-Binding and that you are not rebinding the grid on each postback, because this will cause the data to be lost.

    If you are indeed using Simple Data-Binding, please switch to Advanced Data-Binding by handling the OnNeedDataSource event and see if the issue will be resolved. For your convenience, following is a simple example demonstrating that the selected values are preserved:
    <telerik:RadGrid runat="server" ID="rgDirecciones" OnNeedDataSource="rgDirecciones_NeedDataSource"
        OnItemDataBound="rgDirecciones_ItemDataBound" CellSpacing="-1" GroupPanelPosition="Top">
        <MasterTableView>
            <Columns>
                <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
            </Columns>
            <EditFormSettings EditFormType="Template">
                <EditColumn FilterControlAltText="Filter EditCommandColumn1 column" UniqueName="EditCommandColumn1">
                </EditColumn>
                <FormTemplate>
                    <telerik:RadComboBox ID="cmbPais" runat="server" Width="350px" Height="150px"
                        AutoPostBack="True" OnSelectedIndexChanged="cmbPais_SelectedIndexChanged">
                        <Items>
                            <telerik:RadComboBoxItem Text="item1" Value="1"/>
                            <telerik:RadComboBoxItem Text="item2" Value="2"/>
                            <telerik:RadComboBoxItem Text="item3" Value="3"/>
                        </Items>
                    </telerik:RadComboBox>
                    <telerik:RadComboBox ID="cmbDivisionTerritorial" runat="server" Width="350px">
                    </telerik:RadComboBox>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

    And the code-behind:
    protected void rgDirecciones_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("FirstName", typeof(string));
        table.Columns.Add("LastName", typeof(string));
        table.Columns.Add("NumericValue", typeof(int));
        table.Columns.Add("Date", typeof(DateTime));
        table.Columns.Add("BoolValue", typeof(Boolean));
        for (int i = 0; i < 3; i++)
        {
            table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
        }
     
        (sender as RadGrid).DataSource = table;
    }
     
    protected void cmbPais_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Value", typeof(string));
        for (int i = 0; i < 3; i++)
        {
            table.Rows.Add(i, "Value" + i);
        }
     
        GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
        GridEditFormItem item = (sender as RadComboBox).NamingContainer as GridEditFormItem;
     
        RadComboBox comboPaises = sender as RadComboBox;
        RadComboBox comboDivisionTerritorial = (RadComboBox)editedItem.FindControl("cmbDivisionTerritorial");
     
        comboDivisionTerritorial.DataSource = table;
        comboDivisionTerritorial.DataTextField = "Value";
        comboDivisionTerritorial.DataValueField = "ID";
        comboDivisionTerritorial.DataBind();
    }
     
    protected void rgDirecciones_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//editform
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            GridEditFormItem editItem = (GridEditFormItem)e.Item;
     
            if (!(e.Item is IGridInsertItem))
            {
                RadComboBox comboPais = (RadComboBox)editItem.FindControl("cmbPais");
                comboPais.SelectedValue = editItem["ID"].Text;
            }
        }    
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top