【问题标题】:radio button, set first item as selected单选按钮,将第一项设置为选中
【发布时间】:2010-11-15 18:29:37
【问题描述】:

我有一个用 asp.net c# 构建的页面。它在左侧包含一个单选按钮列表,当用户选择其中一个按钮时,右侧的内容窗口会显示与该按钮关联的数据。我想设置“selectedindex = 0”,以便在页面加载时用户看到第一个单选按钮的内容。

在我后面的代码中,如果我在单选列表数据绑定方法中设置 radioButtonList1.SelectedIndex = 0,用户将在页面加载时看到第一个单选选择。但与预选单选按钮相关的内容不显示。我需要在内容查看器的数据绑定方法中包含什么才能做到这一点。谢谢!

-------- 后面的代码

public partial class test_123 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

      radioButtonList1.SelectedIndex = 0


    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

【问题讨论】:

  • 你试过radioButtonList1.DataBind()吗?
  • 没有工作,但感谢您的意见。
  • 如果墙壁太高而无法攀爬,我会选择客户端解决方案。如果涉及到这一点,请告诉我,我会提供一些示例代码。
  • 实际上,在页面加载方法中,我放置了以下代码以使其正常工作。这似乎是一个好方法。 if(Page.IsPostBack) return; RadioButtonList1.SelectedIndex = 0; RadioButtonList1.DataBind();
  • 嗯..这不是我之前建议的,你说“没用”吗?现在有点困惑。

标签: c# asp.net radio-button


【解决方案1】:

在页面加载时将 SelectedIndex 设置为 0 后,尝试显式调用 ListBox1_SelectedIndexChanged()。

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
        return;

    radioButtonList1.SelectedIndex = 0;
    ListBox1_SelectedIndexChanged(null, null);
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    LoadContent(ListBox1.SelectedIndex);
}

【讨论】:

  • loadconent(Listbox1.selectedIndex); 无法识别。你是不是想写点别的。感谢您的意见!
  • 是的,根据 selectedindex 加载内容应该是您的逻辑。
  • 我使用了您的部分代码来获得我的解决方案,非常感谢!对此,我真的非常感激!你帮助我走上了正轨。
猜你喜欢
  • 2013-04-23
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多