【问题标题】:How TO Prevent Multiple Entries in my ListBox when selecting Item From a List View从列表视图中选择项目时如何防止我的列表框中的多个条目
【发布时间】:2015-09-28 22:48:22
【问题描述】:

我有一个从 SQL Server 数据库填充数据的 ASP ListView 控件,它按设计工作。我还有一个 ASP Listbox 控件,它使用 ListView 控件中的 ID 和名称填充,它有点工作,也就是说,如果我选择它,它会按设计显示,但如果我继续从 ListView 控件中选择同一行,它不断将它添加到列表框中,这不是我想要的,我宁愿它只显示一次,无论我从列表视图中选择同一行多少次。

这就是我所拥有的

protected void distList_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
    int rowIndex;

    distList.SelectedIndex = e.NewSelectedIndex;
    rowIndex = distList.SelectedIndex;
    string sapId = distList.DataKeys[rowIndex].Values[0].ToString();
    string DistributorName = distList.DataKeys[rowIndex].Values[1].ToString();
    string output = sapId + " " + DistributorName;

    SAPListBox.Items.Add(output);
}

我一直处于停滞状态,因为我整天都在尝试很多事情却一无所获,我想我可能只是错过了一些东西。

【问题讨论】:

  • 与经典 ASP 无关,请重新标记。

标签: c# web-controls


【解决方案1】:
int rowIndex;

    distList.SelectedIndex = e.NewSelectedIndex;
    rowIndex = distList.SelectedIndex;

//You might need to change this, to the listbox rather than a listview. Wasn't quite sure which one you wanted. 
    var txt = rowIndex;
    if (!distlist.Items.ContainsKey(txt))
{


string sapId = distList.DataKeys[rowIndex].Values[0].ToString();
    string DistributorName = distList.DataKeys[rowIndex].Values[1].ToString();
    string output = sapId + " " + DistributorName;


    SAPListBox.Items.Add(output);
}

如果没有,希望这会有所帮助。这是我最好的尝试^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多