软件: Visual Web Developer 2008 Express Edition
预订:通过O' REILLY编程ASP.NET 3.5
这是书中的代码
<form id="form1" runat="server">
<div>
<p>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Image ID="img1" runat="server"
AlternateText="Popfly Duck" ImageUrl="Images/ducky.jpg" />
This is a sample paragraph which is being used to demonstrate
the effects of various values of ImageAlign. As you will see,
the effects are sometimes difficult to pin down, and vary
depending on the width of the browser window.
</p>
<hr />
<asp:Button ID="Button1" runat="server" Text="Sample Button" />
<asp:Image ID="img2" runat="server"
AlternateText="Popfly Duck" ImageUrl="Images/ducky.jpg" />
<hr />
<asp:DropDownList ID="ddlAlign" runat="server" AutoPostBack="True">
<asp:ListItem Text="NotSet" />
<asp:ListItem Text="AbsBottom" />
<asp:ListItem Text="AbsBiddle" />
<asp:ListItem Text="Top" />
<asp:ListItem Text="Bottom" />
<asp:ListItem Text="BaseLine" />
<asp:ListItem Text="TextTop" />
<asp:ListItem Text="Left" />
<asp:ListItem Text="Right" />
</asp:DropDownList>
</div>
</form>
以下是生成的网页
但这本书并没有给出页面后面的代码,即aspx.cs,因此我试图确定正确的代码,因此当用户从下拉列表中选择时,文本将相应地对齐。当我尝试
protected void ddlAlign_SelectedIndexChanged(object sender, EventArgs e)
{
img1.ImageAlign = ddlAlign.Text;
}
我在ddlAlign.Text下得到了红色的波浪线。救命啊!
答案 0 :(得分:1)
ImageAlign
是enum
。您无法使用string
分配它,因此您必须先解析它。 Enum.Parse
可以为您做到这一点:
img1.ImageAlign = (ImageAlign)Enum.Parse(typeof(ImageAlign), ddlAlign.Text);