【问题标题】:Drop down list data bind下拉列表数据绑定
【发布时间】:2013-06-13 15:55:18
【问题描述】:

我有两个下拉列表。当我从第一个 ddl 中选择一些项目时,第二个我得到一些其他值可供选择。我有一个问题,因为当我从第二个 ddl 中选择一些值然后在第一个 ddl 中选择标准值时,页面加载后仍然有第二个 ddl 的旧值。 当我想在页面加载之前在第二个 ddl 中设置默认值时应该怎么做?

编辑: 添加代码:

    protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GetFunctions();
        GetTeams();
        LocateCities();
    }
    else
    {
        if (ddlFunctions.Text.ToString() == "All  Functions")
        {
            ddlTeams.Text = " All  Teams";
            LocateCities();
        }
        else
        {
            LocateCities();
        }
    }
}

protected void ddlFunctions_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlFunctions.Text.ToString() == "All  Functions")
    {
        Teams.Visible = false;
    }
    else
    {
        Teams.Visible = true;
        GetTeams();
    }
}

与 ddl 的绑定在 GetFunctions()(第一个 ddl)和 GetTeams()(第二个 ddl)中。唯一的问题是当我在第二个 ddl 中选择了一些值并在第一个 ddl 中更改值时(第二个 ddl 的旧值进入存储过程,导致 LocateCities() 方法返回 0 值)。当我在第一个 ddl 中更改值时,我希望将所有团队都传递给程序的默认值。我希望我解释了exerything。如果没有,请告诉我。

【问题讨论】:

  • 当您在第一个 DDL 中选择一个项目时,您的事件(将项目加载到第二个 ddl)没有执行吗?
  • 你在使用回发吗?
  • 请分享一些代码。否则就不可能找到解决问题的办法。
  • @Learner:是的,我设置了 AutoPostBack="true"

标签: c# asp.net drop-down-menu


【解决方案1】:

使用第一个的SelectedIndexChanged event 设置第二个的默认值。

像这样:

void foo_SelectedIndexChanged(object sender, EventArgs e) {
    bar.SelectedIndex = ((DropDownList)sender).SelectedIndex;
    // or whatever your logic is. Merry coding.
}

【讨论】:

  • 我尝试了您发布的内容。我添加了这一行:ddlFunctions.SelectedIndex = ((DropDownList)sender).SelectedIndex;但它不起作用。
  • 你必须提供你自己的逻辑。在此之前,您还必须重做第二个列表的数据绑定或过滤。我写的代码只是建议使用事件,如果你只是逐字粘贴它很容易出错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 2023-03-13
相关资源
最近更新 更多