猿问

如何在 asp.net Visual Studio 中以 Web 表单显示图像?

我在我的根项目文件夹中创建了一个图像文件夹


<asp:Image ID="Image1" runat="server" ImageUrl="~/images/" />

我在这里链接我的图片:


        if (dropDownList.SelectedItem.Value == "Picture 1")

        {

            Image1.ImageUrl = "~/images/picture1.jpg"

        }

当我访问网页时,我得到一个带有 x 的小 img 框,而不是我的图像。


弑天下
浏览 182回答 3
3回答

慕田峪4524236

<asp:Image ID="Image1" runat="server" ImageUrl="~/images/" />&nbsp;这行代码设置了一个无效的图像 url,因为它只包含文件夹路径。因此,在您的代码中,您必须确保将 Image1 的 ImageUrl 属性覆盖为有效的图像文件。根据您的要求,这里是您可以做的事情。在 aspx 页面中,假设在下拉列表中默认选择 option1,将图像 url 设置为 picture1.jpg,因此在初始页面加载时会显示 picture1.jpg。<asp:Image ID="Image1" runat="server" ImageUrl="~/images/picture1.jpg" />&nbsp;接下来,将下拉列表的 AutoPostBack 属性设置为 true,以便可以根据下拉选择的值动态更新图像源代码&nbsp;<asp:DropDownList&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ID="DropDownList1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;runat="server"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AutoPostBack="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>在 selectedIndexChanged 事件处理程序中,根据 selectedItem 更新图像源&nbsp;protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Image1.ImageUrl = "~/images/" + DropDownList1.SelectedItem.Value;&nbsp; &nbsp; }希望这可以帮助

一只斗牛犬

<asp:Image&nbsp;ID="Image1"&nbsp;runat="server"&nbsp;ImageUrl="~/images/"&nbsp;/>正在将 url 设置为目录(文件夹),而不是图像。这就是为什么你得到的是小图像框而不是图像。如果您希望在页面加载时显示图像,请将其设置为有效图像:<asp:Image&nbsp;ID="Image1"&nbsp;runat="server"&nbsp;ImageUrl="~/images/picture1.jpg"&nbsp;/>

缥缈止盈

它显示带有 X 的小图像框,因为它无法在指定的路径中找到图像。因此,在wwwroot文件夹而不是项目根目录中添加您的图像文件夹。之后你可以使用&nbsp;<asp:Image ID="Image1" runat="server" ImageUrl="~/images/picture1.jpg" />
随时随地看视频慕课网APP
我要回答