Check for "delete" clicked in code behind

3 posts, 0 answers
  1. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 19 Mar 2009 Link to this post

    How can I determine if a "delete" links was clicked?

    My aspx page has

    <telerik:RadGrid ID="radGrid" runat="server"

    <telerik:GridButtonColumn CommandName="Delete" Text="Delete"
    UniqueName= "DeleteColumn"></telerik:GridButtonColumn>

    In my codebehind I want to check to see if the delete was clicked:

    Dim DeleteWasClicked as boolean

    If IsPostback() then
       DeleteWasClicked = ????
    end if

    ' I ahso have the follwing to delete the record (which works fine)

    Protected Sub RadGrid_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
    'Get the GridDataItem of the RadGrid

    Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
    'Get the primary key value using the DataKeyValue.

    Dim PrmaryKeyID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("CustomerID").ToString()

    Dim deleteQuery As String = "DELETE from "....
    ' Run the query

    End Sub

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Mar 2009 Link to this post

    Hi Bruce,

    I tried following code snippet to get the control which causes the PostBack and checked whether its the Delete button in RadGrid.

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) 
        Dim postbackControlInstance As Control = Nothing 
        If IsPostBack Then 
            Dim DeleteWasClicked As Boolean = False 
            Dim postbackControlName As String = Page.Request.Params.[Get]("__EVENTTARGET"
            If postbackControlName IsNot Nothing AndAlso postbackControlName <> String.Empty Then 
                postbackControlInstance = Page.FindControl(postbackControlName) 
            End If 
            Dim cause As Control = postbackControlInstance 
                Dim button As LinkButton = DirectCast(cause, LinkButton) 
                If button.Text = "Delete" AndAlso button.CommandName = "Delete" Then 
                    DeleteWasClicked = True 
                End If 
            End Try 
        End If 
    End Sub 

    Another approach is by using ViewState, save the deltebuttonclick status and check that in PreRender event of RadGrid.
    Protected Sub RadGrid1_DeleteCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
             . . . 
          ViewState("deleteclicked") = "delete" 
    End Sub 
    Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As EventArgs) 
        If IsPostBack AndAlso ViewState("deleteclicked") = "delete" Then 
            Response.Write("delete clicked"
        End If 
    End Sub 
    Hope this helps.

  3. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 20 Mar 2009 Link to this post

    Perfect - Thanks!
Back to Top