【问题标题】:Listbox in asp.net not getting selected itemsasp.net中的列表框没有得到选定的项目
【发布时间】:2013-05-02 12:36:41
【问题描述】:

我的网页中有多个下拉列表和列表框。

我正在尝试从lstCatID 列表框中获取CategoryID 列表,我可以使用类别名称填充列表框。

如果我在第一次尝试时没记错的话,我的代码运行良好,之后我做了一些更改,然后它声明总是选择第一个项目 x 时间

<asp:ListBox ID="lstCatID" runat="server" DataTextField="CategoryName" 
                DataValueField="CategoryID" SelectionMode="Multiple" CssClass="lstListBox">
 </asp:ListBox>



protected void Button1_Click(object sender, EventArgs e)
{
    string CatID = string.Empty;
    foreach (ListItem li in lstCatID.Items)
    {
        if (li.Selected == true)
        {
           // Response.Write();
            CatID += lstCatID.SelectedItem.Value + ",";
        }
    }
    Response.Write(CatID);
}

我不确定出了什么问题,我检查了 MSDN,它显示的方法完全相同。

可能是我做错了什么。

只需使用 Firefox 添加,我就能看到多个选定的值具有选定的属性。

<option value="3" selected="selected">One</option>
<option value="2">Two</option>
<option value="29" selected="selected">Three</option>
<option value="25" selected="selected">Four</option>
<option value="22" >Five</option>

在这种情况下我的输出将是3,3,3

我会很感激这方面的帮助

【问题讨论】:

  • 需要开启视图状态吗?
  • @DaiBok,是的,我需要,因为我在页面上有几个控件..

标签: c# asp.net listbox


【解决方案1】:

我不确定我使用的逻辑有什么问题。

我发现了一个使用 LINQ 的不错的解决方案。

这一条语句效果很好,让我得到了想要的结果。

string values = String.Join(", ", lstCatID.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToArray());

结果:3,29,25

【讨论】:

    【解决方案2】:

    您每次都将其设置为相同的值:

    foreach (ListItem li in lstCatID.Items)
    {
        if (li.Selected == true)
        {
           // you are always using lstCatID.SelectedItem.Value.
            CatID += lstCatID.SelectedItem.Value + ",";
        }
    }
    

    当你真正想要你的循环中被选中的项目的值时:

    foreach (ListItem li in lstCatID.Items)
    {
        if (li.Selected == true)
        {
            // get the value of the item in your loop
            CatID += li.Value + ",";
        }
    }
    

    【讨论】:

    • 我的逻辑有问题。你是对的。但我更喜欢使用我清理过的 LINQ 的解决方案
    • 是的,我也喜欢 LINQ 的答案……只是想让你看到问题 :)
    • 我会标记你的答案,因为它解决了与逻辑相关的实际问题。有时小错误被忽视了..谢谢..
    【解决方案3】:

    尝试在您的 Page_Load 上添加 Page.IsPostback 喜欢

    protected void Page_Load(object sender, EventArgs e)
    {
        // Do your API code here unless you want it to occur only the first
        // time the page loads, in which case put it in the IF statement below.
        if (!Page.IsPostBack)
        {
    
        }
    }
    

    代码:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string CatID = string.Empty;
        foreach (ListItem li in lstCatID.Items)
        {
            if (li.Selected )
            {
               // TODO: Whatever you are doing with a selected item.
            }
        }
        Response.Write(CatID);
    }
    

    一旦我遇到同样的问题并且我犯了回发错误。

    希望它有效。

    【讨论】:

    • 我在Page.IsPostback 的所有页面上都使用它,但是我的代码在按钮事件上会有什么不同
    • 我在if (li.selected)之前也试过了,没有什么不同。
    • @KnowledgeSeeker:如果您对 Web 控件进行数据绑定,则不会触发任何事件并且所有更改都将丢失。
    • @TimSchmelter,很高兴见到你,那么我该如何解决这个问题。我的网页上有多个 ListBox。
    • (li.Selected) 没有太大区别,但 Page.IsPostBack 会有所不同...如果选择了 (li.selected),则您正在使用它,那么它将始终返回 True,这就是为什么我从你的代码中删除了这个 true..
    【解决方案4】:

    使用 linq 获取选中的项目

    var selected = lstCatID.Items.Where(i => i.Selected);
    

    【讨论】:

      【解决方案5】:

      几分钟后我找到了解决方案:

      If lstLocations.Items.Count > 0 Then
                  For i As Integer = 0 To lstLocations.Items.Count - 1
                      If lstLocations.Items(i).Selected Then
                          'insert command
                          Dim selectedItem As String = lstLocations.Items(i).Text
                      End If
                  Next
              End If
      

      这在我的场景中运行良好

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-12
        • 1970-01-01
        • 2016-03-17
        • 2010-11-28
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多