Using InPlace and EditForms Modes
This article will show you how you can access the values on insert when you are using the autogenerated edit forms of RadTreeList and you want to do manual inserts. The stress will be on accessing the values, leaving the insert logic to your choice, depending on your own datasource handling. There are generally two options on how to access the inserted values:
Using the ExtractValuesFromItem() method
Using the column editors of the treelist columns
Accessing the insert values using ExtractValuesFromItem() method
The ExtractValuesFromItem(dictionaryObject, editableItem, includePrimaryKey) method of RadTreeList takes the following arguments:
IDictionary dictionaryObject - the collection which will hold the values, using the column UniqueName of each edit field as a key.
TreeListEditableItem - the current editable item from which the values will be extracted.
bool includePrimaryKey - indicates whether the primary key value for the current item should be extracted along with the other values.
After you get the IDictionary object populated, you can use the provided values to insert a new item into the treelist datasource.
<telerik:RadTreeList RenderMode="Lightweight" ID="RadTreeList1" runat="server" DataKeyNames="EmployeeID" ParentDataKeyNames="ReportsTo"
AutoGenerateColumns="false" EditMode="EditForms" DataSourceID="SqlDataSource1" OnInsertCommand="RadTreeList1_InsertCommand">
<Columns>
<telerik:TreeListBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID" ReadOnly="true" />
<telerik:TreeListBoundColumn DataField="LastName" HeaderText="LastName" UniqueName="LastName" />
<telerik:TreeListBoundColumn DataField="FirstName" HeaderText="FirstName" UniqueName="FirstName" />
<telerik:TreeListBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title" />
<telerik:TreeListDateTimeColumn DataField="HireDate" HeaderText="HireDate" UniqueName="HireDate" />
<telerik:TreeListBoundColumn DataField="ReportsTo" HeaderText="ReportsTo" UniqueName="ReportsTo" ReadOnly="true" />
<telerik:TreeListEditCommandColumn UniqueName="EditColumn" />
</Columns>
</telerik:RadTreeList>
protected void RadTreeList1_InsertCommand(object sender, TreeListCommandEventArgs e)
{
//Canceling out the automatic datasource operation (needed if you use a datasource control)
e.Canceled = true;
//Using the ExtractValuesFromItem() method to get hold of the newly provided values
Hashtable insertValues = new Hashtable();
TreeListEditableItem editedItem = e.Item as TreeListEditableItem;
e.Item.OwnerTreeList.ExtractValuesFromItem(insertValues, editedItem, false);
//Inserting logic follows, this part depends on your own custom way of performing CRUD operations
SqlDataSource1.InsertParameters["LastName"].DefaultValue = insertValues["LastName"].ToString();
SqlDataSource1.InsertParameters["LastName"].DefaultValue = insertValues["LastName"].ToString();
SqlDataSource1.InsertParameters["FirstName"].DefaultValue = insertValues["FirstName"].ToString();
SqlDataSource1.InsertParameters["Title"].DefaultValue = insertValues["Title"].ToString();
SqlDataSource1.InsertParameters["HireDate"].DefaultValue = insertValues["HireDate"].ToString();
SqlDataSource1.Insert();
//Closing the insert form and rebinding the treelist control (needed when the default action was canceled)
RadTreeList1.InsertIndexes.Clear();
RadTreeList1.Rebind();
}
Protected Sub RadTreeList1_InsertCommand(ByVal sender As Object, ByVal e As TreeListCommandEventArgs) Handles RadTreeList1.InsertCommand
'Canceling out the automatic datasource operation (needed if you use a datasource control)
e.Canceled = True
'Using the ExtractValuesFromItem() method to get hold of the newly provided values
Dim insertValues As New Hashtable()
Dim editedItem As TreeListEditableItem = TryCast(e.Item, TreeListEditableItem)
e.Item.OwnerTreeList.ExtractValuesFromItem(insertValues, editedItem, False)
'Inserting logic follows, this part depends on your own custom way of performing CRUD operations
SqlDataSource1.InsertParameters("LastName").DefaultValue = insertValues("LastName").ToString()
SqlDataSource1.InsertParameters("LastName").DefaultValue = insertValues("LastName").ToString()
SqlDataSource1.InsertParameters("FirstName").DefaultValue = insertValues("FirstName").ToString()
SqlDataSource1.InsertParameters("Title").DefaultValue = insertValues("Title").ToString()
SqlDataSource1.InsertParameters("HireDate").DefaultValue = insertValues("HireDate").ToString()
SqlDataSource1.Insert()
'Closing the insert form and rebinding the treelist control
RadTreeList1.InsertIndexes.Clear()
RadTreeList1.Rebind()
End Sub
Accessing the insert values using column editors
This can be achieved by getting hold of the current editable item and then accessing each column editor by column UniqueName. Then you just get the value from the control that the editor holds by using the control's own API.
<telerik:RadTreeList RenderMode="Lightweight" ID="RadTreeList2" runat="server" DataKeyNames="EmployeeID" ParentDataKeyNames="ReportsTo"
AutoGenerateColumns="false" EditMode="InPlace" DataSourceID="SqlDataSource1" OnInsertCommand="RadTreeList2_InsertCommand">
<Columns>
<telerik:TreeListBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID" ReadOnly="true" />
<telerik:TreeListBoundColumn DataField="LastName" HeaderText="LastName" UniqueName="LastName" />
<telerik:TreeListBoundColumn DataField="FirstName" HeaderText="FirstName" UniqueName="FirstName" />
<telerik:TreeListBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title" />
<telerik:TreeListDateTimeColumn DataField="HireDate" HeaderText="HireDate" UniqueName="HireDate" />
<telerik:TreeListBoundColumn DataField="ReportsTo" HeaderText="ReportsTo" UniqueName="ReportsTo" ReadOnly="true" />
<telerik:TreeListEditCommandColumn UniqueName="EditColumn" />
</Columns>
</telerik:RadTreeList>
protected void RadTreeList2_InsertCommand(object sender, TreeListCommandEventArgs e)
{
//Canceling out the automatic datasource operation (needed if you use a datasource control)
e.Canceled = true;
//Accessing the values of the insert item through the column editors
TreeListEditableItem insertedItem = e.Item as TreeListEditableItem;
string lastName = (insertedItem.GetColumnEditor("LastName") as TreeListTextBoxColumnEditor).TextBoxControl.Text;
string firstName = (insertedItem.GetColumnEditor("FirstName") as TreeListTextBoxColumnEditor).TextBoxControl.Text;
string title = (insertedItem.GetColumnEditor("Title") as TreeListTextBoxColumnEditor).TextBoxControl.Text;
DateTime? hireDate = (insertedItem.GetColumnEditor("HireDate") as TreeListDateTimeColumnEditor).DatePickerControl.SelectedDate;
//Inserting logic follows, this part depends on your own custom way of performing CRUD operations
SqlDataSource1.InsertParameters["LastName"].DefaultValue = lastName;
SqlDataSource1.InsertParameters["FirstName"].DefaultValue = firstName;
SqlDataSource1.InsertParameters["Title"].DefaultValue = title;
SqlDataSource1.InsertParameters["HireDate"].DefaultValue = hireDate.ToString();
SqlDataSource1.Insert();
//Closing the insert form and rebinding the treelist control
RadTreeList2.InsertIndexes.Clear();
RadTreeList2.Rebind();
}
Protected Sub RadTreeList2_InsertCommand(ByVal sender As Object, ByVal e As TreeListCommandEventArgs)
'Canceling out the automatic datasource operation (needed if you use a datasource control)
e.Canceled = True
'Accessing the values of the insert item through the column editors
Dim insertedItem As TreeListEditableItem = TryCast(e.Item, TreeListEditableItem)
Dim lastName As String = TryCast(insertedItem.GetColumnEditor("LastName"), TreeListTextBoxColumnEditor).TextBoxControl.Text
Dim firstName As String = TryCast(insertedItem.GetColumnEditor("FirstName"), TreeListTextBoxColumnEditor).TextBoxControl.Text
Dim title As String = TryCast(insertedItem.GetColumnEditor("Title"), TreeListTextBoxColumnEditor).TextBoxControl.Text
Dim hireDate As System.Nullable(Of DateTime) = TryCast(insertedItem.GetColumnEditor("HireDate"), TreeListDateTimeColumnEditor).DatePickerControl.SelectedDate
'Inserting logic follows, this part depends on your own custom way of performing CRUD operations
SqlDataSource1.InsertParameters("LastName").DefaultValue = lastName
SqlDataSource1.InsertParameters("FirstName").DefaultValue = firstName
SqlDataSource1.InsertParameters("Title").DefaultValue = title
SqlDataSource1.InsertParameters("HireDate").DefaultValue = hireDate.ToString()
SqlDataSource1.Insert()
'Closing the insert form and rebinding the treelist control
RadTreeList2.InsertIndexes.Clear()
RadTreeList2.Rebind()
End Sub
Note that RadTreeList expects the newly inserted value of the field that is specified as DataKeyNames to be larger than any of the already available values of this field in the datasource.