【问题标题】:Repeater control radiobuttonlist always return null value中继器控件radiobuttonlist总是返回空值
【发布时间】:2014-05-18 07:06:01
【问题描述】:

我正在使用下面的代码来读取单选按钮列表值。但它总是返回 null。请帮我解决这个问题。

foreach (RepeaterItem item in repeaterItems.Items)
{
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        lbl_slno = (Label)item.FindControl("id");
        lbl_ques = (Label)item.FindControl("lblques");
        radiobtn = (RadioButtonList)item.FindControl("rdbtn");
        string radio_value = radiobtn.SelectedItem.Value;
        //radio_value return "Object reference not set to an instance of an object."
    }
}

<ItemTemplate>
    <table cellspacing="0" width="100%" align="center">
        <tr>
            <td style="width: 42px;" class="cu_style" >
                <asp:Label ID="id" runat="server" Text='<%#Bind("fld_id")%>'></asp:Label>
            </td>
            <td style="width: 503px;" class="cu_style">
                <asp:Label ID="lblques" runat="server" Text='<%#Bind("fld_Question")%>'></asp:Label>
            </td>
            <td style="width: 80px;" class="cu_style" colspan="3">
                <asp:RadioButtonList ID="rdbtn" Width="229px" runat="server" RepeatDirection="Horizontal" >
                <asp:ListItem Text="Agree">Agree&nbsp;</asp:ListItem>
                <asp:ListItem Text="Neutral">Neutral&nbsp;&nbsp;</asp:ListItem>
                <asp:ListItem Text="Disagree">Disagree</asp:ListItem>
                </asp:RadioButtonList>
            </td>

        </tr>
    </table>
</ItemTemplate>

我的设计密码在这里,..

【问题讨论】:

  • 向我们展示 html,您是否从单选按钮列表中选择一个选项?
  • 您是否从 lbl_slno 和 lbl_ques 等其他文件中获得价值?
  • RadioButtonList radiobtn = (RadioButtonList)item.FindControl("rdbtn");
  • krshekhar 是的,我得到了其他两个值。
  • 为什么不调试代码?

标签: c# asp.net radiobuttonlist


【解决方案1】:

尝试通过.SelectedValue获取价值

if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            var rdbList = item.FindControl("rdbtn") as RadioButtonList;
            // Get the selected value
            string selected = radiobtn.SelectedValue;
        }

【讨论】:

  • 因为和上面一样
猜你喜欢
  • 2010-10-15
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2013-11-17
  • 1970-01-01
相关资源
最近更新 更多