|
| 1 | +--- |
| 2 | +title: Dynamically Updating Dropdowns in RadGrid EditItemTemplate Based on Selection |
| 3 | +description: Learn how to update a dropdown list within a RadGrid's EditItemTemplate based on another dropdown's selection change using client-side logic. |
| 4 | +type: how-to |
| 5 | +page_title: Dynamically Updating Dropdowns in RadGrid EditItemTemplate Based on Selection |
| 6 | +slug: grid-dynamically-updating-dropdowns-in-edititemtemplate-based-on-selection |
| 7 | +tags: radgrid, dropdownlist, edititemtemplate, clientside, asp.net ajax |
| 8 | +res_type: kb |
| 9 | +ticketid: 1677014 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>Progress® Telerik® UI for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>All</td> |
| 23 | +</tr> |
| 24 | +</table> |
| 25 | + |
| 26 | +## Description |
| 27 | + |
| 28 | +I am using a RadGrid that contains dropdown lists in the EditItemTemplate. I need to populate the dropdown in column 2 based on the selection made in the dropdown of column 1, all from the client side. |
| 29 | + |
| 30 | +This knowledge base article also answers the following questions: |
| 31 | + |
| 32 | +- How can I dynamically change the options of a dropdown in RadGrid based on another dropdown's selection? |
| 33 | +- What is the process to link two dropdown lists in a RadGrid EditItemTemplate? |
| 34 | +- How do I implement cascading dropdowns within a RadGrid? |
| 35 | + |
| 36 | +## Solution |
| 37 | + |
| 38 | +To dynamically update a dropdown list in a RadGrid's EditItemTemplate based on the selection of another dropdown, follow the steps below: |
| 39 | + |
| 40 | +### Step 1: Setup the RadGrid with Dropdowns |
| 41 | + |
| 42 | +Ensure your RadGrid is configured with two dropdown lists in the EditItemTemplate. Example setup: |
| 43 | + |
| 44 | +````ASP.NET |
| 45 | +<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"> |
| 46 | + <MasterTableView EditMode="EditForms"> |
| 47 | + <Columns> |
| 48 | + <telerik:GridEditCommandColumn /> |
| 49 | + <telerik:GridTemplateColumn HeaderText="Column 1"> |
| 50 | + <EditItemTemplate> |
| 51 | + <telerik:RadDropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> |
| 52 | + <Items> |
| 53 | + <telerik:DropDownListItem Text="Option 1" Value="1" /> |
| 54 | + <telerik:DropDownListItem Text="Option 2" Value="2" /> |
| 55 | + </Items> |
| 56 | + </telerik:RadDropDownList> |
| 57 | + </EditItemTemplate> |
| 58 | + </telerik:GridTemplateColumn> |
| 59 | + <telerik:GridTemplateColumn HeaderText="Column 2"> |
| 60 | + <EditItemTemplate> |
| 61 | + <telerik:RadDropDownList ID="DropDownList2" runat="server" /> |
| 62 | + </EditItemTemplate> |
| 63 | + </telerik:GridTemplateColumn> |
| 64 | + </Columns> |
| 65 | + </MasterTableView> |
| 66 | +</telerik:RadGrid> |
| 67 | +```` |
| 68 | + |
| 69 | +### Step 2: Handle the SelectedIndexChanged Event |
| 70 | + |
| 71 | +In the code-behind, manage the `SelectedIndexChanged` event of the first dropdown to update the second dropdown's data source. |
| 72 | + |
| 73 | +````C# |
| 74 | +protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) |
| 75 | +{ |
| 76 | + RadDropDownList dropdown1 = (RadDropDownList)sender; |
| 77 | + GridEditableItem editItem = (GridEditableItem)dropdown1.NamingContainer; |
| 78 | + RadDropDownList dropdown2 = (RadDropDownList)editItem.FindControl("DropDownList2"); |
| 79 | + |
| 80 | + string selectedValue = dropdown1.SelectedValue; |
| 81 | + dropdown2.DataSource = GetDataBasedOnSelection(selectedValue); |
| 82 | + dropdown2.DataBind(); |
| 83 | +} |
| 84 | + |
| 85 | +private List<string> GetDataBasedOnSelection(string selectedValue) |
| 86 | +{ |
| 87 | + // Fetch data based on the selected value |
| 88 | + return new List<string> { "Item 1", "Item 2", "Item 3" }; |
| 89 | +} |
| 90 | + |
| 91 | +protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) |
| 92 | +{ |
| 93 | + DataTable dataTable = new DataTable(); |
| 94 | + |
| 95 | + dataTable.Columns.Add("Column1", typeof(string)); |
| 96 | + dataTable.Columns.Add("Column2", typeof(string)); |
| 97 | + |
| 98 | + dataTable.Rows.Add("Value 1", "Value A"); |
| 99 | + dataTable.Rows.Add("Value 2", "Value B"); |
| 100 | + dataTable.Rows.Add("Value 3", "Value C"); |
| 101 | + |
| 102 | + RadGrid1.DataSource = dataTable; |
| 103 | +} |
| 104 | +```` |
| 105 | + |
| 106 | +### Explanation |
| 107 | + |
| 108 | +The `SelectedIndexChanged` event dynamically populates the second dropdown based on the first dropdown's selection. Adjust the `GetDataBasedOnSelection` method to suit your data retrieval logic. |
| 109 | + |
| 110 | +This method leverages the AJAX capabilities of Telerik controls for a seamless user experience. |
| 111 | + |
| 112 | +## See Also |
| 113 | + |
| 114 | +- [RadGrid Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/overview) |
| 115 | +- [RadDropDownList Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/overview) |
0 commit comments