ListBox没有获取所选项目 [英] ListBox not getting selected items

查看:123
本文介绍了ListBox没有获取所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListBox,我在代码中添加ListItems。我遇到的问题是ListBox没有看到所选项目。我根据用户从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(MyType.GetProperties()中的PropertyInfo信息)
listbox.Items.Add(new ListItem(info.Name));
}

对于什么值得,这里是DropDownList和ListBox:

 < asp:DropDownList runat =serverID =typewidth =281pxOnSelectedIndexChanged =SelectedIndexChangedAutoPostBack =true /> 

< asp:ListBox runat =serverID =listboxwidth =281pxheight =200pxselectionmode =Multiple/>

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



listbox.GetSelectedIndices()没有返回。



更新



我可以访问选择的项目,如果我没有在DropDownList更改。 ListBox最初在页面加载时填充,如果我进行选择,那么它们将被识别。如果我从DropDownList中选择一些东西并重新填充ListBox,则无法识别选择。



我的 Page_Load 方法只做两件事它初始化我的Api变量并调用 PopulateDropDown ,如下所示:

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


解决方案

问题是你打电话每个 Page_Load(),调用 PopulateListBox() PopulateDropDown() c $ c>,它清除列表框并重新填充它。通过清除列表框,您可以清除选择。



您需要将您的呼叫替换为 PopulateDropDown() Page_Load()包含以下代码。我认为您没有意识到的问题是页面在每个回发上都被加载,而在页面生命周期中,页面加载发生在事件之前。因此,通过选择一个下拉项目,先执行 Page_Load()事件(间接执行LoadListBox方法,清除选择)。以下代码将在页面第一次仅加载 时填充下拉列表。使用加载下拉方式,无论您身在何处,请保持不变:

  protected void Page_Load(object sender,EventArgs e)
{
//你的API代码在这里,除非你希望它只发生在页面加载的第一个
//时间,在这种情况下放在下面的IF语句中。
if(!IsPostBack)
{
PopulateDropDown();
}
}

IsPostBack返回一个布尔值,表示服务器端代码正在运行,因为页面第一次加载(false)或作为后贴(true)。



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

  foreach ListItem项目在lbFullNames)
{
if(item.Selected)
{
// TODO:无论你在选择的项目中做什么
}
}


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

解决方案

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.
    }
}

这篇关于ListBox没有获取所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆