How to call a command of a rad grid

user710502 picture user710502 · Dec 5, 2011 · Viewed 8.7k times · Source

i have a rad grid with the following code

 <telerik:GridTemplateColumn>
   <ItemTemplate>
   <asp:LinkButton ID="Delete" Text="Remove &raquo" 
    CommandArgument='<%# Eval("ApartmentId") %>'
     CommandName="RemoveItem" CssClass="Button" runat="server" />
      </ItemTemplate>
   </telerik:GridTemplateColumn>

and .CS code as follows

protected void radGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Page")
    {
    }
    else
    {
        if (e.CommandName == "RemoveItem")
        {
           Apartments apartAdmin = new Apartment();
           bool deleted = apartAdmin.Delete(int.Parse(e.CommandArgument.ToString()); 
            if (deleted)
            {
                radGrid.Rebind();
            }
        }
    }
}

My problem is that when I debug it, say I add the breakpoint to this event, it is never fired, is like if it does not see the event for some reason... Can anyone see what the problem may be? This is the mark up of the grid at the top

 <telerik:RadGrid ID="radGrid" ShowFooter="true" ShowHeader="true" CaptionAlign="Left"
  runat="server" ForeColor="Black" CellPadding="4" AutoGenerateColumns="False"
  CssClass="Grid" Width="100%" GridLines="None" OnRowCommand="radGrid_RowCommand"
  OnNeedDataSource="radGrid_NeedDataSource" AllowPaging="True" AllowSorting="true">
  <MasterTableView DataKeyNames="ApartmentID,ApartmentTypeID">
      <CommandItemSettings ShowRefreshButton="true" ShowAddNewRecordButton="false" />
          <Columns>
           <telerik:GridTemplateColumn>
       <ItemTemplate>
       <asp:LinkButton ID="Delete" Text="Remove &raquo" 
        CommandArgument='<%# Eval("ApartmentId") %>'
         CommandName="RemoveItem" CssClass="Button" runat="server" />
          </ItemTemplate>
       </telerik:GridTemplateColumn>
        </Columns>
          <NoRecordsTemplate>
            No related items found</NoRecordsTemplate>
       </MasterTableView>
                    <FooterStyle CssClass="FooterStyle" />
                    <ItemStyle CssClass="RowStyle" />
                    <HeaderStyle CssClass="HeaderStyle" />
                    <AlternatingItemStyle CssClass="AlternatingRowStyle" />
                    <PagerStyle CssClass="PagerStyle" FirstPageText="First" LastPageText="Last" Mode="NextPrevAndNumeric"
                        AlwaysVisible="true" />
                </telerik:RadGrid>

Answer

Icarus picture Icarus · Dec 5, 2011

You have a linkbutton inside an itemtemplate, add a handler for OnClick on the linkbutton itself and the event will surely be fired.

For example:

protected void LinkButton1_Click(Object sender, EventArgs   
 {
         LinkButton button = sender as LinkButton;
          Apartments apartAdmin = new Apartment();
           bool deleted = apartAdmin.Delete(int.Parse(button.CommandArgument.ToString()); 
            if (deleted)
            {
                radGrid.Rebind();
            }

 }