简体   繁体   English

ListBox没有获取选定的项目

[英]ListBox not getting selected items

I have a ListBox which I am adding ListItems to in a codebehind. 我有一个ListBox,我将ListItems添加到代码隐藏中。 The problem I'm having is the ListBox is not seeing the selected items. 我遇到的问题是ListBox没有看到所选项目。 I have the ListBox being populated dynamically depending on what the user selects from a DropDownList, so the DropDownList has AutoPostBack set to true. 我根据用户从DropDownList中选择的内容动态填充ListBox,因此DropDownList将AutoPostBack设置为true。 I think this is somehow causing the problem. 我认为这是以某种方式导致问题。

My SelectedIndexChanged method, which is used whenever an item in the DropDownList is selected, calls a method called PopulateListBox . 我的SelectedIndexChanged方法(每当选择DropDownList中的项目时使用)调用一个名为PopulateListBox的方法。 Here's what those methods looks like: 以下是这些方法的样子:

protected void SelectedIndexChanged(object sender, EventArgs e)
{
    string typeStr = type.SelectedItem.Text;
    MyType = Api.GetType(typeStr);
    PopulateListBox();
}

private void PopulateListBox()
{
    listbox.Items.Clear();
    foreach (PropertyInfo info in MyType.GetProperties())
        listbox.Items.Add(new ListItem(info.Name));
}

For what it's worth, here are the DropDownList and ListBox: 对于它的价值,这里是DropDownList和ListBox:

<asp:DropDownList runat="server" ID="type" width="281px" OnSelectedIndexChanged="SelectedIndexChanged" AutoPostBack="true" />

<asp:ListBox runat="server" ID="listbox" width="281px" height="200px" selectionmode="Multiple" />

What I am trying to do is add a List of strings (strings being the selected items) as a session variable upon clicking a submit button. 我想要做的是在单击提交按钮时添加一个字符串列表(作为所选项的字符串)作为会话变量。 The button redirects to a new page after the List has been added to the session. 将List添加到会话后,该按钮将重定向到新页面。 Going through in debugger, the List of strings is empty at the point where I add it to the session. 在调试器中,字符串列表在我将其添加到会话时是空的。

listbox.GetSelectedIndices() returns nothing. listbox.GetSelectedIndices()返回任何内容。

Update 更新

I can access the selected items if I do not make a change in the DropDownList. 如果我没有在DropDownList中进行更改,我可以访问所选项目。 The ListBox is initially populated on page load, and if I make selections they are recognized. ListBox最初是在页面加载时填充的,如果我进行选择,则会识别它们。 If I select something from the DropDownList and the ListBox is repopulated, the selections are not recognized. 如果我从DropDownList中选择一些内容并重新填充ListBox,则无法识别选择。

My Page_Load method does only two things. 我的Page_Load方法只做两件事。 It initializes my Api variable and calls PopulateDropDown , which looks like this: 它初始化我的Api变量并调用PopulateDropDown ,如下所示:

private void PopulateDropDown()
{
    foreach (Type t in Api.GetAllTypes())
        type.Items.Add(new ListItem(t.Name));
    string typeStr = type.Items[0].Text;
    Type = Api.GetType(typeStr);
    PopulateListBox();
}

The problem is that you call PopulateDropDown() on every single Page_Load() , which calls PopulateListBox() , which clears the listbox and repopulates it. 问题是你在每个调用PopulateDropDown() Page_Load()上调用PopulateListBox() ,它会清除列表框并重新填充它。 By clearing the listbox, you clear the selection. 通过清除列表框,您可以清除选择。

You need to replace your call to PopulateDropDown() in the Page_Load() with the following code. 您需要使用以下代码替换对Page_Load() PopulateDropDown()调用。 The issue that I think you don't realize is that the page is loaded on every postback -- and that in the page life cycle, the page load occurs before the event. 我认为你没有意识到的问题是每次回发都会加载页面 - 而在页面生命周期中,页面加载发生在事件之前。 So by selecting a drop down item, you execute the Page_Load() event first (which indirectly executes the LoadListBox method, clearing the selection). 因此,通过选择下拉项,首先执行Page_Load()事件(间接执行LoadListBox方法,清除选择)。 The following code will populate the drop down list the first time the page loads only . 下面的代码将填充下拉列表中第一次加载页面。 Keep it the same wherever else you are using the load dropdown method: 在使用加载下拉方法的任何其他地方保持相同:

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 (!IsPostBack)
    {
        PopulateDropDown();
    }
}

The IsPostBack returns a boolean indicating whether the server side code is running because the page is loading for the first time ("false") or as a post back ("true"). IsPostBack返回一个布尔值,指示服务器端代码是否正在运行,因为页面是第一次加载(“false”)或作为回发(“true”)。

As I said elsewhere, keep in mind that a listbox with potential for multiple selected values must be handled differently than one with potential for a single selection. 正如我在其他地方所说的那样,请记住,具有多个所选值的潜在列表框必须以不同于具有单个选择潜力的列表框进行处理。 Don't reference listbox.SelectedItem , but rather: 不要引用listbox.SelectedItem ,而是:

foreach (ListItem item in lbFullNames)
{
    if (item.Selected)
    {
        // TODO: Whatever you are doing with a selected item.
    }
}

I have also found that if you disable the ListBox server-side, then use client side code to enable the list box using code like the following, then you cannot get the selected items server side. 我还发现,如果你禁用ListBox服务器端,然后使用客户端代码使用如下代码启用列表框,那么你无法获得所选项目服务器端。

$('.css-class-assigned-to-listbox').attr('disabled', '');

The fix is simply to make sure it is enabled server-side (the default), then disable it (see blow) or enable it (see above) using client-side code. 修复只是为了确保它在服务器端启用(默认),然后禁用它(请参阅打击)或使用客户端代码启用它(见上文)。

$('.css-class-assigned-to-listbox').attr('disabled', 'disabled');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM