views:

474

answers:

3

Background: I am using ASP.NET 2.0 (with C#) and the code below is embedded in a DataGrid control. I have the following in my .aspx file:

  <ASP:TEMPLATECOLUMN HeaderText="Includes CRS Statement?">  
   <ITEMTEMPLATE>
    <asp:RadioButtonList id="rblSCIncludesCRSStatement" runat="server" RepeatDirection="Horizontal"
    SelectedIndex='<%# Convert.ToInt32(DataBinder.Eval(Container, "DataItem.CRS_Included")) %>'
    DataValueField="CRS_Included" RepeatLayout="Flow">
         <asp:ListItem value="true" selected="true">Yes</asp:ListItem>
         <asp:ListItem value="false">No</asp:ListItem>
         </asp:RadioButtonList>
   </ITEMTEMPLATE>
  </ASP:TEMPLATECOLUMN>
  <ASP:BOUNDCOLUMN visible="false" HeaderText="IncludesCRSStatement" DataField="CRS_Included"></ASP:BOUNDCOLUMN>

It is supposed to bind the boolean value CRS_Included with the RadioButtonList. It works, but in reverse order. Yes is turned to no, no is turned to yes, and the only way I can see to fix it is swap the order of the ListItems, which would be counterintuitive (Radio buttons shouldn't start like No/Yes, it needs to be Yes/No).

Does anyone know a quick way (preferably using .NET functions) to swap the 0 for 1, 1 for 0 and fix the problem seamlessly? Or, is there a better way to write the SelectedIndex code?

Any help is appreciated :-)

+11  A: 
SelectedIndex='<%# 1 - Convert.ToInt32(...) %>

1 - 0 = 1; 1 - 1 = 0. Cases swapped :)

EDIT: There may well be a better way of handling the more general question - this was just the simple approach of solving the 1/0 swap :)

Jon Skeet
*duh* why didn't i think of that :) the idea of using a math function occurred to me but I was thinking mod() or something... seriously though great idea!
n2009
+4  A: 

Why not use the SelectedValue property rather than SelectedIndex.?

Jaime Febres
A: 

Have you tried using SelectedValue instead of SelectedIndex.

(From MSDN) The SelectedValue property can also be used to select an item in the list control by setting it with the value of the item.

WildJoe