Add OnClick to ImageButton in code-behind
Posted on 2007-08-09
I"m writing a web page in .Net 2.0 using vb. I have an ImageButton control on the page. I'm trying to add the OnClick event in my code-behind, but am having no success.
On my page I have several controls like: <asp:ImageButton runat="server" id="lhe_image" />
I am then grabbing some information using a SQLDataSource and looping through that source to set the image of the button to either a check box or a check mark. I want a post-back event when the user clicks on the ImageButton.
In the On_Load event I have the following code which works great EXCEPT for the OnClick stuff. There are no errors thrown, it just doesn't work.
Dim vwEASYData As Data.DataView = CType(EASYData.Select(DataSourceSelectArguments.Empty), Data.DataView)
Dim ezdc As Data.DataRow
For Each ezdc In vwEASYData.Table.Rows
Dim ezimage As ImageButton = CType(EASYGrid.FindControl(Trim(ezdc.Item("easyname")) & "_image"), ImageButton)
If Not ezimage Is Nothing Then
If ezdc.Item("Responseid") = 1 Then
ezimage.ImageUrl = "img/check.gif"
ezimage.CommandName = "DeleteResponse"
ezimage.ImageUrl = "img/box.gif"
ezimage.CommandName = "InsertResponse"
ezimage.ToolTip = ezdc.Item("helptext")
ezimage.CommandArgument = ezdc.Item("easyID")
This is the line that is not working:
If I go to the page, I can set the onClick event literally, but I'd like to do this dynamically in the future, and figured there HAS to be a way. So, to be clear, explicitly setting the onClick attribute on my web page page WORKS:
<asp:ImageButton runat="server" id="lhe_image" onClick="update_EASYResponse" />