vendredi 20 novembre 2020

Dynamically generated image button not working asp.net web app

I am using asp.net Web Application in Visual basic. I want to make iframe change source when a dynamically generated image button is pressed. When I click normal button the code is executed, while when I want to execute code with image button it does not execute. Why? I used a lot of codes wound online but none of them worked.

HTML
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="index.aspx.vb" Inherits="WebApp.index" %>


<!DOCTYPE html>


<html xmlns="https://www.w3.org/1999/xhtml">;
<head runat="server">
    <title>WebApp</title>
    <script language="VB" runat="server">
        Dim connection As New System.Data.SqlClient.SqlConnection("Data Source=myserver;Initial Catalog=mycatalog;Persist Security Info=True;User ID=Guest")
        Dim table1 As System.Data.DataTable
        Dim id As Int16
        Overloads Sub showproduct(ByVal sender As Object, ByVal e As ImageClickEventArgs)
            MatchesCounter.Text = "Test"
        End Sub


   </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Name</td>
                    <td><asp:TextBox ID="nameButton" runat="server"></asp:TextBox></td>
                </tr>
            </table>
            <asp:Button ID="SearchButton" runat="server" Text="Search" />
        </div>
        <asp:Label ID="MatchesCounter" runat="server"></asp:Label>
        <br />
        <asp:Table ID="ProductList" runat="server">
            <asp:TableRow>
                <asp:TableCell></asp:TableCell>
                <asp:TableCell>Name</asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </form>
    <iframe name="VideoFrame" id="VideoFrame" accesskey="VideoFrame" class="VideoFrame" runat="server"></iframe>
</body>
</html>

VB

Protected Sub Search_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        search()
    End Sub
    Private Sub search()
        Dim command As New SqlCommand("select top 20 ID, [Name], MainPicture from File1 where [Name] like '%" + nameButton.Text + "%' and Type = 'M' and Visible='1'", connection)
        Dim adapter As New SqlDataAdapter(command)
        Dim table As New DataTable()
        adapter.Fill(table)
        table1 = table
        MatchesCounter.Text = "Found: " + table.Rows.Count.ToString
        Dim top As Short = 0
        For Each product In table.Rows
            Dim row As TableRow = New TableRow()
            Dim p As ImageButton = New ImageButton
            p.ID = top.ToString
            p.ImageUrl = table.Rows(top)(2)
            p.Height = 170
            AddHandler p.Click, AddressOf Me.showproduct
            Dim cell1 As TableCell = New TableCell()
            cell1.Controls.Add(p)
            Dim cell2 As TableCell = New TableCell()
            cell2.Text = product(1).ToString
            row.Cells.Add(cell1)
            row.Cells.Add(cell2)
            ProductList.Rows.Add(row)
            top += 1
        Next
    End Sub
    Protected Sub showproduct(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        ClientScript.RegisterStartupScript(Me.GetType, "ImageButton_Alert", "alert('ImageButton clicked!');", True)
    End Sub

Aucun commentaire:

Enregistrer un commentaire