【问题标题】:Pre-Select a radio button in a asp.net radiobuttonlist based on a value/text in asp.net根据 asp.net 中的值/文本预先选择 asp.net 单选按钮列表中的单选按钮
【发布时间】:2018-02-12 16:47:09
【问题描述】:

我有一个从 sql-server 获取数据的 asp:RadioButtonList。单选按钮列表是在弹出的编辑窗口中创建的。 而且由于它是一个编辑窗口,所以应该有预先选择,以便用户可以看到预先选择的单选按钮的现有数据。

<asp:RadioButtonList ID="RadioButtonList4" runat="server"
                     DataSourceID="SqlDataSource3"
                     DataTextField="NAME"
                     RepeatColumns="2">
</asp:RadioButtonList>

我尝试将这行代码放在“on_click 以打开编辑窗口”按钮中,但它不起作用:

RadioButtonList4.Items.FindByValue("some string").Selected = true;

我现在也对 jquery 解决方案持开放态度。

【问题讨论】:

  • 没有指定DataValueFieldRadioButtonList4.Items 中的“值”到底是什么?
  • 它是动态的,项目由sqldatasource填充
  • 好吧,如果您不知道这些值是什么或控制它们的来源,那么.FindByValue() 有点像在黑暗中拍摄,不是吗?您使用的数据是什么?为什么您希望 "some string" 出现在该数据中?当您说“它不起作用”时,您具体希望发生什么,为什么?具体正在发生什么

标签: c# asp.net radiobuttonlist


【解决方案1】:

也许你可以先尝试清除选择,像这样:

RadioButtonList4.ClearSelection();
RadioButtonList4.Items.FindByValue("some string").Selected = true;

但您的问题可能是由于弹出窗口,可能是您在此事件上无权访问 RadioButtonList4,或者有人正在预选该值。 还要确保你有值:你的数据源中有一些字符串。

一般来说,你的方法是正确的,在其他地方搜索问题

【讨论】:

    猜你喜欢
    • 2012-09-11
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多