我希望在DropDownList项和可变图像之间建立一个链接,以便当我选择“背景图像”(我有一个具有多个名称的背景图像)作为花卉时,花的照片就会出现在图像中。
当我选择“背景图像”作为瓦伦丁时,图像将更改为情人节照片。
http://www.mediafire.com/view/xdyp45iqdva8011/2.png http://www.mediafire.com/view/dnvrxj569x92jyg/3.png
发布于 2014-06-17 13:56:19
Windows窗体:
下面是如何使用一个ComboBox和一个PictureBox来完成这个任务:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.Equals("Flowers"))
pictureBox1.Image = Image.FromFile("<path to Flower's picture>");
else if (comboBox1.SelectedItem.Equals("Valentine"))
pictureBox1.Image = Image.FromFile("<path to Valentine's picture>");
}编辑:
ASP.NET:
在ASP.NET应用程序中也可以这样做,方法是将下拉列表选项的值设置为您希望它们中的每个图像的URL,并将这些值作为源传递到图像中。
这里是如何。在.aspx源代码中添加以下内容:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" Height="20px" Width="200px">
<asp:ListItem Value="<Place your image URL here>">Item 1</asp:ListItem>
<asp:ListItem Value="<Place your image URL here>">Item 2</asp:ListItem>
<asp:ListItem Value="<Place your image URL here>">Item 3</asp:ListItem>
</asp:DropDownList>
<asp:Image ID="Image1" runat="server" Height="400px" Width="550px" />并将其添加到.aspx.cs代码中:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs
{
Image image = this.Image1;
image.ImageUrl = ((DropDownList)sender).SelectedValue;
}这将根据下拉列表中选择的值更改图像。
发布于 2014-06-17 13:56:24
可以使用Dictionary<string, string>在选定项和图像源之间链接。
例如:
public ActionResult Index(string selectedValue)
{
var imageBySelection = new Dictionary<string, string>();
imageBySelection.Add("Flowers", "http://www.mediafire.com/view/xdyp45iqdva8011/2.png");
imageBySelection.Add("Valentine", "http://www.mediafire.com/view/dnvrxj569x92jyg/3.png");
return View(imageBySelection[selectedValue]);
}考虑到:
@model string
<img src="@Model" />https://stackoverflow.com/questions/24265332
复制相似问题