0

我想使用发送到 SetSectorImage 子的 Sector 变量来命名正在更改的图像(图像确实已经存在于 Web 表单中,我只是在更改 URL)。谷歌搜索只让我在 MSDN 上找到一篇关于 CallByName 方法的文章,但我不知道它是否能在这种情况下工作,而且我不知道如何工作。

如果有帮助,这里是这篇文章:http: //msdn.microsoft.com/en-us/library/22x2chfx.aspx

Imports System
Imports System.IO

Public Class Launcher
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetSectorImage("Sector1")
        SetSectorImage("Sector2")
        SetSectorImage("Sector3")
    End Sub

    Sub SetSectorImage(Sector As String)
        Dim SectorStatus As String
        Try
            Using Reader As New StreamReader(Sector + ".txt")
                SectorStatus = Reader.ReadToEnd()
                Reader.Close()
                Sector_SHOULD_BE_USED_HERE.ImageUrl = ("~/Images/" + SectorStatus)
            End Using
        Catch ex As Exception
            ErrorMessage.Text = ("There was an error reading the status of: " + Sector)
            ErrorMessage.Visible = True
        End Try
    End Sub

End Class
4

2 回答 2

0

我对 CallByName() 做了更多研究,发现我可以在这种情况下使用它。更改图像 URL 的行将是:

CallByName(Sector, "ImageUrl", CallType.Set, "~/Images/" + SectorStatus)
于 2013-11-02T14:04:51.347 回答
0

假设您在页面上有图像:

<asp:Image ID="Sector1" />
<asp:Image ID="Sector2" />
<asp:Image ID="Sector3" />

您可以使用Control.FindControl 方法访问它们

Dim myControl1 As Control = FindControl(Sector)
Dim myImage As Image = myControl1
myImage.ImageUrl = ("~/Images/" + SectorStatus)
于 2013-11-02T19:00:31.623 回答