列表框未获取所选项目
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,您在每个
Page_Load()
上调用PopulateDropDown()
,这会调用PopulateListBox()
,从而清除列表框并重新填充它。通过清除列表框,您可以清除选择。您需要将
Page_Load()
中对PopulateDropDown()
的调用替换为以下代码。我认为您没有意识到的问题是页面在每次回发时加载 - 并且在页面生命周期中,页面加载发生在事件之前。因此,通过选择一个下拉项,您首先执行Page_Load()
事件(它间接执行 LoadListBox 方法,清除选择)。以下代码将仅在页面第一次加载时填充下拉列表。仅。在使用加载下拉方法的其他地方保持相同:IsPostBack 返回一个布尔值,指示服务器端代码是否正在运行,因为页面是第一次加载(“false”)还是作为回发(“true”) )。
正如我在其他地方所说,请记住,可能具有多个选定值的列表框的处理方式必须与可能具有单个选择的列表框不同。不要引用
listbox.SelectedItem
,而是:The problem is that you call
PopulateDropDown()
on every singlePage_Load()
, which callsPopulateListBox()
, which clears the listbox and repopulates it. By clearing the listbox, you clear the selection.You need to replace your call to
PopulateDropDown()
in thePage_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 thePage_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: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:我还发现,如果您禁用列表框服务器端,然后使用客户端代码使用如下代码启用列表框,那么您将无法获取服务器端的所选项目。
修复方法只是确保它在服务器端启用(默认),然后禁用它(见下文)或使用客户端代码启用它(见上文)。
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.
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.