This is a migrated thread and some comments may be shown as answers.

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

1 Answer 439 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Faizaan
Top achievements
Rank 1
Faizaan asked on 06 Mar 2020, 09:24 PM
 <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>

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 11 Mar 2020, 04:10 PM

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.
Tags
ListView
Asked by
Faizaan
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or