samedi 25 novembre 2023

Show image using a random number in code behind

I tried this and it is not working for me. I have 14 images (7 of each appearing side by side-acctimg1.jpeg, acctimg2.jpeg, etc.) The images reside in the images folder under templates. This is the Home.master.vb page in the templates folder. If I hard code the path on the aspx page as templates/images/acctimg1.jpeg the images appear. I have put a breakpoint and looked at the urls created and they appear correct. Does this use of random number to create a url need to be in a pre render sub instead of page load?

 <table align="center">
                        <tr>
                            <td align="left">
                                <asp:Image ID="acctimage" runat="server" Width="275" Height="220"  /></td>
                            <td align="right">
                                <asp:Image ID="taximage" runat="server" Width="275" Height="220" /></td>
                        </tr>
                    </table>



 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim a As Int16
    a = Int((7 * Rnd()) + 1)
    acctimage.ImageUrl = "templates/images/acctimg" + Convert.ToString(a) + ".jpeg"
    Dim b As Int16
    b = Int((7 * Rnd()) + 1)
    taximage.ImageUrl = "templates/images/tax" + Convert.ToString(b) + ".jpeg"
End Sub



Aucun commentaire:

Enregistrer un commentaire