碰巧我必须使用一些旧的WebForms代码,它需要紧急重构。 我以前只和MVC合作过,所以你了解我。
让我们想象一下我有一个模特:
public class Guy
{
public int Age { get; set; }
public string Name { get; set; }
public T Personality;
}
我需要显示一个Guys列表,我使用绑定:
<asp:ListView ID="GuysListView" runat="server" ItemType="Guy"SelectMethod="SeedGuys">
<ItemTemplate>
<asp:Label ID="Age" runat="server" Text='<%# Item.Age%>' />
<asp:Label ID="Name " runat="server" Text='<%# Item.Name %>' />
<Third control/>
</ItemTemplate>
让我们想象一下Personality属性可能就像这样
public class Logician: Personality
{
public bool IsLogic(get; set;)
}
或
public class Advanturer: Personality
{
public string SomeText(get; set;)
}
对于我的家伙,如果Personality属性类型是Logician我想要第三个控件是#34; asp:checkBox&#34;,如果Advanturer我想成为&#34; asp:TextBox&#34; 。我也希望有绑定等优点。
我的问题是,实现这种行为的好方法是什么,根据模型的属性类型进行适当的控制? Thx提前!
答案 0 :(得分:0)
处理GuysListView_ItemDataBound事件。
在那里应用你的逻辑。
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var guy = e.Item.DataItem as Guy;
if (guy.Personality is Logician)
{
e.Item.FindControl("checkBoxControlId").Visible = true;
}
else
{
e.Item.FindControl("textBoxControlId").Visible = true;
}
}