列表框未获取所选项目

发布于 2024-10-12 12:42:30 字数 1621 浏览 2 评论 0原文

我有一个 ListBox,我正在将 ListItems 添加到代码隐藏中。我遇到的问题是列表框看不到所选项目。我根据用户从 DropDownList 中选择的内容动态填充 ListBox,因此 DropDownList 将 AutoPostBack 设置为 true。我认为这在某种程度上导致了问题。

我的 SelectedIndexChanged 方法(每当选择 DropDownList 中的项目时都会使用该方法)调用名为 PopulateListBox 的方法。这些方法如下所示:

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));
}

对于它的价值,这里是 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" />

我想做的是在单击提交按钮时添加一个字符串列表(字符串是所选项目)作为会话变量。将列表添加到会话后,该按钮将重定向到新页面。在调试器中进行检查时,在我将字符串列表添加到会话中时,字符串列表为空。

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

更新

如果我不在 DropDownList 中进行更改,我可以访问所选项目。列表框最初是在页面加载时填充的,如果我做出选择,它们就会被识别。如果我从 DropDownList 中选择某些内容并重新填充 ListBox,则无法识别所选内容。

我的 Page_Load 方法只做两件事。它初始化我的 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();
}

I have a ListBox which I am adding ListItems to in a codebehind. The problem I'm having is the ListBox is not seeing the selected items. I have the ListBox being populated dynamically depending on what the user selects from a DropDownList, so the DropDownList has AutoPostBack set to 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. 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:

<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. Going through in debugger, the List of strings is empty at the point where I add it to the session.

listbox.GetSelectedIndices() returns nothing.

Update

I can access the selected items if I do not make a change in the DropDownList. The ListBox is initially populated on page load, and if I make selections they are recognized. If I select something from the DropDownList and the ListBox is repopulated, the selections are not recognized.

My Page_Load method does only two things. It initializes my Api variable and calls PopulateDropDown, which looks like this:

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病女 2024-10-19 12:42:30

问题是,您在每个 Page_Load() 上调用 PopulateDropDown(),这会调用 PopulateListBox(),从而清除列表框并重新填充它。通过清除列表框,您可以清除选择。

您需要将 Page_Load() 中对 PopulateDropDown() 的调用替换为以下代码。我认为您没有意识到的问题是页面在每次回发时加载 - 并且在页面生命周期中,页面加载发生在事件之前。因此,通过选择一个下拉项,您首先执行 Page_Load() 事件(它间接执行 LoadListBox 方法,清除选择)。以下代码将仅在页面第一次加载时填充下拉列表。。在使用加载下拉方法的其他地方保持相同:

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();
    }
}

IsPostBack 返回一个布尔值,指示服务器端代码是否正在运行,因为页面是第一次加载(“false”)还是作为回发(“true”) )。

正如我在其他地方所说,请记住,可能具有多个选定值的列表框的处理方式必须与可能具有单个选择的列表框不同。不要引用 listbox.SelectedItem,而是:

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

The problem is that you call PopulateDropDown() on every single Page_Load(), which calls PopulateListBox(), which clears the listbox and repopulates it. By clearing the listbox, you clear the selection.

You need to replace your call to PopulateDropDown() in the Page_Load() with the following code. 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). 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").

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:

foreach (ListItem item in lbFullNames)
{
    if (item.Selected)
    {
        // TODO: Whatever you are doing with a selected item.
    }
}
喵星人汪星人 2024-10-19 12:42:30

我还发现,如果您禁用列表框服务器端,然后使用客户端代码使用如下代码启用列表框,那么您将无法获取服务器端的所选项目。

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

修复方法只是确保它在服务器端启用(默认),然后禁用它(见下文)或使用客户端代码启用它(见上文)。

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

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.

$('.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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文