Asp.net 使用SelectedValue对RadioButtonList进行数据绑定…可能吗?

Asp.net 使用SelectedValue对RadioButtonList进行数据绑定…可能吗?,asp.net,Asp.net,我正在将GridView数据绑定到对象数据源。gridview包含一个TemplateField,其中包含一个RadioButtonList,其中ListItems是内联定义的。 我希望能够将RadioButtonList的SelectedValue数据绑定到与其他网格列相同的基础表中,但这不起作用 我的语法是否有误,或者这是不可能的,并且需要循环代码来分别选择每行中的正确项 <llblgenpro:LLBLGenProDataSource ID="llbComputerApplicati

我正在将GridView数据绑定到对象数据源。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>

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

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

当绑定MS SQL中的布尔值时,我也遇到了在radiobuttonlist中未选择任何内容的问题:

radDefault.Items.Add(new ListItem("Yes", "true"));
radDefault.Items.Add(new ListItem("No", "false"));
在我的例子中,解决方案是将真/假值的第一个字母大写,然后RadioButtonList按预期工作:

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>
它让我工作。。。。。
gnanasekar.s vilangulathur

我不喜欢使用css隐藏项目的想法。相反,我发现添加了一个空白项,但在代码隐藏中删除了它

<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);
 }

我尝试了各种方法…我发布的特定版本甚至根本不呈现GridView!!!然而,如果我去掉它,所有的行都会被渲染,但是没有一行被选中,即使它们在数据库中的行与ListItem.ValueNope匹配,也没有任何错误。我使用的是Eval,而不是像你一样使用Bind……我确信我也尝试过Bind。但这应该行得通,对吗?您正在将MyRbl绑定到datagrid底层的数据源,对吗?是的,这是取自我当前项目的工作代码-您的代码看起来确实正确。我的示例代码取自绑定到ObjectDataSource的GridView中的模板字段。Scott Word,该代码仅适用于大写值:True和False。非常有用。给其他读者的两条评论:1。SelectedValue没有与我的Intellisense一起出现-不要被愚弄,它是有效的。2.如果你不接受空值,那么添加一个RequiredFieldValidator,空值将不会被接受。我知道这篇文章很旧,但我只是想评论一下大小写确实很重要。这也帮我修好了。谢谢
<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);
 }