Nic*_*ing 4 c# asp.net asp.net-4.5
我们的UX团队不喜欢处理布尔值的复选框 - 他们想要一个asp:DropDownList,它有两个true/false选项.
我的bool必须使用<%#Bind("")%>进行绑定,因为它位于asp:GridView的编辑模板中.
这是我的代码:
<asp:GridView ...>
...
<Columns>
...
<asp:TemplateField ...>
...
<EditItemTemplate>
<asp:DropDownList ID="ExcludedDropDown" runat="server" SelectedValue='<%# Bind("IsExcluded") %>'>
<asp:ListItem Value="false" Text="Include" meta:resourcekey="ListItemResource0"></asp:ListItem>
<asp:ListItem Value="true" Text="Exclude" meta:resourcekey="ListItemResource1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
在EditItemTemplate中有一个断点,我试着在即时窗口中关注:
Eval("Exclude")
false
Run Code Online (Sandbox Code Playgroud)
其中"假"是Eval的结果.为了好的措施,我确实尝试将我的"真实"项目的值更改为:"True","T","t","Y","y"," - 1","0"," 1",""所有产生相同的例外:
'DropDownList'具有选定值,该值无效,因为它不存在于项列表中.
我尝试使用"OnDataBinding"事件,但这根本没有帮助我(也许我只是做错了).
我不想在我们的类中添加一个属性,将bool转换为字符串/整数(因为它可以立即工作).
将bool绑定到DropDownList是不可能的 - 如果是,为什么在ASP.NET中有这个限制是有意义的?很难看到支持整数和DropDownList中的布尔值之间的区别.
我用大写(True | False)改变了ListItem的Value start,它运行正常.你可能想尝试一下.

<asp:DropDownList ID="ExcludedDropDown" runat="server"
SelectedValue='<%# Bind("IsExcluded") %>'>
<asp:ListItem Value="True" Text="Include"></asp:ListItem>
<asp:ListItem Value="False" Text="Exclude"></asp:ListItem>
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
public class Something
{
public string Some { get; set; }
public bool IsExcluded { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var collections = new List<Something>
{
new Something {Some = "One", IsExcluded = true},
new Something {Some = "Two", IsExcluded = false},
};
GridView1.DataSource = collections;
GridView1.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud)