Tutorial :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?


By convention, all event handlers accept two parameters: the sender, and the EventArgs. If you need to send custom information to the listeners, create a new class that inherits from EventArgs and contains the information that you need to communicate.

Check out this article on CodeProject that shows you how to do this.


Short answer: no. Where would you send it? You've got two parameters.

Longer answer: sender is the control that sent the event. In this case, it will be your btnEdit control. Maybe that will help you.


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  


If you don't want to use the 'default'/already defined parameters, you can create your own events.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »