RadGrid dynamic fromtemplate couldnt retrieve data from control

8 posts, 2 answers
  1. Petrofac
    Petrofac avatar
    8 posts
    Member since:
    Nov 2008

    Posted 04 Sep 2011 Link to this post

        Hi,

    I am trying to create a add controls dynamically from the server side in Form template. I am able to create controls and display the controls in Formtemplate but i couldnt find controls in the server side to the values from controls.

    Add Controls code sample

     

     

     

    Protected Sub rdGridUDFRule_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rdGridUDFRule.ItemDataBound 
        
    If TypeOf e.Item Is Telerik.Web.UI.GridEditFormItem And e.Item.IsInEditMode Then 
        
        
        
    If e.Item.OwnerTableView.IsItemInserted Then 
        
        
        
    'item is about to be inserted 
        
        
        
    For Each editItem As Telerik.Web.UI.GridEditFormItem In rdGridUDFRule.MasterTableView.GetItems(Telerik.Web.UI.GridItemType.EditFormItem) 
        
    If (editItem.IsInEditMode) Then 
        
        
        
    Dim plcHolder As HtmlTableCell = DirectCast(editITem.FindControl("placeHolderRadGridFormTemplate"), HtmlTableCell) 
        
        
    Dim objDynamicTxt As New TextBox 
        
    objDynamicTxt.ID = "DynamicControl"  
    objDynamicTxt.MaxLength = intLength 
        
    plcHolder.Controls.Add(objDynamicTxt) 
        
        
    End If 
        
        
        
    Next 
        
        
        
    Else 
        
        
        
    'item is about to be edited 
        
        
        
    Dim editedItem As Telerik.Web.UI.GridEditableItem = TryCast(e.Item, Telerik.Web.UI.GridEditableItem) 
        
    End If 
        
        
        
    End If 
        
        
        
    End Sub 

     

     

    Retrieve Dynamic control values from server side sample code

     

    Protected Sub RadGrid1_UpdateCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) Handles rdGridUDFRule.UpdateCommand 
            If (e.CommandName = RadGrid.UpdateCommandName) Then 
    For Each editItem As Telerik.Web.UI.GridEditFormItem In rdGridUDFRule.MasterTableView.GetItems(Telerik.Web.UI.GridItemType.EditFormItem) 
                    If (editItem.IsInEditMode) Then 
                        Dim plcHolder As HtmlTableCell = DirectCast(editITem.FindControl("placeHolderRadGridFormTemplate"), HtmlTableCell) 
    Dim objDynamicTxt As TextBox = DirectCast(plcHolder.FindControl("DynamicControl"
                   '******************************* THE objDynamicTxt RETURN NOTHING *******************************
                    End If 
                Next 
            End If 
        End Sub


    The Couldnt find the controls, but it is available in the FormTemplate while trying to click on the add new record.

    Please let me know in which event the dynamic controls should be created

    Thanks
    Prabhu

  2. Answer
    Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 09 Sep 2011 Link to this post

    Hello Prabhu,

    You have to create the controls in the ItemCreated event and then (if needed) populate them with data on ItemDataBound.
    Distinguishing the major differences between ItemCreated and ItemDataBound events
    Note that you don't have to check whether the item is in edit mode in the UpdateCommand handler.

    Let me know if you need more information.

    Regards,
    Daniel
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. Petrofac
    Petrofac avatar
    8 posts
    Member since:
    Nov 2008

    Posted 14 Sep 2011 Link to this post

    Hello Daniel,

    Thanks a lot for reply. Actually all my RadGrid Columns are dynamic i have to create columns based on a page state information of the page. Please let me know the right place to create a dynamic columns in the RadGrid

    Issue Description: (Example Scenario)
    I have a Dropdown list which says how many columns to be created in the RadGrid on the page.
    But i couldnt get the number of columns to be created from the dropdownlist selected value in RadGrid ItemCreated event (It always shows the default value), because i believe the RadGrid ItemCreated event occurs before the page load event. Only of page load event i m getting the dropdownlist selected value.

    Workaround done:
    Currently i m able the selected dropdownlist value in the RagGrid ItemCreated event from the querystring. I m passing the selected value in the querystring, but i have some other issues (security, validation issues) on following this method.

    Please let me know the correct method to get the page state information and create the columns in RadGrid.
    Appreciate you quick response.

    Thanks
    Prabhu
  4. Answer
    Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 17 Sep 2011 Link to this post

    Hello Prabhu,

    You can find a sample code that shows how to implement this functionality in a scenario similar to yours:
    Changing the Grid Structure Dynamically on Postback

    I hope this helps.

    Best regards,
    Daniel
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal
  5. Lilith
    Lilith avatar
    1 posts
    Member since:
    Nov 2014

    Posted 01 Jul 2015 in reply to Daniel Link to this post

    I have the same problem, I'm getting dynamic text boxes in the <formtemplate>, but when I want to access the controls in the ItemCommand, the panel is empty .

     please i need an idea. Thankyou 

    <telerik:RadGrid runat="server" Id="grdConceptos"
                    skin="Outlook"  EnableEmbeddedSkins="False"  DataSourceID="SqlEquipos"
                AllowPaging="True" PageSize="20" AllowSorting="True" AutoGenerateColumns="False" Culture="es-MX" TableLayout="Fixed"
                ShowStatusBar="true" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
                AllowAutomaticUpdates="True"  Width="100%" EnableViewState="true"  >
                <MasterTableView  CommandItemDisplay="TopAndBottom" GridLines="None"
                InsertItemPageIndexAction="ShowItemOnCurrentPage"   DataSourceID="SqlEquipos"
                    DataKeyNames="IdEquipo" AllowFilteringByColumn="true" AllowSorting="True">
    <CommandItemTemplate>
    ......
    </CommandItemTemplate>
     
    <columns>
    ....
    </columns>
     
     <EditFormSettings EditFormType="Template"   >
                             <FormTemplate>
     
       <table style="width: 100%;">
                                 <tr>
                                 <td colspan="2" class="titulo2">
                                    <br /> Edición del equipo: 
                                    <asp:Label ID="lbIdEquipo"  Text='<%#  Bind("IdEquipo")  %>' runat="server" Width="70px"/>
                                       
                                    <br /><br />
                                 </td>
                                 </tr>
                                  
                                     <tr>
                                         <td class="tipografiaBOLD">
                                           <span style="color:Red"> *</span> Cliente:
                                         </td>
                                         <td>
                                             <telerik:RadComboBox ID="ddlCliente" DataSourceID="SqlCliente" DataTextField="Nombre"
                                            DataValueField="IdCliente" Height="300px" AppendDataBoundItems="true"
                                            AllowCustomText="True" Filter="Contains"
                                            SelectedValue='<%# Bind("IdCliente") %>'
                                            runat="server" ShowMoreResultsBox="true" EnableLoadOnDemand="true" EnableVirtualScrolling="true"
                                            Width="350px">
                                            <Items>
                                                <telerik:RadComboBoxItem Text="Seleccionar..." value=""/>
                                            </Items>
                                        </telerik:RadComboBox>
                                         </td>
                                          
                                     </tr>
                                     <tr>
                                         <td class="tipografiaBOLD">
                                          <span style="color:Red"> *</span>  Unidad:
                                         </td>
                                         <td>
                                             <telerik:RadComboBox ID="ddlUnidad" DataSourceID="SqlUnidad" DataTextField="Unidad"
                                            DataValueField="IdUnidad" Height="300px" AppendDataBoundItems="true"
                                            AllowCustomText="True" Filter="Contains"
                                            SelectedValue='<%# Bind("IdUnidad") %>'
                                            runat="server" ShowMoreResultsBox="true" EnableLoadOnDemand="true" EnableVirtualScrolling="true"
                                            Width="100px">
                                            <Items>
                                                <telerik:RadComboBoxItem Text="Seleccionar..." value=""/>
                                            </Items>
                                        </telerik:RadComboBox>
                                         </td>
                                         
                                     </tr>
                                     <tr>
                                         <td class="tipografiaBOLD">
                                          <span style="color:Red"> *</span>  Marca:
                                         </td>
                                         <td>
                                             <telerik:RadComboBox ID="ddlMarca" DataSourceID="SqlMarca" DataTextField="Marca"
                                            DataValueField="IdMarca" Height="300px" AppendDataBoundItems="true"
                                            AllowCustomText="True" Filter="Contains"
                                            SelectedValue='<%# Bind("IdMarca") %>'
                                            runat="server" ShowMoreResultsBox="true" EnableLoadOnDemand="true" EnableVirtualScrolling="true"
                                            Width="250px">
                                            <Items>
                                                <telerik:RadComboBoxItem Text="Seleccionar..." value=""/>
                                            </Items>
                                        </telerik:RadComboBox>
                                         </td>
                                         
                                     </tr>
                                     <tr>
                                         <td class="tipografiaBOLD">
                                          <span style="color:Red"> *</span>  Tipo de Equipo:
                                         </td>
                                         <td>
                                             <telerik:RadComboBox ID="ddlTipoEquipo" DataSourceID="SqlTipoEquipo" DataTextField="TipoEquipo"
                                            DataValueField="IdTipoEquipo" Height="300px" AppendDataBoundItems="true"
                                            AllowCustomText="True" Filter="Contains"
                                            SelectedValue='<%# Bind("IdTipoEquipo") %>'
                                            runat="server" ShowMoreResultsBox="true" EnableLoadOnDemand="true" EnableVirtualScrolling="true"
                                            Width="150px">
                                            <Items>
                                                <telerik:RadComboBoxItem Text="Seleccionar..." value=""/>
                                            </Items>
                                        </telerik:RadComboBox>
                                         </td>
                                         
                                     </tr>
                                     <tr>
                                         <td colspan="2">
                                              
                                         </td>
                                        
                                     </tr>
                                 </table>
     
     
      <asp:Panel ID="ctrlDynamic" runat = "server"></asp:Panel>
     
       
                                       <asp:Button ID="btnActualizar" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insertar", "Actualizar") %>'
                                            runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'></asp:Button> 
                                        <asp:Button ID="btnCancelar" Text="Cancelar" runat="server" CausesValidation="False"
                                            CommandName="Cancel"></asp:Button>
                                            <br /><br />             
                             </FormTemplate>
                        </EditFormSettings>
                    </MasterTableView>
     
    </telerik:Radgrid>

     

    Protected Sub grdConceptos_ItemDataBound(ByVal source As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdConceptos.ItemDataBound
     
           If (TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode) Then
               Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
               IdTipoEquipo = DirectCast(editItem.FindControl("ddlTipoEquipo"), RadComboBox).SelectedValue
               IdEquipo = DirectCast(editItem.FindControl("lbIdEquipo"), Label).Text.Trim()
               ctrlDynamic = DirectCast(editItem.FindControl("ctrlDynamic"), Panel)
     
     
               Dim colEq As oColumna = New oColumna()
               Dim listaCol As List(Of oColumna) = New List(Of oColumna)
               Dim datos As List(Of String) = New List(Of String)
               listaCol = colEq.ObtieneColumnasEquipoValor(IdEquipo, sesion.IdLaboratorio)
               Dim tbl As Table = New Table()
               For i As Integer = 0 To listaCol.Count - 1
     
                   Dim lbl As New Label()
                   lbl.ID = "lbl" + listaCol(i).Nombre
                   lbl.Text = listaCol(i).Nombre
     
                   Dim tcLabel As TableCell = New TableCell()
                   tcLabel.Controls.Add(lbl)
                   'ctrlDynamic.Controls.Add(lbl)
     
                   Dim txt As New TextBox()
                   txt.Attributes.Add("style", "margin-bottom: 10px")
                   txt.ID = "txt" + listaCol(i).Nombre
                   txt.Text = listaCol(i).Valor
                   Dim tcTxt As TableCell = New TableCell()
                   tcTxt.Controls.Add(txt)
     
                   Dim tr As TableRow = New TableRow()
     
                   tr.Cells.Add(tcLabel)
                   tr.Cells.Add(tcTxt)
                   tbl.Rows.Add(tr)
     
                   ctrlDynamic.Controls.Add(tbl)
     
     
     
               Next
     
     
     
           End If
     
       End Sub

     

     

    Protected Sub grdConceptos_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdConceptos.ItemCommand
     
            
           If e.CommandName = "Update" Then
     
     
               If (TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode) Then
                   Dim editItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
                   Dim c As oColumna = New oColumna()
                   Dim idCliente As RadComboBox = DirectCast(editItem.FindControl("ddlCliente"), RadComboBox)
                   Dim idunidad As RadComboBox = DirectCast(editItem.FindControl("ddlUnidad"), RadComboBox)
                   Dim IdMarca As RadComboBox = DirectCast(editItem.FindControl("ddlMarca"), RadComboBox)
                   Dim IdTipoEquipo As RadComboBox = DirectCast(editItem.FindControl("ddlTipoEquipo"), RadComboBox)
                   c.UpdateEquipo(IdEquipo, idCliente.SelectedValue, idunidad.SelectedValue, IdMarca.SelectedValue, IdTipoEquipo.SelectedValue)
     
                   Dim colEq As oColumna = New oColumna()
                   Dim listaCol As List(Of oColumna) = New List(Of oColumna)
                   listaCol = colEq.ObtieneColumnasEquipo(IdTipoEquipo.SelectedValue, sesion.IdLaboratorio)
                   ctrlDynamic = DirectCast(editItem.FindControl("ctrlDynamic"), Panel)
                   Dim txt1 As TextBox = New TextBox()
                   txt1 = DirectCast(ctrlDynamic.FindControl("txtMVA"), TextBox)
     
                   For i As Integer = 0 To listaCol.Count - 1
                       Dim txt As TextBox = New TextBox()
                       txt = DirectCast(editItem.FindControl("txt" + listaCol(i).Nombre), TextBox)
                       c.UpdateEquipoColumna(IdEquipo, listaCol(i).IdColumna, txt.Text)
                   Next
     
                 
            
     
               End If
     
         
           End If
     
     
     
       End Sub

     

     

     

     

     

  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2447 posts

    Posted 06 Jul 2015 Link to this post

    Hi Lilith,

    Since you are creating the controls in the server-side OnItemDataBound event, those controls will not be persisted after the next postback, because that event is too late in the page's life cycle. If you need to the controls that you are adding dynamically to the Panel to persist you will have to add them within an earlier event - OnItemCreated.

    I understand that the values that you need will not be present within the OnItemCreated event, but you could create all of the controls within that event and within the OnItemDataBound event, hide those that will not be used.

    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
  7. Gajanan
    Gajanan  avatar
    49 posts
    Member since:
    May 2010

    Posted 17 Aug 2015 in reply to Konstantin Dikov Link to this post

    Dear all,
    My requirement is Bind the grid dynamic , and add the controls like Text Box And rad Como box at run time by Item value.
    so far have done the control adding and now my grid is ready to take values from user , 
    but when user selected value from rad combo box (dynamic added control in grid), and click the save button i cannot find the Rad Combo box 
    pls suggest me the way how to get the values for dynamically added controls in Rad-grid on button click.
    below are the steps i followed
    1. Added Grid in Aspx. with Autogenerated Column = true
    2. On Item created event added dynamic controls in Grid. 
    3. bind the data to ad combo box (dynamic added control in grid) 
    4. on save Button i have to save the selected values from rad combo box (dynamic added control in grid), to data base
    in above step 1 to 3 are done , but step 4 making me trouble .

    below is code for button click.
    protected void btnSaveAll_Click(object sender, EventArgs e)
    {
        Questionnaire MyQue = Questionnaire.getQuestionnaire(mQuestionnaireid); 
        ArrayList ColumnLst = GetColumnList();
                    Dictionary<string,string> RowSplitValue = new Dictionary<string,string>();
                    foreach (GridEditableItem Item in rgPivotQuestionnaireResult.MasterTableView.Items)
                    {
                        foreach (GridColumn Column in rgPivotQuestionnaireResult.MasterTableView.RenderColumns)
                        {
                            if (ColumnLst.Contains(Column.UniqueName))
                            {
                                //RowSplitValue = new Dictionary<string, string>(Item.SavedOldValues[Column.UniqueName]);
                                if (Item[Column].Controls.Count > 0)
                                {
                                    Control ItemControle= new Control();
                                    ItemControle = Item[Column].Controls[0];
                                    if (ItemControle is TextBox)
                                    {
                                        TextBox TxtBx = (TextBox)Item[Column].Controls[0];
                                    }
                                    if (Item[Column].Controls.Count > 1)
                                    {
                                        ItemControle = Item[Column].Controls[1];
                                        if (ItemControle is RadComboBox)
                                        {
                                            RadComboBox RCB = (RadComboBox)Item[Column].Controls[1];
                                              MyQue .Answervalue = RCB.SelectedValue;
                                        }
                                    }
                                }
                            }
                        }
                    }
       MyQue .Save();
    }
  8. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2251 posts

    Posted 18 Aug 2015 Link to this post

    Hi,

    I have replied to your query in the other thread where you have posted. Link is available below.

    Please avoid submitting duplicate questions in different threads. Thus, we would be able to answer your queries quicker.



    Regards,
    Viktor Tachev
    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