Using a Delete Command with RowClick enabled.

5 posts, 0 answers
  1. Joshua Horton
    Joshua Horton avatar
    4 posts
    Member since:
    Aug 2008

    Posted 13 Feb 2009 Link to this post

    I have a grid that will display items and when you click on a row it's designed to redirect you to another page where you can edit that items data. My problem is that there is also a delete button that is to be in the row as well and when you click the delete button it fires the OnItemCommand event first which redirects before the delete can occur. So I'm looking for some direction here. If anyone can help I would greatly appreciate it.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Feb 2009 Link to this post

    Hi Joshua Horton,

    You can check the CommandName in ItemCommand event handler whether it is RowClick or Delete and execute the corresponding code for that (if you are using any template column for delete button, then you can set the commandname property to "Delete" and continue to the below steps for checking that). See the example below. I hope this will help you.

    CS:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)     
    {     
        if (e.CommandName == "RowClick")     
        {     
            //Code for Redirect     
        }     
        if (e.CommandName == "Delete")     
        {     
            // Code for Delete     
        }     

    Thanks,
    Princy.
  3. Joshua Horton
    Joshua Horton avatar
    4 posts
    Member since:
    Aug 2008

    Posted 16 Feb 2009 Link to this post

    I currently have it set up like that. The problem is though that the RowClick command is fired first and then the Delete command is fired second. So the page is always redirected before the Delete command is reached in the switch statement. I'm guessing this is cause the client side row click event fires a post back along with the delete button.

    One solution I am trying is to use the column mouse over client side event to set a variable showing what column the mouse is over when the client side row click happens. If it is over the Delete column then it cancels the ajax request. But there is some issues I'm working on for this.

    If anyone else can suggest a more efficient and easier method I would greatly appreciate it. This will have to be used on multiple grids across the app.
  4. Nour
    Nour avatar
    1 posts
    Member since:
    Oct 2017

    Posted 21 Sep 2020 in reply to Joshua Horton Link to this post

    were you able to resolve this , i am running into the same scenario and any suggestions will be helpful
  5. Attila Antal
    Admin
    Attila Antal avatar
    588 posts

    Posted 24 Sep 2020 Link to this post

    Hi Nour,

    The code shared by Princy is working as intended, if this is not working for you, then there must be something else you are doing. You could share some source code with us and we will take a look why this could happen.

    Here is a demo sample of Princy's approach:

    Add the following Markup to an ASPX page:

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand" AutoGenerateDeleteColumn="true">
        <ClientSettings EnablePostBackOnRowClick="true"></ClientSettings>
    </telerik:RadGrid>
    
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

     

    Add the following C# code in the aspx.cs file:

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = Enumerable.Range(1, 2).Select(x => new
        {
            ID = x,
            Desc = "Description " + x
        });
    }
    
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            // Only the Delete button click is considered
            Label1.Text += "Delete button was clicked <br />";
        }
        else if (e.CommandName == "RowClick")
        {
            // Only the RowClick is considered
            Label1.Text += "Row was clicked <br />";
        }
    }

     

    Results

     

    Regards,
    Attila Antal
    Progress Telerik

    Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Back to Top