使用下拉列表从文件夹中获取图像名称并将其应用于图像URL

我正在尝试应用图片名称,但图片未加载。

if (!IsPostBack)
{
    string[] str = new string[] { "Select","Anniversary","Birthday","Graduation","Sympathy" };
    for (int i = 0; i < str.Length; i++)
    {
        DDl1.Items.Add(str[i]);
    }  // this Loop creates the items in the drop down list

    {
        string str = DDl1.Text;
        Image1.ImageUrl="~/Media"+str+".png";
    } // This is how i was trying to call the images from the Media folder in my project
A008888 回答:使用下拉列表从文件夹中获取图像名称并将其应用于图像URL

更改此

Image1.ImageUrl="~/Media"+str+".png";

Image1.ImageUrl="~/Media/"+str+".png";
,

在您的OnSelectedIndexChanged上创建一个事件<asp:DropDownList>,然后添加AutoPostBack="True"

在事件OnSelectedIndexChanged后的代码中,将代码如下:

string str = DDl1.Text;string str = DDl1.SelectedText;

Image1.ImageUrl="~/Media/"+str+".png";

我希望这会对您有所帮助。

本文链接:https://www.f2er.com/3169758.html

大家都在问