• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1204
  • Last Modified:

Add OnClick to ImageButton in code-behind

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"
                        Else
                            ezimage.ImageUrl = "img/box.gif"
                            ezimage.CommandName = "InsertResponse"
                        End If
                        ezimage.ToolTip = ezdc.Item("helptext")
                        ezimage.CommandArgument = ezdc.Item("easyID")
                        ezimage.Attributes.Add("onClick", "update_EASYResponse")
                    End If
                Next
This is the line that is not working:
ezimage.Attributes.Add("onClick", "update_EASYResponse")

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" />
0
Steve Krile
Asked:
Steve Krile
  • 7
  • 4
1 Solution
 
gjutrasCommented:
move your code above in to a subroutine

in your code behind you need a function like:
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)  Handles lhe_image.Click
    mySubroutine()
End Sub
The handles lhe_image.click is what causes this to be called when the button is clicked.
The attribute you added needs to be taken out.  It's purpose is to cause a javascript or vbscript function to be called, not to postback to the application server.
0
 
gjutrasCommented:
actually you already moved your code where you need to, change mySubroutine() line in my code to update_EASYResponse()
0
 
Steve KrileAuthor Commented:
Hmm.  The issue with my code is not getting the update_EASYResponse function to work.  Here it is, and when I set the OnClick event of an image button on the .aspx page, everything works as expected.:

    Protected Sub update_EASYResponse(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim cname As String
        Dim cargument As String
        Dim button As System.Web.UI.WebControls.ImageButton = DirectCast(sender, System.Web.UI.WebControls.ImageButton)
        cname = button.CommandName
        cargument = button.CommandArgument
        testlable.text = cname & "/" & cargument
End Sub

But when I use :  ezimage.Attributes.Add("onClick", "update_EASYResponse"), the resulting control (on the rendered page) looks ok, but the update_EASYResponse does not trigger.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Steve KrileAuthor Commented:
Also, I'm trying to avoid the Handles part because I will have 52 of these boxes on my page.
0
 
gjutrasCommented:
you can ad as many controls as you want to the handles phrase like the following
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)  Handles lhe_image.Click, lha_image.Click, lhb_image.Click, lhc_image.Click
    mySubroutine
End Sub
0
 
gjutrasCommented:
the attribute add adds a script handler, not a .net event handler.
0
 
gjutrasCommented:
you also could do in the Page_OnLoad event
if Page.IsPostback then
        If sender.GetType() = "System.Web.UI.WebControls.ImageButton" Then
          update_EASYResponse()
        End If
end if
0
 
gjutrasCommented:
and if you have more than those image buttons put another nested if around the update_EasyResponse()
if  CType(sender,System.Web.UI.WebControls.ImageButton).ID.IndexOf("string to search for") > -1
end if
0
 
gjutrasCommented:
lastly what you were originally asking is answered by the following
make the handles function and have it call your update function.
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
    update_EASYResponse()
End Sub
then do an addhandler for each id you need to add a handler of.
AddHandler lhe_image.Click, AddressOf ImageButton1_Click
0
 
Steve KrileAuthor Commented:
Yeah, this is great.  Thanks and I've marked your anwer.
0
 
Steve KrileAuthor Commented:
Intellisense doesn't like:

if Page.IsPostback then
        If sender.GetType() = "System.Web.UI.WebControls.ImageButton" Then
          update_EASYResponse()
        End If
end if

I've changed the sender.GetType() to :  Convert.ToString(sender.GetType()) and that seems to work, but your suggestion for:  update_EASYResponse() says Argument not specified for parameter e.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now