[英]How to get RadioButtonList ListItems to display text in gridview
The Code Code below is the section that binds to my asp.net gridview. 下面的代码是绑定到我的asp.net gridview的部分。 But I can't get none of the ListItems to display text values even when using Text="Male" or Text="Female".
但是,即使使用Text =“ Male”或Text =“ Female”,我也无法让ListItems显示文本值。 The whole grid is bound to a List of objects that a populate from a database.
整个网格绑定到从数据库中填充的对象列表。 And each grid column is bound to a datacolumn from the database.
每个网格列都绑定到数据库中的一个数据列。 Any help would be much appreciated.
任何帮助将非常感激。
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:RadioButtonList ID="rlGender" runat="server" RepeatDirection="Vertical" SelectedValue='<%# bool.Parse(Eval("GenderS").ToString()) %>'><asp:ListItem Value="True" >Male</asp:ListItem><asp:ListItem Value="False">Female</asp:ListItem></asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:RadioButtonList ID="rlGender" runat="server" RepeatDirection="Vertical" SelectedValue='<%# bool.Parse(Eval("GenderS").ToString()) %>'><asp:ListItem Value="True">Male</asp:ListItem><asp:ListItem Value="False">Female</asp:ListItem></asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
In ItemTemplate better to keep value in control which doesn't require editing. 在ItemTemplate中,最好将值保留在不需要编辑的控件中。 When grid goes into edit mode then control present in EditItemTemplate would be visible.
当网格进入编辑模式时, EditItemTemplate中存在的控件将可见。 There you could change the value.
在那里您可以更改值。 The changing should be saved in the datasource by handling gridview rowupdating event.
更改应通过处理gridview rowupdating事件保存在数据源中。
Label
could be use to display readonly text in the ItemTemplate
. Label
可用于在ItemTemplate
显示只读文本。 In the Text
property of Label you could directly refer to column which gives text either Male
or Female
. 在Label的
Text
属性中,您可以直接引用给出了Male
或Female
文本的列。
Èxample would be something like that: Èxample就是这样:
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:RadioButtonList ID="rlGender" runat="server" RepeatDirection="Vertical" SelectedValue='<%# bool.Parse(Eval("GenderS").ToString()) %>'>
<asp:ListItem Value="True" Text = "Male" />
<asp:ListItem Value="False" Text="Female" />
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGender" runat="server" Text='<%# Eval("GenderS").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
or use ItemTemplate to always display RadioButtonList
或使用ItemTemplate始终显示
RadioButtonList
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:RadioButtonList ID="rlGender" runat="server" RepeatDirection="Vertical" SelectedValue='<%# bool.Parse(Eval("GenderS").ToString()) %>'>
<asp:ListItem Value="True" Text = "Male" />
<asp:ListItem Value="False" Text="Female" />
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.