当有重复量时更改VB.Net中的Image.Url

问题描述 投票:0回答:3

我有 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
asp.net sql-server vb.net imagebutton commandargument
3个回答
1
投票

您可以使用 Repeater 创建多个 ImageButton,只需查看文档

     <asp:Repeater id="Repeater1" runat="server">             
          <ItemTemplate>
               <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" />
          </ItemTemplate>         
      </asp:Repeater>

然后你需要将 Id、CommandArgument 和 ImageURL 替换为类似这样的内容

 Id='<%# Eval("Id") %>'

请记住,将图像保存在数据库中将占用大量存储空间。不要将它们存储在数据库中,只需将文件位置详细信息存储在数据库中。


0
投票

您可以通过名称获取图像按钮以摆脱 Select Case 语句

While DR.Read()
    Dim accountId = DR("accountID")
    Dim boxNo = DR("boxNo").ToString()
    session("accountID") = accountID
    session("boxNo") = boxNo

    If accountID = acc.accountID Then
        Dim box = CType(FindControl("box" & boxNo), ImageButton)
        box.ImageUrl = $"~/files/images/icons/boxesFooty/selectedbox{boxNo}.gif"
    End If
End While

0
投票

按照建议,您有重复按钮,因此请考虑“中继器”(这正是中继器设计的任务类型)。

所以,说出这个标记:

        <asp:Repeater ID="Repeater1" runat="server"
            OnItemDataBound="Repeater1_ItemDataBound" >
            <ItemTemplate>
                <asp:ImageButton ID="mybox" alt="Add To Watch List" runat="server"
                    class="BoxPriceMenu"
                    OnClick="mybox_Click"
                    CommandArgument="<%# Container.ItemIndex %>"
                    style="margin-left:25px;width:128px"
                  />
            </ItemTemplate>
        </asp:Repeater>
        <br />
        <h1 id="ShowChoice" runat="server"></h1>

所以,让我们在后面的代码中向上面提供一个包含 10 件事的“表”。

所以,背后的代码是这样的:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadData()
    End If

End Sub

Sub LoadData()

    Dim rstData As New DataTable
    rstData.Columns.Add("Title")
    For i = 1 To 10
        Dim MyNewRow As DataRow = rstData.NewRow
        MyNewRow("Title") = $"Button {i}"

        rstData.Rows.Add(MyNewRow) ' add this row to table
    Next

    ' now bind the data table to repeater to repeat over and over

    Repeater1.DataSource = rstData
    Repeater1.DataBind()


End Sub


Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)

    ' this code runs DURING data binding and is triggered for each row of the repeater 
    ' During the binding process. A great place to set the image for each button


    If e.Item.ItemType = ListItemType.Item Or
            e.Item.ItemType = ListItemType.AlternatingItem Then

        ' set the URL for this button image

        Dim iRowIndex As Integer = e.Item.ItemIndex + 1 ' starts at 0, we start at 1
        Dim sURL As String = $"~/Content/Balls/b{iRowIndex}.png"
        Dim OneBut As ImageButton = e.Item.FindControl("mybox")
        OneBut.ImageUrl = sURL


    End If

End Sub

Protected Sub mybox_Click(sender As Object, e As ImageClickEventArgs)

    Dim MyImageBut As ImageButton = sender
    Dim MyRow As RepeaterItem = MyImageBut.NamingContainer

    Debug.Print($"Row click (zero based) = {MyRow.ItemIndex}")

    ShowChoice.InnerText = $"Button ball click = {MyRow.ItemIndex + 1}"


End Sub

注意上面的代码是多么简单。就我而言,我的图像名为 b1-b10。

当我运行上面的命令时,结果是这样的:

所以,考虑一个中继器。现在上面的代码非常少,实际上我们的标记也非常少。

而且,对于给定的行单击,我们可以做我们想做的事情,并且代码与 5 个或 40 个按钮相同。

© www.soinside.com 2019 - 2024. All rights reserved.