I have 30 image buttons, shown here is just a small snippet. I need to change the Image.Url
from VB code as I read from a database. You can see from the code below what I'm trying to do, but its very repetitive. Is there a way I can shorten all this?
Frontend
<asp:ImageButton ID="box1" alt="Add To Watch List" runat="server" CommandArgument = "1" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box1.gif" />
<asp:ImageButton ID="box2" alt="Add To Watch List" runat="server" CommandArgument = "2" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box2.gif" />
<asp:ImageButton ID="box3" alt="Add To Watch List" runat="server" CommandArgument = "3" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box3.gif" />
<asp:ImageButton ID="box4" alt="Add To Watch List" runat="server" CommandArgument = "4" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box4.gif" />
<asp:ImageButton ID="box5" alt="Add To Watch List" runat="server" CommandArgument = "5" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box5.gif" />
<br />
<asp:ImageButton ID="box6" alt="Add To Watch List" runat="server" CommandArgument = "6" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box6.gif" />
<asp:ImageButton ID="box7" alt="Add To Watch List" runat="server" CommandArgument = "7" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box7.gif" />
<asp:ImageButton ID="box8" alt="Add To Watch List" runat="server" CommandArgument = "8" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box8.gif" />
<asp:ImageButton ID="box9" alt="Add To Watch List" runat="server" CommandArgument = "9" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box9.gif" />
<asp:ImageButton ID="box10" alt="Add To Watch List" runat="server" CommandArgument = "10" class="BoxPriceMenu" OnClick="Box_Click" ImageUrl="~/files/images/icons/boxesFooty/box10.gif" />
VB.Net
'*** SELECT CARD ENTRANTS ***
Dim DBConnect91 As New DBConn
Using db As DbConnection = DBConnect91.Conn("DBConnectionString")
Dim cmd As SqlCommand = DBConnect91.Command(db, "SelectEntrants")
cmd.Parameters.Add(New SqlParameter("fileID", SqlDbType.Int, ParameterDirection.Input)).Value = Request.QueryString("oid")
db.Open()
Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While DR.Read
session("accountID") = DR("accountID")
session("boxNo") = DR("boxNo").ToString
If session("accountID") = acc.accountID Then
Select session("boxNo")
Case "1"
box1.ImageUrl="~/files/images/icons/boxesFooty/selectedbox1.gif"
Case "2"
box2.ImageUrl="~/files/images/icons/boxesFooty/selectedbox2.gif"
Case "3"
box3.ImageUrl="~/files/images/icons/boxesFooty/selectedbox3.gif"
Case Else
End Select
End If
End While
DR.Close()
DR = Nothing
cmd.Dispose()
cmd = Nothing
db.Dispose()
db.Close()
End Using
Protected Sub Box_Click(sender As Object, e As System.EventArgs)
Dim btn As ImageButton = CType(sender, ImageButton)
Dim boxNo As Integer = btn.CommandArgument
Dim acc As New accounts(Membership.GetUser.ProviderUserKey)
Dim DBConnect As New DBConn
Using db As DbConnection = DBConnect.Conn("DBConnectionString")
Dim cmd As SqlCommand = DBConnect.Command(db, "addEntrant")
cmd.Parameters.Add(New SqlParameter("accountID", SqlDbType.UniqueIdentifier, ParameterDirection.Input)).Value = acc.accountID
cmd.Parameters.Add(New SqlParameter("fileID", SqlDbType.Int, ParameterDirection.Input)).Value = Request.QueryString("oid")
cmd.Parameters.Add(New SqlParameter("boxNo", SqlDbType.Int, ParameterDirection.Input)).Value = boxNo
db.Open()
cmd.ExecuteNonQuery()
cmd.Dispose()
cmd = Nothing
db.Dispose()
db.Close()
End Using
End Sub
You could use a Repeater to create multiple ImageButtons, just check the docs
then you will need to replace the Id, CommandArgument and ImageURL with something like this
And remember, saving images inside the database will be a considerable amount of storage. Don't store them in a database just store the file location details in the database.