button inside insert item template not firing to item command event on server side

2 posts, 0 answers
  1. Faizaan
    Faizaan avatar
    1 posts
    Member since:
    Aug 2019

    Posted 06 Mar 2020 Link to this post

     <telerik:RadListView ID="RadListView" runat="server" ItemPlaceholderID="pp"  OnItemCommand="RadListView_ItemCommand">
                <LayoutTemplate>
                    <telerik:RadButton RenderMode="Lightweight" ID="btnInitInsert" runat="server" CommandName="InitInsert" Text="Insert Employee" >
                    </telerik:RadButton>
                    <asp:PlaceHolder ID="pp" runat="server"></asp:PlaceHolder>
                </LayoutTemplate>
                <ItemTemplate>
                    <telerik:RadLabel ID="RadLabel1" runat="server" ForeColor="Black" Text="<%# Container.DataItem %>"></telerik:RadLabel>
                </ItemTemplate>
                <InsertItemTemplate>
                    <asp:Button ID="Button2" runat="server" Text="cancel insert" CommandName="Cancel" />
                </InsertItemTemplate>
            </telerik:RadListView>
  2. Attila Antal
    Admin
    Attila Antal avatar
    591 posts

    Posted 11 Mar 2020 Link to this post

    Hi Faizaan,

    I am suspecting that the data is being bound similar to the example below:

    protected void Page_Load(object sender, EventArgs e)
    {
        RadListView1.DataSource = new string[] { "Item 1", "Item 2", "Item 3" };
        RadListView1.DataBind();
    }

     

    It is either in the Page Load, Page Init or any other event.

    If that is the case, I highly recommend using the NeedDataSource event, see Simple vs Advanced data binding - RadListView. The DataBind() method is inherited from the ASP ListView Control, while the Telerik RadListView requires a specific way of binding data, especially for complex scenarios.

     

    You can try the following setup:

    <telerik:RadListView ID="RadListView1" runat="server" ItemPlaceholderID="pp" OnItemCommand="RadListView_ItemCommand" OnNeedDataSource="RadListView1_NeedDataSource">
        <LayoutTemplate>
            <telerik:RadButton RenderMode="Lightweight" ID="btnInitInsert" runat="server" CommandName="InitInsert" Text="Insert Employee">
            </telerik:RadButton>
            <asp:PlaceHolder ID="pp" runat="server"></asp:PlaceHolder>
        </LayoutTemplate>
        <ItemTemplate>
            <p>
                <telerik:RadLabel ID="RadLabel1" runat="server" ForeColor="Black" Text="<%# Container.DataItem %>"></telerik:RadLabel>
            </p>
        </ItemTemplate>
        <InsertItemTemplate>
            <asp:Button ID="Button2" runat="server" Text="cancel insert" CommandName="Cancel" />
        </InsertItemTemplate>
    </telerik:RadListView>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

     

    Server code:

    protected void RadListView_ItemCommand(object sender, RadListViewCommandEventArgs e)
    {
        Label1.Text += string.Format("{0} was fired <br />", e.CommandName);
    }
    
    protected void RadListView1_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
    {
        (sender as RadListView).DataSource = new string[] { "Item 1", "Item 2", "Item 3" };
    }

     

    Please keep in mind, that the ID of the controls must not be the same as the Class itself.

    For example, RadListView declaration you shared has the same id as the Class name:

     <telerik:RadListView ID="RadListView"

    While it works in some scenarios, may cause an issue in others.

    When it works:

    When an issue occurs:

     

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Back to Top