Switching between Insert/Update/Regular Modes
There are many cases where one would like to manipulate the RadListView insert/update/regular modes manually. The samples in this topic illustrate how you can switch between the different modes of the RadListView control.
Insert to Update mode and Update to Insert mode
One of the most common scenario is to display items in edit mode or the insert item at a time. With RadListView this is easy to achieve:
protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
{
if (e.CommandName == RadListView.EditCommandName)
{
RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None;
}
if (e.CommandName == RadListView.InitInsertCommandName)
{
RadListView1.EditIndexes.Clear();
}
}
Protected Sub RadListView1_ItemCommand(sender As Object, e As RadListViewCommandEventArgs)
If e.CommandName = RadListView.EditCommandName Then
RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None
End If
If e.CommandName = RadListView.InitInsertCommandName Then
RadListView1.EditIndexes.Clear()
End If
End Sub
Open RadListView Insert form
There are three different approaches you can follow when you need to show the RadListView insert form:
- Add button with CommandName set to "InitInsert"
<asp:Button ID="Button1" runat="server" CommandName="InitInsert" Text="Add New Record" />
- Using the InsertItemPosition property
<asp:Button ID="btnAddNewRecord" runat="server" Text="Add New Record" OnClick="btnAddNewRecord_Click" />
protected void btnAddNewRecord_Click(object sender, EventArgs e)
{
RadListView1.InsertItemPosition = RadListViewInsertItemPosition.FirstItem;
}
Protected Sub btnAddNewRecord_Click(sender As Object, e As EventArgs)
RadListView1.InsertItemPosition = RadListViewInsertItemPosition.FirstItem
End Sub
- Calling the ShowInsertItem() method
<asp:Button ID="btnAddNewRecord1" runat="server" Text="Add New Record" OnClick="btnAddNewRecord1_Click" />
protected void btnAddNewRecord1_Click(object sender, EventArgs e)
{
RadListView1.ShowInsertItem();
}
Protected Sub btnAddNewRecord1_Click(sender As Object, e As EventArgs)
RadListView1.ShowInsertItem()
End Sub
Close RadListView Insert form
For closing the insert form you can use the below code:
protected void btnCancelInsert_Click(object sender, EventArgs e)
{
RadListView1.ShowInsertItem(RadListViewInsertItemPosition.None);
//OR
RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None;
//OR
RadListView1.InsertItem.FireCommandEvent("Cancel", "");
}
Protected Sub btnCancelInsert_Click(sender As Object, e As EventArgs)
RadListView1.ShowInsertItem(RadListViewInsertItemPosition.None)
'OR
RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None
'OR
RadListView1.InsertItem.FireCommandEvent("Cancel", "")
End Sub
Put items in Edit mode
To turn an item or multiple items in edit mode, you might choose between the following approaches:
- Add the indexes of the items to the EditIndexes collection on Page_Load (without additional rebind)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadListView1.EditIndexes.Add(0);
}
}
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
RadListView1.EditIndexes.Add(0)
End If
End Sub
- Set the Edit property of an item to true and Rebind()
protected void RadListView1_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (RadListViewDataItem item in RadListView1.Items)
{
item.Edit = true;
}
RadListView1.Rebind();
}
}
Protected Sub RadListView1_PreRender(sender As Object, e As EventArgs)
If Not IsPostBack Then
For Each item As RadListViewDataItem In RadListView1.Items
item.Edit = True
Next
RadListView1.Rebind()
End If
End Sub
Clear edited items
Another case is where you need to close all edited items on external button click:
<asp:Button ID="btnClearEditedItems" runat="server" Text="Clear Edited Items" OnClick="btnClearEditedItems_Click" />
protected void btnClearEditedItems_Click(object sender, EventArgs e)
{
RadListView1.EditIndexes.Clear();
RadListView1.Rebind();
}
Protected Sub btnClearEditedItems_Click(sender As Object, e As EventArgs)
RadListView1.EditIndexes.Clear()
RadListView1.Rebind()
End Sub