2009-06-26 154 views
8

我将数据绑定到对象数据源。 gridview包含一个TemplateField,其中包含一个带有ListItems内联定义的RadioButtonList。
我希望能够将RadioButtonList的SelectedValue绑定到与其他网格列相同的基础表,但它不起作用!使用SelectedValue ...的数据绑定RadioButtonList可能吗?

我的语法错了吗?或者这是不可能的,并且需要循环代码来单独选择每行中的适当项目?

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

回答

18

你应该有什么工作。你有错误吗?这是从我当前的项目复制的一个工作示例。我绑定到一个可空的位字段 - 所以有一个隐藏的列表项来接受空值。

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

我试过不同的方式......我发布的特定版本甚至不会呈现GridView!而如果我把它拿出来,所有的行渲染,但没有被选中(即使他们有数据库中的行(与ListItem.Value匹配) – tbone 2009-06-26 22:37:26

+0

),你会得到一个错误? – 2009-06-26 23:02:13

4

我反对在MS SQL布尔值绑定时也遇到这个问题(没有在单选按钮列表中选择):

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

在我的情况下,解决办法是利用真实的第一个字母/假值,那么radiobuttonlistworked预期:

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

或者声明:

<asp:RadioButtonList runat="server" ID="radDefault" SelectedValue='<%# Bind("DB_FIELD") %>'> 
    <asp:ListItem Value="False" Text="No" /> 
    <asp:ListItem Value="True" Text="Yes" /> 
</asp:RadioButtonList> 
0
<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="" selected="true" style="display: none" /> 
</asp:RadioButtonList> 

这工作我..... gnanasekar.s vilangulathur

1

我不喜欢用css来隐藏一个项目的想法。相反,我发现this solution添加了一个空白项目,但在后面的代码中将其删除。

<asp:RadioButtonList ID="MyRadioButtonList" runat="server" 
    SelectedValue='<%# Bind("Blah") %>' 
    OnDataBound="MyRadioButtonList_DataBound"> 
     <asp:ListItem Value=""></asp:ListItem> 
     <asp:ListItem Value="A"></asp:ListItem> 
     <asp:ListItem Value="B"></asp:ListItem> 
     <asp:ListItem Value="C"></asp:ListItem> 
</asp:RadioButtonList> 

protected void MyRadioButtonList_DataBound(object sender, EventArgs e) 
{ 
     RadioButtonList list = (RadioButtonList)sender; 
     ListItem blank = list.Items.FindByValue(""); 
     if (blank != null) 
      list.Items.Remove(blank); 
} 
相关问题