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

Check for "delete" clicked in code behind

2 Answers 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bruce
Top achievements
Rank 1
Bruce asked on 20 Mar 2009, 12:58 AM

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

My aspx page has

<telerik:RadGrid ID="radGrid" runat="server"
.......
OnDeleteCommand="RadGrid_DeleteCommand"
>

<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 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Mar 2009, 07:42 AM
Hi Bruce,

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

VB:
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 
        Try 
            Dim button As LinkButton = DirectCast(cause, LinkButton) 
            If button.Text = "Delete" AndAlso button.CommandName = "Delete" Then 
                DeleteWasClicked = True 
            End If 
        Catch 
        End Try 
        Response.Write(DeleteWasClicked) 
    End If 
End Sub 

Another approach is by using ViewState, save the deltebuttonclick status and check that in PreRender event of RadGrid.
VB:
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.

Thanks,
Shinu.
0
Bruce
Top achievements
Rank 1
answered on 20 Mar 2009, 11:56 AM
Perfect - Thanks!
Tags
Grid
Asked by
Bruce
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Bruce
Top achievements
Rank 1
Share this question
or