[英]Updating image based on drop down list selection in asp.net
I have a drop down list defined as 我有一个下拉列表定义为
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" style="width: 107px">
<asp:ListItem Selected="True" Text="Select a value" Value="select">Select a value</asp:ListItem>
<asp:ListItem Value="p1">Image 1</asp:ListItem>
<asp:ListItem Value="p2">Image 2</asp:ListItem>
<asp:ListItem Value="p3">Image 3</asp:ListItem>
</asp:DropDownList>
<div id="img">
<asp:Image ID="image1" runat="server" Width="100"/>
</div>
On changing the selection the event is defined in the code-behind as 更改选择后,事件将在后面的代码中定义为
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
string i = ddl1.SelectedValue;
switch (i)
{
case "p1":
image1.ImageUrl = "img/p1.jpg";
break;
case "p2":
image1.ImageUrl = "img/p2.jpg";
break;
case "p3":
image1.ImageUrl = "img/p3.jpg";
break;
default "select":
image1.ImageUrl = "";
break;
}
}
However, this does not seem to pick the images. 但是,这似乎没有选择图像。
.SelectedValue
is only used when list is databinded!
.SelectedValue
仅在列表已数据绑定时使用!
So this is why no image is selected.
因此,这就是为什么没有选择图像的原因。
. 。 Loooking at MSDN proofs me wrong http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx
看MSDN可以证明我错了http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx
Change it to this: 更改为此:
string i = ddl1.SelectedItem.Value;
and it will work :). 它会工作:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.