【问题标题】:Databinding of RadioButtonList using SelectedValue...possible?使用 SelectedValue 对 RadioButtonList 进行数据绑定...可能吗?
【发布时间】:2015-04-01 00:01:52
【问题描述】:

我正在将 GridView 数据绑定到对象数据源。网格视图包含一个 TemplateField,其中包含一个 RadioButtonList,其中 ListItems 内联定义。
我希望能够将 RadioButtonList 的 SelectedValue 数据绑定到与其他网格列相同的基础表,但它不起作用!

我的语法是否错误,或者这是不可能的并且需要循环代码来单独选择每一行中的正确项目?

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

【问题讨论】:

    标签: asp.net


    【解决方案1】:

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

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

    【讨论】:

    • 我尝试了各种方法...我发布的特定版本甚至根本不渲染 GridView !!!而如果我把它拿出来,所有的行都会呈现,但没有被选中(即使它们在数据库中有行(与 ListItem.Value 匹配)
    • 不,完全没有错误。我使用的是 Eval 而不是 Bind ,但我确定我也尝试过 bind 。但这应该工作,对吧?您正在将 MyRbl 绑定到数据网格下的数据源,对吗?
    • 是的,这是取自我当前项目的工作代码 - 您的代码看起来确实正确。我的示例代码取自绑定到 ObjectDataSource 的 GridView 中的模板字段。
    • #Scott 很奇怪,该代码仅适用于我的大写值:True 和 False。
    • 非常有帮助。给其他读者的两个 cmets: 1. 我的 Intellisense 没有出现“SelectedValue”——不要被愚弄,它是有效的。 2.如果不接受null,则添加RequiredFieldValidator,null值将不被接受。
    【解决方案2】:

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

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

    就我而言,解决方案是将真/假值的第一个字母大写,然后单选按钮列表按预期工作:

    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>
    

    【讨论】:

    • 我知道这篇文章很旧,但只是想评论一下大小写确实很重要。这也为我修复了它。谢谢
    【解决方案3】:

    我不喜欢使用 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);
     }
    

    【讨论】:

      【解决方案4】:
      <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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-09
        相关资源
        最近更新 更多