Dustin Hodges
Dustin Hodges

Reputation: 4193

RadListView insertitemtemplate still showing after insert

I have a pretty simple scenario using manual data operations with rad list view. I insert an item into my collection in the ItemInserting event, the listview shows the new item but the insertitemtemplate is still showing. Is my setup wrong? Do I have to manually hide the thing?

Markup

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test2.aspx.vb" Inherits="Test2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="rsm"></telerik:RadScriptManager>
    <div>
        <telerik:RadListView ID="rlv" runat="server" DataKeyNames="UserID" ItemPlaceholderID="itemPlaceholder" Height="400px" AllowPaging="true">
            <InsertItemTemplate>
                <tr>
                    <td>
                        <div style="vertical-align: middle; white-space: nowrap;">
                            <asp:LinkButton ID="btnInsert2" runat="server" Text="Insert" CommandName='<%# Telerik.Web.UI.RadListView.PerformInsertCommandName %>'></asp:LinkButton>
                            <asp:LinkButton ID="btnCancel2" runat="server" Text="Cancel" CommandName='<%# Telerik.Web.UI.RadListView.CancelCommandName %>'></asp:LinkButton>                           
                        </div>
                    </td>
                    <td>
                        <div style="vertical-align: middle; white-space: nowrap;">
                            <telerik:RadComboBox id="RadComboBox1" runat="server" autopostback="True" causesvalidation="False" allowcustomtext="True" backcolor="White" emptymessage="Select a Person" enableloadondemand="True" showmoreresultsbox="True" width="150px" dropdownwidth="200px" zindex="9002" enablescreenboundarydetection="False" style="margin-bottom: 0px" datatextfield="UserName" datavaluefield="pkUserID" filter="Contains" onitemsrequested="RCB_ItemsRequested">
                                        <CollapseAnimation Duration="200" Type="OutQuint" />
                                    </telerik:RadComboBox>
                        </div>
                    </td>
                </tr>
            </InsertItemTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Literal ID="lName" runat="server" Text='<%# Eval("UserName") %>'></asp:Literal>
                    </td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <asp:LinkButton ID="btnInsert" runat="server" CausesValidation="False" CommandName='<%# Telerik.Web.UI.RadListView.InitInsertCommandName %>' Text="Add User" />
                <table>
                    <tr>
                        <td style="border: ridge 1px lightgray">
                            <strong>Applicable Users</strong>
                            <table>
                                <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                            </table>
                        </td>
                    </tr>                   
                </table>
            </LayoutTemplate>
        </telerik:RadListView>
    </div>
    </form>
</body>
</html>

Code Behind

Partial Class Test2
    Inherits System.Web.UI.Page

    Public Property Users As IList(Of UserInfo)
        Get
            Return If(ViewState("Users"), New List(Of UserInfo))
        End Get
        Set(ByVal value As IList(Of UserInfo))
            ViewState("Users") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Me.Users = GetInitialUsers()
        End If

        rlv.DataSource = Users
        rlv.DataBind()

        Me.ViewState.SetItemDirty("Users", True)
    End Sub

    Private Function GetInitialUsers() As IList(Of UserInfo)
        Dim Users As New List(Of UserInfo)

        Users.Add(New UserInfo(1, "1"))
        Users.Add(New UserInfo(2, "2"))
        Users.Add(New UserInfo(3, "2"))
        Users.Add(New UserInfo(4, "4"))

        Return Users
    End Function

    Protected Sub RCB_ItemsRequested(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs)
        Dim ComboBox As Telerik.Web.UI.RadComboBox = sender

        Dim itemOffset As Integer = e.NumberOfItems
        Dim NumberOfItems As Integer = 1000000000
        Dim Take As Integer = 20

        For i As Integer = 0 To Take
            ComboBox.Items.Add(New Telerik.Web.UI.RadComboBoxItem(i + itemOffset, (i + itemOffset).ToString))
        Next

        Dim NumberOfItemsInComboBox As Integer = e.NumberOfItems + ComboBox.Items.Count

        If NumberOfItems > 0 Then
            e.Message = [String].Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", NumberOfItemsInComboBox, NumberOfItems)
        Else
            e.Message = "No matches"
        End If

        e.EndOfItems = (NumberOfItemsInComboBox >= NumberOfItems)
    End Sub

    Protected Sub rlv_ItemInserting(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadListViewCommandEventArgs) Handles rlv.ItemInserting
        Dim ComboBox As Telerik.Web.UI.RadComboBox = e.ListViewItem.FindControl("RadComboBox1")

        If Not String.IsNullOrWhiteSpace(ComboBox.SelectedValue) Then
            Dim UserID As Integer = ComboBox.SelectedValue
            Dim UserName As String = ComboBox.Text

            Me.Users.Add(New UserInfo(UserID, UserName))
        Else
            e.Canceled = True
        End If

    End Sub

    <Serializable()>
    Public Class UserInfo
        Private _UserID As Integer
        Private _UserName As String

        Public ReadOnly Property UserID As Integer
            Get
                Return _UserID
            End Get
        End Property

        Public ReadOnly Property UserName As String
            Get
                Return _UserName
            End Get
        End Property

        Public Sub New(ByVal UserID As Integer, ByVal UserName As String)
            _UserID = UserID
            _UserName = UserName
        End Sub

    End Class

End Class

Upvotes: 1

Views: 1625

Answers (2)

user3766317
user3766317

Reputation: 1

radlistview1.IsItemInserted=false;

or set e.canceled=false after save click

Upvotes: 0

Kevin Babcock
Kevin Babcock

Reputation: 10247

After you have inserted the new item, set the InsertItemPosition to None:

RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None

Upvotes: 1

Related Questions