[英]How to get converted boolean value to show as selected value in the editable drop down list in asp.net gridview?
I have a database flag, ActiveFlag, that I show as Yes/No in the gridview row. 我有一个数据库标志ActiveFlag,它在gridview行中显示为Yes / No。 On edit, a drop down list that presents the options is displayed, however it always shows Yes even if the current value is No. I'm sure the problem is that the code I have to show the selected item is not working, but I'm unsure of how to fix it. 在编辑时,会显示一个显示选项的下拉列表,但是即使当前值为“否”,它也会始终显示“是”。我确定问题是我必须显示所选项目的代码无法正常工作,但是我我不确定如何解决它。
<asp:TemplateField HeaderText="Active" SortExpression="ActiveFlag"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# (Boolean.Parse(Eval("ActiveFlag").ToString()))? "Yes" : "No" %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlActive" runat="server"
Selected='<%# (Boolean.Parse(Eval("ActiveFlag").ToString()))? "Yes" : "No" %>' >
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="0" />
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
You can use DropDownList's SelectedValue . 您可以使用DropDownList的SelectedValue 。
Note : Make sure conditional operator are ListItems' values. 注意 :确保条件运算符是ListItems的值。 In your code, they are "1" and "0". 在您的代码中,它们是“ 1”和“ 0”。
<asp:DropDownList ID="ddlActive" runat="server"
SelectedValue='<%# Convert.ToBoolean(Eval("ActiveFlag"))? "1" : "0" %>'>
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="0" />
</asp:DropDownList>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.