我有30个图像按钮,这里显示的只是一个小片段。我需要从VB代码中更改Image.Url
,因为我从数据库中读取。你可以从下面的代码中看到我正在尝试做什么,但它非常重复。有没有办法我可以缩短这一切?
前端
<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
型
3条答案
按热度按时间oewdyzsn1#
您可以使用Repeater来创建多个ImageStream,只需查看文档
字符串
然后你需要替换Id,CommandArgument和ImageURL,如下所示
型
请记住,将图像保存在数据库中会占用大量的存储空间。不要将它们存储在数据库中,只需将文件位置详细信息存储在数据库中。
sd2nnvve2#
正如建议的那样,您有重复按钮,因此可以考虑使用“重复器”(这正是重复器设计的任务类型)。
所以,说这个标记:
字符串
所以,让我们有代码后面饲料的“表”的10件事以上。
所以,后面的代码是这样的:
型
请注意上面的代码是多么简单,在我的例子中,我的图像名为b1-b10。
当我运行上面的代码时,结果是:x1c 0d1x
所以,考虑一个中继器。注意上面的代码很少,标记也很少。
对于一个给定的行(按钮)点击,我们可以轻松地获取该按钮的“索引”,因此我们的代码对于5个或25个按钮几乎相同。
vom3gejh3#
您可以通过名称获取图像按钮,以摆脱Select Case语句
字符串