asp.net 在VB.Net中当有重复量时更改Image.Url

btqmn9zl  于 12个月前  发布在  .NET
关注(0)|答案(3)|浏览(150)

我有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

oewdyzsn

oewdyzsn1#

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

<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") %>'


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

sd2nnvve

sd2nnvve2#

正如建议的那样,您有重复按钮,因此可以考虑使用“重复器”(这正是重复器设计的任务类型)。
所以,说这个标记:

<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。
当我运行上面的代码时,结果是:x1c 0d1x
所以,考虑一个中继器。注意上面的代码很少,标记也很少。
对于一个给定的行(按钮)点击,我们可以轻松地获取该按钮的“索引”,因此我们的代码对于5个或25个按钮几乎相同。

vom3gejh

vom3gejh3#

您可以通过名称获取图像按钮,以摆脱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

字符串

相关问题