简体   繁体   English

如何获取转换后的布尔值以在asp.net gridview的可编辑下拉列表中显示为选定值?

[英]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.

相关问题 如何从下拉列表中显示所选值并将其显示在gridview中 - How to show the selected value from the drop down list and show it in gridview 如何在asp.net mvc3中获取下拉列表的选定值? - how to get selected value of drop down list in asp.net mvc3? asp.net:设置为下拉列表选择的默认值 - asp.net : set default value selected for a drop down list 无法在asp.net的下拉列表中显示选定的值 - Not able to display selected value in drop down list in asp.net ASP.NET 代码隐藏无法获取选定的下拉列表值(设置 Javascript 属性后) - ASP.NET codebehind can't get selected drop down list value (after set Javascript attribute) 如何从gridview ASP.NET中的selectedrow获取布尔值 - How to get boolean value from selectedrow in gridview ASP.NET 获取下拉列表用于在ASP.NET MVC中显示选定的值 - Get the drop downListFor show the selected value in asp.net mvc ASP.NET MVC 2-如何从下拉菜单中获取选定的值? - ASP.NET MVC 2 - How can I get the selected value from a drop down into my view model? GridView Asp .Net-根据同一列中第一个下拉列表的值更改第二个下拉列表的选定值 - GridView Asp .Net-Changing the selected value of a second drop down list based on value of first drop Down in same column 如何根据ASp.Net Gridview的Selected列填充下拉列表? - How to populate Drop down list depending on the Selected column of a ASp.Net Gridview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM