Inserting Values Using UserControl/FormTemplate
Inserting values through WebUserControl Edit Form and FormTemplate can be done in exactly the same way as updating values from these custom edit forms. The only difference is that you need to create a new record in the grid data source instead of updating an already existing one. To handle the actual insert operation when the user hits the Insert button, you can hook the InsertCommand event of Telerik RadGrid or the ItemCommand event (checking whether e.CommandName is RadGrid.PerformInsertCommandName).The forthcoming code snippets show how this can be done with user control custom edit form:
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
//Create new row in the DataSource
DataRow newRow = this.Employees.NewRow();
//Insert new values
Hashtable newValues = new Hashtable();
newValues["Country"] = (userControl.FindControl("TextBox7") as TextBox).Text;
newValues["City"] = (userControl.FindControl("TextBox8") as TextBox).Text;
newValues["Region"] = (userControl.FindControl("TextBox9") as TextBox).Text;
newValues["HomePhone"] = (userControl.FindControl("TextBox10") as TextBox).Text;
newValues["BirthDate"] = (userControl.FindControl("TextBox11") as TextBox).Text;
newValues["TitleOfCourtesy"] = (userControl.FindControl("ddlTOC") as DropDownList).SelectedItem.Value;
newValues["Notes"] = (userControl.FindControl("TextBox1") as TextBox).Text;
newValues["Address"] = (userControl.FindControl("TextBox6") as TextBox).Text;
newValues["FirstName"] = (userControl.FindControl("TextBox2") as TextBox).Text;
newValues["LastName"] = (userControl.FindControl("TextBox3") as TextBox).Text;
newValues["HireDate"] = (userControl.FindControl("Textbox5") as TextBox).Text;
newValues["Title"] = (userControl.FindControl("TextBox4") as TextBox).Text;
//make sure that unique primary key value is generated for the inserted row
newValues["EmployeeID"] = (int)this.Employees.Rows[this.Employees.Rows.Count - 1]["EmployeeID"] + 1;
try
{
foreach (DictionaryEntry entry in newValues)
{
newRow[(string)entry.Key] = entry.Value;
}
this.Employees.Rows.Add(newRow);
this.Employees.AcceptChanges();
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to update/insert Employees. Reason: " + ex.Message));
e.Canceled = true;
}
}
and with form template custom edit form (note that with form template you may prefer the codeless approach by enabling the automatic operations supported by Telerik RadGrid):
private void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.PerformInsertCommandName)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
//Create new row in the DataSource
DataRow newRow = this.Employees.NewRow();
//Insert new values
Hashtable newValues = new Hashtable();
newValues["Country"] = (editedItem.FindControl("TextBox7") as TextBox).Text;
newValues["City"] = (editedItem.FindControl("TextBox8") as TextBox).Text;
newValues["Region"] = (editedItem.FindControl("TextBox9") as TextBox).Text;
newValues["HomePhone"] = (editedItem.FindControl("TextBox10") as TextBox).Text;
newValues["BirthDate"] = (editedItem.FindControl("TextBox11") as TextBox).Text;
newValues["TitleOfCourtesy"] = (editedItem.FindControl("ddlTOC") as DropDownList).SelectedItem.Value;
newValues["Notes"] = (editedItem.FindControl("TextBox1") as TextBox).Text;
newValues["Address"] = (editedItem.FindControl("TextBox6") as TextBox).Text;
newValues["FirstName"] = (editedItem.FindControl("TextBox2") as TextBox).Text;
newValues["LastName"] = (editedItem.FindControl("TextBox3") as TextBox).Text;
newValues["HireDate"] = (editedItem.FindControl("Textbox5") as TextBox).Text;
newValues["Title"] = (editedItem.FindControl("TextBox4") as TextBox).Text;
//make sure that unique primary key value is generated for the inserted row
newValues["EmployeeID"] = (int)this.Employees.Rows[this.Employees.Rows.Count - 1]["EmployeeID"] + 1;
try
{
foreach (DictionaryEntry entry in newValues)
{
newRow[(string)entry.Key] = entry.Value;
}
this.Employees.Rows.Add(newRow);
this.Employees.AcceptChanges();
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to update/insert Employees. Reason: " + ex.Message));
e.Canceled = true;
}
}
}
Additionally, review the online demos linked below to see the approach in real-life example:
Form template custom edit form
Important detail when inserting with WebUserControl as edit form
If you use WebUserControl as edit form in Telerik RadGrid and populate the values inside the user control with DataItem property (defined in the code-behind of this user control), you have to make sure that this DataItem property is of type Object. Thus the automatic insert operation (when clicking Add new record from the link button inside the CommandItem) will be performed as expected.
public object DataItem
{
get
{
return this._dataItem;
}
set
{
this._dataItem = value;
}
}
Setting predefined values for controls inside user control on item insertion
When you want to access controls inside the currently inserted item, having user control as an edit form in Telerik RadGrid, you should do the following:
-
Cancel the default action if e.CommandName is RadGrid.InitInsertCommandName;
-
Insert the new item and rebind the grid;
-
Obtain reference to the newly inserted item using the GetInsertItem() method for the respective GridTableView;
-
Locate the respective control inside the user control calling the FindControl() method of this user control.
Here is an example which will change the text for TextBox with ID txtEmployeeID inside insertion form user control of Telerik RadGrid:
private void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.InitInsertCommandName)
{
e.Canceled = true;
e.Item.OwnerTableView.InsertItem();
GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
String MyUserControlId = GridEditFormItem.EditFormUserControlID;
UserControl MyUserControl = insertedItem.FindControl(MyUserControlId) as UserControl;
TextBox box = MyUserControl.FindControl("txtEmployeeID") as TextBox;
box.Text = "11";
}
}
Setting predefined values for controls inside FormTemplate on item insertion
When you want to access controls inside the currently inserted item, having FormTemplate as an edit form in Telerik RadGrid, you should do the following:
-
Cancel the default action if e.CommandName is RadGrid.InitInsertCommandName;
-
Insert the new item and rebind the grid;
-
Obtain reference to the newly inserted item using the GetInsertItem() method for the respective GridTableView;
-
Locate the respective control inside the FormTemplate calling the FindControl() method of the corresponding GridEditableItem.
Here is an example which will change the text for TextBox with ID txtEmployeeID inside insertion form user control of Telerik RadGrid:
private void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.InitInsertCommandName)
{
e.Canceled = true;
e.Item.OwnerTableView.InsertItem();
GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
TextBox box = insertedItem.FindControl("txtEmployeeID") as TextBox;
box.Text = "11";
}
}
When you have dropdown list/checkbox in the grid make sure that you specify the field to which you bind the control inside the form template as key for the dictionary object (passed to the InsertItem(newValues) method). For example:
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
If (e.CommandName = RadGrid.InitInsertCommandName) Then
e.Canceled = True
'Prepare an IDictionary with the predefined values
Dim newValues As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()
'set initial selection for the dropdown list on init insert
newValues("Country") = "Germany"
'set initial checked state for the checkbox on init insert
newValues("Bool") = False
'Insert the item and rebind
e.Item.OwnerTableView.InsertItem(newValues)
End If
End Sub
The other option is to set AppendDataBoundItems="true" for a dropdown list (residing in form template custom edit form) and add default empty item in the dropdown control to avoid exception generation on initial insert:
<EditFormSettings EditFormType="Template">
<FormTemplate>
-------------
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" DataSourceID="AccessDataSource2"
DataTextField="ProductID" DataValueField="ProductID" SelectedValue='<%# Bind("ProductID") %>' runat="server">
<asp:ListItem Text="" Value="" />
</asp:DropDownList>
--------------
</FormTemplate>
</EditFormSettings>
This is a codeless approach, however have in mind that this empty option will be displayed in the dropdown editor of an existing edited grid row as well.