Search code examples
asp.netvb.netdelegatesaddhandler

Send parameter to addhandler?


I have a button in a grid that I created programmatically. The button edits some data in a table using data in a hidden column of the grid that the button is in. Normally I send a hidden field the row data using javascript onclientclick of the button then make the changes to the database using that hidden field. But there must be a way to send the addhandler of the button a parameter. This is the code i have to clarify....

Dim btnedit As New ImageButton
    AddHandler btnedit.Click, AddressOf btnedit_Click
    btnedit.ImageUrl = "\images\bttnEditMini.gif"

If e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Footer Then
        e.Row.Cells(3).Controls.Add(btnedit)
End If

here is my Addhandler with its delegate:

Public Delegate Sub ImageClickEventHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Sub btnedit_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
//programming stuff
End Sub

How can i send this handler a parameter?


Solution

  • Since it was in a grid i just used the row command instead. And when Row command is used you can send it a commandname and a commandargument. I passed my parameter as the argument.

     GridView1.Rows(i).Cells(3).Controls.Add(btndel)
                btndel.ImageUrl = "\images\bttnDelete.gif"
                btndel.ToolTip = "This will delete the Selected Assignment"
                btndel.CommandName = "destroy"
                btndel.CommandArgument = GridView1.Rows(i).Cells(0).Text
                btndel.Attributes.Add("onclick", "javascript: if(confirm('Are you sure you want to delete this Department Cost Days Assignment?')==false) return false;")
    

    here is the rowcommand:

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "destroy" Then 'used destroy because Delete command was prohibited.
           Call Connection()
            Dbcmd.CommandText = "Delete from table where condition = '" & e.CommandArgument & "'"
            Dbcmd.ExecuteNonQuery()
            Dbconn.Close()
            Dbconn.Dispose()
        End If