我有一个ListBox,我正在代码后端添加ListItems。问题是ListBox无法识别已选项目。 ListBox是根据用户从DropDownList中选择的内容动态填充的,因此DropDownList设置了AutoPostBack为true。我认为这可能是导致问题的原因。
我的SelectedIndexChanged方法用于在选择DropDownList中的项目时调用PopulateListBox方法。以下是这些方法的示例:
我试图在单击提交按钮后将一个字符串列表(选定项为字符串)作为会话变量添加。在将列表添加到会话后,按钮将重定向到新页面。通过调试器,当我将其添加到会话中时,字符串列表为空。
更新: 如果我不更改 DropDownList,则可以访问所选项目。ListBox最初在页面加载时填充,如果我进行选择,则它们将被识别。如果我从DropDownList中选择某些内容并重新填充ListBox,则不会识别选择内容。
我的 Page_Load 方法只有两个操作。它初始化我的 Api 变量并调用 PopulateDropDown,它的代码如下:
我的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,则可以访问所选项目。ListBox最初在页面加载时填充,如果我进行选择,则它们将被识别。如果我从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();
}