简体   繁体   中英

How to get RadioButtonList ListItems to display text in gridview

The Code Code below is the section that binds to my asp.net gridview. But I can't get none of the ListItems to display text values even when using Text="Male" or Text="Female". 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. When grid goes into edit mode then control present in EditItemTemplate would be visible. There you could change the value. The changing should be saved in the datasource by handling gridview rowupdating event.

Label could be use to display readonly text in the ItemTemplate . In the Text property of Label you could directly refer to column which gives text either Male or Female .

Èxample would be something like that:

<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

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM