对于 asp.net radiobuttonlist:通过 FindByValue 获取列表项并设置 Selected 属性不起作用?

发布于 2024-12-02 00:29:11 字数 383 浏览 6 评论 0原文

可能是一个基本问题,但我有以下代码:

ListItem l = radiolist.Items.FindByValue(mediaTypeID.ToString());
if (l != null)
  l.Selected = true;
else
  radiolist.SelectedIndex = 0;

handleMediaTypeChanged();

在上面的代码中,我可以成功地从单选按钮列表中检索正确的项目,但是将项目设置为“选定”不起作用。 SelectedIndexChanged 事件不会触发,当我使用 handleMediaTypeChanged() 手动调用它时,单选按钮列表不会反映更改的索引。如果不是这样,正确的方法是什么?

Probably a basic question but I have the following code:

ListItem l = radiolist.Items.FindByValue(mediaTypeID.ToString());
if (l != null)
  l.Selected = true;
else
  radiolist.SelectedIndex = 0;

handleMediaTypeChanged();

In the above code, I can successfully retrieve the correct item from the radiobuttonlist, however setting the item to be Selected is not working. The SelectedIndexChanged event does not fire, and when I call it manually using handleMediaTypeChanged() the radiobuttonlist does not reflect a changed index. What is the proper way if this isn't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-12-09 00:29:11

据我所知,只有当您从 UI 物理更改索引时才会触发 SelectedIndexChanged 事件。

你可以尝试这样的事情:

ListItem l = radiolist.Items.FindByValue(mediaTypeID.ToString());

radioList.ClearSelection();
if (l != null)
    l.Selected = true;

As far as I know, the SelectedIndexChanged event will only fire when you've physically changed the index from the UI.

You can try something like this:

ListItem l = radiolist.Items.FindByValue(mediaTypeID.ToString());

radioList.ClearSelection();
if (l != null)
    l.Selected = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文