Accessing controls on a Radgrid inside of a FormTemplate inside of a Radgrid

8 posts, 1 answers
  1. Dale
    Dale avatar
    15 posts
    Member since:
    Sep 2015

    Posted 02 Dec 2016 Link to this post

    Yeah...  This is almost similar to a previous post that was answered, so now I have gone to the next level.

    I have a RadGrid that when I add a record, or edit an existing record, I want a GridBoundColumn automatically updated.

    This in the scenario, I select Add A Record, the grid opens.  I have a dropdownlist inside of an EditItemTemplate.  I can select an item from the drop down list.  Now based on the item selected from the drop down list I want a GridBoundColumn that is available for editing to be progammatically populated.

    So my question is how do I get ahold of the control so when the item in the drop down list is selected it fires off a code behind routine that automatically populates the box for the GridBoundColumn.  After the box is populated I either select Insert or Update(if applicable) and it goes into my database.

    Regardless of what I have tried I cannot get to the controls on the RadGrid(s) in the FormTemplate.

    The structure is:

    1. RadGrid

    2. MasterTableView

    3. EditFormSettings

    4. Form Template

    5. RadGrid(s) (actually 5 on a tabstrip - this applies to all when it finally working)

    6. MasterTableView

    7. GridTemplateColumn

    8. EditItemTemplate

    9. asp:DropDownlist  (what I want to get the selected value from.)

     

    So now we are 9 levels down.

    I can access the controls in the FormTemplate( tables/rows/cells containing labels, textboxes, etc.)

    But how do I access the controls in the RadGrid(s) within the Form Template.

    The Insert and Update on the RadGrids function correctly.

     

    Thank you for your time.  Dale

  2. Eyup
    Admin
    Eyup avatar
    4046 posts

    Posted 07 Dec 2016 Link to this post

    Hello Dale,

    I am sending 2 web site samples to demonstrate how you can extract the value of the GridDataItem containing the DropDownList.
    The main goal is to get a reference to the grid item. Please try following these steps:

    1. On which event you want to access this information? Let's say that you are using the selected index changed event handler of the DropDownList.

    2. Cast the sender to DropDownList using the code-behind event handler, for example
    DropDownList combo = sender as DropDownList;

    3. Get a reference to the grid item:
    GridDataItem item = combo.NamingContainer as GridDataItem.

    4. Then, you can use the approaches demonstrated in the attached web site samples and access the record unique value. For example:
    string value = item.GetDataKeyValue("OrderID").ToString();

    I hope this will prove helpful. In addition, you can also check the GetItems method which can be used to get the currently edited item from an external action:
    http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html


    Regards,
    Eyup
    Telerik by Progress
    Telerik UI for ASP.NET Core is ready for Visual Studio 2017 RC! Learn more.
  3. Dale
    Dale avatar
    15 posts
    Member since:
    Sep 2015

    Posted 07 Dec 2016 in reply to Eyup Link to this post

    Thank you very much for the reply and the code samples.  They do address what I am looking for.

    This is an issue that I did not mention on my original posting and did not notice until I was going over your reply.

    The main RadGrid is Radgrid1, then the RadGrid I want to access is RadGrid5 which is within the Form Template.

    When I go to code behind the I can see RadGrid1 and all of it's associated event handlers.

    I do not see RadGrid5 available where I can choose any of it's associated event handlers.

    It does not come up in the Intellisense, RadGrid1 does, RadGrid5 does not.

    So that is my next issue, I cannot access any control that is in the Form Template from code behind.

    To access items in the Form Template this is a sample of my code.

    If TypeOf e.Item Is GridEditFormItem Then
    Dim item As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
    Dim intID As Integer = CInt(Session("SessionRecordPlatID"))
    Dim theSubdivisionName As TextBox = DirectCast(item.FindControl("textbox1"), TextBox)
    Dim theLegalDescription As TextBox = DirectCast(item.FindControl("textbox2"), TextBox)
    Dim theMunicipality As TextBox = DirectCast(item.FindControl("textbox3"), TextBox)
    Dim theNumberOfLots As RadNumericTextBox = DirectCast(item.FindControl("RadNumericTextBox1"), RadNumericTextBox)
    Dim theEffectiveYear As RadNumericTextBox = DirectCast(item.FindControl("RadNumericTextBox2"), RadNumericTextBox)
    Dim theRadDateInput As RadDatePicker = DirectCast(item.FindControl("RadDatePicker1"), RadDatePicker)
    Dim theDateInputDate As DateTime = theRadDateInput.DbSelectedDate

    I can access the items in the Form Template from code behind using the above code.

    I have not been able to find a way to access the RadGrid that is within the Form Template from code behind.

     

     

     

  4. Dale
    Dale avatar
    15 posts
    Member since:
    Sep 2015

    Posted 07 Dec 2016 in reply to Dale Link to this post

    I think I have found the problem, not sure what to do.  I have found several simple code samples on the internet that show a RadGrid inside a Form Template that is within a RadGrid.

    Below is a simple example, in this example I cannot access/intellisense any of RadGrid2 event handlers from code behind, all I can see is RadGridTEST.

    So am I missing a setting?

     

    <telerik:RadGrid ID="RadGridTEST" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGridTEST_NeedDataSource"
            OnItemDataBound="RadGridTEST_ItemDataBound">
            <MasterTableView CommandItemDisplay="Top" DataKeyNames="RecordID" EditMode="EditForms">
                <Columns>
                    <telerik:GridBoundColumn DataField="RecordID" UniqueName="RecordID" HeaderText="Record ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="RecordName" UniqueName="RecordName" HeaderText="Record Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                </Columns>
                <EditFormSettings EditFormType="Template">
                    <FormTemplate>
                        <telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid2_NeedDataSource"
                            OnUpdateCommand="RadGrid2_UpdateCommand">
                            <MasterTableView>
                                <Columns>
                                    <telerik:GridEditCommandColumn>
                                    </telerik:GridEditCommandColumn>

                                     <telerik:GridBoundColumn DataField="SubRecordID" UniqueName="SubRecordID" HeaderText="SubRecord ID">
                                    </telerik:GridBoundColumn>
                                    <<telerik:GridBoundColumn DataField="SubRecordName" UniqueName="SubRecordName" HeaderText="Sub Record Name">
                                    </telerik:GridBoundColumn>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>
                    </FormTemplate>
                </EditFormSettings>
            </MasterTableView>
        </telerik:RadGrid>

     

     

  5. Dale
    Dale avatar
    15 posts
    Member since:
    Sep 2015

    Posted 08 Dec 2016 in reply to Dale Link to this post

    Onto the next attempt to make this work.

    Refresher:  We are in Add New Record or Edit mode on the Radgrid within a FormTemplate.

    I have a asp:dropdownlist where a number is selected, once it is selected I have wrote a javascript that is fired off by the "OnChange" event and determines a value that to set in a asp:hiddenfield.

    The dropdownlist and an asp:textbox are in edititemtemplates within separate gridtemplate columns.

    Now I have the value in a hiddenfield and I want to populate the asp:textbox with the hiddenfield value when the javascript is executed.  Or just set the value of the asp:textbox from the javascript.

     

    Once again I cannot access the inner RadGrid controls.

    Also, I cannot access the inner RadGrid controls in the markup with  document.getElementById('<%= TextBox1.ClientID%>');
              

     

     

  6. Answer
    Eyup
    Admin
    Eyup avatar
    4046 posts

    Posted 12 Dec 2016 Link to this post

    Hi Dale,

    Every control that is placed within INamingContainer, the IntelliSense will not show it. It can be accessed using the FindControl method to the corresponding INamingContainer parent:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode

    In your scenario, it is best to get the relative approach for accessing the control. It depends on the event you are using. If you want to achieve this requirement on client-side, you can examine the RadGridEditComboClientHideModified.zip web site sample provided in the following post:
    http://www.telerik.com/forums/how-to-change-and-get-value-from-a-dropdownlist-in-a-radgrid-column#jIjRiaOYhkucLZ82pztwow


    I hope this will prove helpful.

    Regards,
    Eyup
    Telerik by Progress
    Telerik UI for ASP.NET Core is ready for Visual Studio 2017 RC! Learn more.
  7. Dale
    Dale avatar
    15 posts
    Member since:
    Sep 2015

    Posted 12 Dec 2016 in reply to Eyup Link to this post

    Thanks for the reply Eyup.

    I have been going over the links and I think I have a grasp of what is going on.  I am working on adapting some of the code in RadGridEditComboClientHideModified to my application though it is not doing anything.

    I will go back over what I am doing:  I have a dropdownlist in the RadGrid within the Form Template.  When I am in insert or update mode I want to select a code from the dropdownlist and have a corresponding string variable inserted into a textbox that is visible in insert and update mode when the dropdownlist code is selected.

    I have a javascript that is ran on the OnChange event of the dropdownlist that creates the text to be inserted into the textbox.  That is my last piece, getting the textbox to populate.

    My main issue was not knowing why the intellisense was not showing the RadGrid in the Form Template.

     

    I am working on implementing the selectedindexchange event from RadGridEditComboClientHideModified.

    Here is the javascript:

    function selectedIndexChanged(sender, args, index, tableViewID) {
                 alert("selectedIndexChanged");
                 var tableView = $find(tableViewID);
                 var editForm = index >= 0 ? tableView.get_dataItems()[index].get_element() :
                     tableView.get_insertItem();


                 hideIndefiniteSharing(editForm);

    }

    This is the OnSelectedIndexChanged event on the dropdownlist:

    OnSelectedIndexChanged='<%# string.Format("function (s,a){{selectedIndexChanged(s,a,{0},\"{1}\",);}}", Container.ItemIndex, Container.OwnerTableView.ClientID) %>'>

    This is all copied from RadGridEditComboClientHideModified.

    I am not familiar with the sender, args part of the Function SelectedIndexChanged.

    My question is what do I need to do to recognize the following code:

     

    <EditItemTemplate>
                                 <asp:DropDownList ID="DropDownNumber" runat="server" Width="170px" TabIndex="16" ForeColor="Black" BackColor="White" Font-Bold="true" Text='<%#Bind("TOWNSHIP_NUMBER") %>' AutoPostBack="true" OnChange="GetNUMBER(this.value)" OnSelectedIndexChanged='<%# string.Format("function (s,a){{}}", Container.ItemIndex, Container.OwnerTableView.ClientID) %>' >
                                                <asp:ListItem Value=""> </asp:ListItem>           
                                                <asp:ListItem Value="1">1</asp:ListItem>
                                                <asp:ListItem Value="2">2</asp:ListItem>
                                                <asp:ListItem Value="3">3</asp:ListItem>
                                                <asp:ListItem Value="4">4</asp:ListItem> 
                                                <asp:ListItem Value="5">5</asp:ListItem>
                                                <asp:ListItem Value="6">6</asp:ListItem>
                                                <asp:ListItem Value="7">7</asp:ListItem>
                                                <asp:ListItem Value="8">8</asp:ListItem>
                                                <asp:ListItem Value="9">9</asp:ListItem>
                                                <asp:ListItem Value="10">10</asp:ListItem>
                                                <asp:ListItem Value="11">11</asp:ListItem>
                                                <asp:ListItem Value="12">12</asp:ListItem>
                                                <asp:ListItem Value="13">13</asp:ListItem>
                                                <asp:ListItem Value="14">14</asp:ListItem>
                                                <asp:ListItem Value="15">15</asp:ListItem>
                                                <asp:ListItem Value="16">16</asp:ListItem>
                                                <asp:ListItem Value="17">17</asp:ListItem>
                                                <asp:ListItem Value="18">18</asp:ListItem>
                                                <asp:ListItem Value="19">19</asp:ListItem>
                                                <asp:ListItem Value="20">20</asp:ListItem>
                                                <asp:ListItem Value="21">21</asp:ListItem>
                                                <asp:ListItem Value="22">22</asp:ListItem>
                                                <asp:ListItem Value="23">23</asp:ListItem>
                                                <asp:ListItem Value="24">24</asp:ListItem>
                                 </asp:DropDownList>
                            </EditItemTemplate> 

     

     <EditItemTemplate>
                                <asp:TextBox runat="server" ID="NAME" Text='<%# Bind("NAME") %>' ></asp:TextBox>

     </EditItemTemplate> 

     

    Thank you for your time.  Dale,

     

     

  8. Dale
    Dale avatar
    15 posts
    Member since:
    Sep 2015

    Posted 13 Dec 2016 in reply to Dale Link to this post

    I do want to thank you for your time, I did get it working by a different method.  On the asp DropDownList I added

    OnSelectedIndexChanged="DropDownNumber_SelectedIndexChanged" by using Create New Event.

    This gave me the DropDownNumber_SelectedIndexChanged in code behind.

    Once I had that I created the following  code which updated the text box on the RadGrid in the form template.

     Protected Sub DropDownNumber_SelectedIndexChanged(sender As Object, e As EventArgs)
            Try
                Dim dropDownList As DropDownList = DirectCast(sender, DropDownList)
                Dim editItem As GridEditableItem = DirectCast(dropDownList.NamingContainer, GridEditableItem)
                Dim theNumber As DropDownList = DirectCast(editItem.FindControl("DropDownNumber"), DropDownList)
                Dim theName As TextBox = DirectCast(editItem.FindControl("NAME"), TextBox)
                Dim index As Integer = editItem.ItemIndex
               

    End Sub

     

    From this I can get the selected DropDownNumber and then cast to the "NAME" control which is the ID of my textbox.

    I can then set the value of the "NAME" control with

    theName.text = "TEST"  or whatever text value you want to use.

     

                                                       

Back to Top