tags:

views:

1164

answers:

2

I am databinding a GridView to an object datasource. The gridview contains a TemplateField which contains a RadioButtonList with ListItems defined inline.
I want to be able to databind the SelectedValue of the RadioButtonList to the same underlying table as the other grid columns, but it doesn't work!

Do I have my syntax wrong, or is this impossible and requires looping code to individually select the proper item in each row?

<llblgenpro:LLBLGenProDataSource ID="llbComputerApplication" DataContainerType="EntityCollection" runat="server"></llblgenpro:LLBLGenProDataSource>
        <asp:GridView ID="gridComputerApps" DataSourceID="llbComputerApplication" runat="server" AutoGenerateColumns="False" 
            EmptyDataText ="NO APPLICATIONS FOUND FOR THIS COMPUTER." 
            DataKeyNames="ComputerID, ApplicationID" EnableViewState="False"
            style="border-style:dotted;border-width:thin"
            >
            <Columns>
                <asp:BoundField DataField="ApplicationID" HeaderText="Application ID" SortExpression="ApplicationID" Visible="True" />
                <asp:TemplateField HeaderText="Application Name"><ItemTemplate><%#Eval("Application.ApplicationName")%></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="Normalized Name"><ItemTemplate><%#Eval("Application.NormalizedAppName")%></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="Notes"><ItemTemplate><%#Eval("Application.NormalizedNotes")%></ItemTemplate></asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:RadioButtonList SelectedValue='<%#Eval("RequirementOption")%>' ID="rblRequirementOption" RepeatDirection="Horizontal"  runat="server">
                            <asp:ListItem Value="Need Now" Text="Need Now"></asp:ListItem>
                            <asp:ListItem Value="Need Someday" Text="Need Someday"></asp:ListItem>
                            <asp:ListItem Value="Do Not Need" Text="Do Not Need"></asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="NormalizedNotes" HeaderText="Notes" Visible="False" />
            </Columns>
        </asp:GridView>
+2  A: 

What you have should work. Are you getting an error? Here's a working example copied from my current project. I'm binding to a nullable bit field - so have a hidden list item to accept the nulls.

<asp:RadioButtonList runat="server" ID="MyRbl" SelectedValue='<%# Bind("MyRblField") %>'
    CssClass="NormalTextBox" RepeatDirection="Horizontal">
    <asp:ListItem Value="false" Text="No" />
    <asp:ListItem Value="true" Text="Yes" />
    <asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
Scott Ivey
I've tried various ways...the particular version I posted doesn't even render the GridView at all!!! Whereas if I take that out, all the rows render, but none are selected (even though they have rows in the database (matching the ListItem.Value)
tbone
are you getting an error?
Scott Ivey
Nope, no error at all. I am using Eval rather than Bind as you are....I'm sure I tried bind as well though. But this *should* work, right? You are binding MyRbl to the datasource underlying your datagrid right?
tbone
yes, this is working code taken from my current project - and your code does look right. My sample code was taken from a template field in a GridView bound to an ObjectDataSource.
Scott Ivey
ok thanks, will mess with it more.
tbone
Ok, it is now working using this syntax, noooo idea what the problem was in the first place. Thanks!
tbone
+2  A: 

I also experienced this problem (nothing selected in radiobuttonlist) when binding against boolean values in MS SQL:

radDefault.Items.Add(new ListItem("Yes", "true"));
radDefault.Items.Add(new ListItem("No", "false"));

In my case, the solution was to capitalize the first letter of the true/false values, then the radiobuttonlistworked as expected:

radDefault.Items.Add(new ListItem("Yes", "True"));
radDefault.Items.Add(new ListItem("No", "False"));

Or, declaratively:

<asp:RadioButtonList runat="server" ID="radDefault" SelectedValue='<%# Bind("DB_FIELD") %>'>
    <asp:ListItem Value="False" Text="No" />
    <asp:ListItem Value="True" Text="Yes" />
</asp:RadioButtonList>
Shaun3180