Dynamic ImageButton EventHandler in ASP.Net

TheWizardofInt

Junior Contributor
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'we want to be situationally dependent on which buttons we show
        Dim glob As globVars
        Dim but As System.Web.UI.WebControls.ImageButton
        Dim tblRow As TableRow
        Dim tblCell As TableCell
        Dim sImageUrl As String                     'this is the image url based on the contents of the folder

        glob = globVars.GetInstance

        sImageUrl = "~/images/" & glob.sImageFolder & "/buttons/"

        'go through each of the possible buttons in the table
        'if it is visible, add a row to the table and add a button for the row, in its only cell
        With tblButtons
            If glob.butHome.bVisible = True Then
                but = New System.Web.UI.WebControls.ImageButton
                but = BuildButton(sImageUrl & "N-Home.gif", "btnHome", glob.butHome.sReDirect)
                tblCell = New TableCell
                tblCell.Controls.Add(but)
                tblRow = New TableRow
                tblRow.Cells.Add(tblCell)
                tblButtons.Rows.Add(tblRow)
            End If
        End With

    End Sub
    Private Function BuildButton(ByVal sImageUrl As String, ByVal sID As String, ByVal sReDirect As String) As ImageButton

        Dim but As New System.Web.UI.WebControls.ImageButton

        sReDirect = "<script language='JavaScript'>window.open('" & sReDirect & "')</script>"
        With but
            .ID = sID
            .ImageUrl = sImageUrl
            .ToolTip = "Hello"
            .CommandName = "onClick"
            .CommandArgument = "alert('Bite me')"
            .EnableViewState = True
            '.Attributes.Add("onClick", sReDirect)

            AddHandler .Click, AddressOf imgButton_Click
        End With
      

        Return but
    End Function
    Private Sub imgButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        
        Response.Write("The button was clicked")
    End Sub

Doesn't work

It creates the table, the row, the cell and the button, but clicking on the button does nothing.

I am about to use an image instead and wrap it with <a href=></a>, but wanted to see if I left something out first
 
Not sure what is happening with
Visual Basic:
but = New System.Web.UI.WebControls.ImageButton
but = BuildButton(sImageUrl & "N-Home.gif", "btnHome", glob.butHome.sReDirect)
but the first of those two lines is redundant as you never use the instance of ImageButton that it creates.

If you put a breakpoint on the event handler is it ever reached?
 
Back
Top