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

Drop Down List in insert box

1 Answer 56 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 11 Dec 2013, 09:57 AM
Hi,
I have a TreeList control, with an insert template as below:

            <EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <table>
                        <tr>
                            <td>Is this a header:
                            </td>
                            <td>
                                <asp:checkbox ID="cbIsHeader" runat="server" />
                            </td>
                        </tr>                       
                        <tr>
                            <td>Header Text:
                            </td>
                            <td>
                                <asp:TextBox ID="txtNewHeader" Text='<%# Bind("DepartmentList_Header")%>' runat="server">
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>Department</td>
                            <td><asp:DropDownList runat="server" ID="ddlDepartments"/></td>
                        </tr>
                    </table>
                    <asp:Button ID="btnUpdate" Text="Insert"
                        runat="server" CommandName="PerformInsert"></asp:Button>
                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                        CommandName="Cancel"></asp:Button>
                    <asp:HiddenField ID="hfParent" runat="server" Value='<%# Bind("DepartmentList_Parent")%>' />
                </FormTemplate>
            </EditFormSettings>

I am trying to pre-populate the drop down menu from a stored procedure, but not certain how this could be accomplished. Can anyone help?

Cheers,
Mark

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 16 Dec 2013, 07:50 AM
Hi Mark,

You could accomplish the desired functionality by handling the ItemDataBound event of the RadTreeList control, get reference to the TreeListEditFormItem and then to the DropDownList  control. After having the reference to the DropDownList control you can assign a DataSource or DataSourceID with parameters depending on the current item.

Bellow you will find a simple example of the described approach:
<telerik:RadTreeList runat="server" ID="RadTreeList1" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
    AllowPaging="true" PageSize="5" DataKeyNames="EmployeeID" ParentDataKeyNames="ReportsTo" OnItemDataBound="RadTreeList1_ItemDataBound">
       <Columns>
            <telerik:TreeListBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID">
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title">
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn DataField="ReportsTo" HeaderText="ReportsTo" UniqueName="ReportsTo">
            </telerik:TreeListBoundColumn>
           <telerik:TreeListEditCommandColumn></telerik:TreeListEditCommandColumn>
       </Columns>
     <EditFormSettings EditFormType="Template">
        <FormTemplate>
            <table>                   
                <tr>
                    <td>Header Text:
                    </td>
                    <td>
                        <asp:TextBox ID="txtNewHeader" Text='<%# Bind("EmployeeID")%>' runat="server">
                        </asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Department</td>
                    <td><asp:DropDownList runat="server" ID="ddlDepartments" DataTextField="Title"/></td>
                </tr>
            </table>
            <asp:Button ID="btnUpdate" Text="Insert"
                runat="server" CommandName="PerformInsert"></asp:Button>
            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                CommandName="Cancel"></asp:Button>
            <asp:HiddenField ID="hfParent" runat="server" Value='<%# Bind("EmployeeID")%>' />
        </FormTemplate>
    </EditFormSettings>
  </telerik:RadTreeList>
 
 <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" ProviderName="System.Data.SqlClient"
     SelectCommand="SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees" runat="server"></asp:SqlDataSource>
 
<asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" ProviderName="System.Data.SqlClient"
     SelectCommand="SELECT Title FROM Employees WHERE EmployeeID = @EmployeeID" runat="server">
    <SelectParameters>
        <asp:Parameter Name="EmployeeID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

And the code-behind:
protected void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
{
    if (e.Item is TreeListEditFormItem)
    {
        TreeListEditFormItem item = e.Item as TreeListEditFormItem;
        if (item.IsInEditMode)
        {
            DropDownList dropDown = item.FindControl("ddlDepartments") as DropDownList;
            string employeeID = (item.FindControl("txtNewHeader") as TextBox).Text;
            SqlDataSource2.SelectParameters[0].DefaultValue = employeeID;
            dropDown.DataSourceID = "SqlDataSource2";
        }
    }
}

Hope that helps.


Regards,
Konstantin Dikov
Telerik
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 the blog feed now.
Tags
TreeList
Asked by
Mark
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or