简体   繁体   English

如何在 ASP.NET 中获取列表框的选定项?

[英]How to get the selected item of a listbox in ASP.NET?

I use a listbox to view some record of my database as an string, but I have two problems:我使用列表框将我的数据库的一些记录作为字符串查看,但我有两个问题:

  1. the selection change does not invoke with double click or single click, it only invokes when you select an item and press return.选择更改不会通过双击或单击来调用,它只会在您选择一个项目并按回车键时调用。
  2. even when the selection changed invokes, the selectedIndex returns -1 and selected value is null.即使在选择更改调用时, selectedIndex 也返回 -1 并且选择的值为空。

ASP code: ASP代码:

<form runat="server">
    <div class="clearfix">
        <asp:ListBox runat="server" ID="g1SeasonsListView" CssClass="adminSeasonsListView"  OnSelectedIndexChanged="g1SeasonsListView_SelectedIndexChanged" />
        <asp:ListBox runat="server" ID="g2SeasonsListView" CssClass="adminSeasonsListView" />
        <asp:ListBox runat="server" ID="g3SeasonsListView" CssClass="adminSeasonsListView" />
    </div>
    <div class="clearfix">
        <asp:Button ID="g1DeleteBtn" Text="Delete" runat="server" OnClick="g1DeleteBtn_Click" CssClass="adminDeleteBtn" />
        <asp:Button ID="g2DeleteBtn" Text="Delete" runat="server" OnClick="g2DeleteBtn_Click" CssClass="adminDeleteBtn" />
        <asp:Button ID="g3DeleteBtn" Text="Delete" runat="server" OnClick="g3DeleteBtn_Click" CssClass="adminDeleteBtn" />
    </div>
</form>

The CS Code, which I checked via watch window in VS2013; CS 代码,我在 VS2013 中通过观察窗口检查过; there is nothing special in it:没有什么特别之处:

protected void g1DeleteBtn_Click(object sender, EventArgs e)
{    
}
protected void g2DeleteBtn_Click(object sender, EventArgs e)
{
}
protected void g3DeleteBtn_Click(object sender, EventArgs e)
{    
}
protected void g1SeasonsListView_SelectedIndexChanged(object sender, EventArgs e)
{    
}

and when I want to update listboxes, I use this code:当我想更新列表框时,我使用以下代码:

private void UpdateListeView()
{
    List<string> seasonsListTemp = null;

    g1SeasonsListView.Items.Clear();
    seasonsListTemp = (from s in database.Gs where s.grade == 1 select s.season).ToList();
    for (int i = 0; i < seasonsListTemp.Count; i++)
        g1SeasonsListView.Items.Add(seasonsListTemp[i]);

    g2SeasonsListView.Items.Clear();
    seasonsListTemp = (from s in database.Gs where s.grade == 2 select s.season).ToList();
    for (int i = 0; i < seasonsListTemp.Count; i++)
        g2SeasonsListView.Items.Add(seasonsListTemp[i]);

    g3SeasonsListView.Items.Clear();
    seasonsListTemp = (from s in database.Gs where s.grade == 3 select s.season).ToList();
    for (int i = 0; i < seasonsListTemp.Count; i++)
        g3SeasonsListView.Items.Add(seasonsListTemp[i]);
}

I even tried to get the selected index when I press the delete button, but the result are the same.我什至试图在按下删除按钮时获取所选索引,但结果是一样的。

Bind your code in a !Page.IsPostBack block.将您的代码绑定在!Page.IsPostBack块中。 That will ensure that code does not executes on postback.这将确保代码不会在回发时执行。 If you bind the ListView on PostBack you will loose the state of the GridView that includes the event being triggered, SelectedIndex etc.如果您在 PostBack 上绑定 ListView,您将失去 GridView 的状态,包括正在触发的事件、SelectedIndex 等。

Page.IsPostBack Property Page.IsPostBack 属性

Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.获取一个值,该值指示页面是首次呈现还是为响应回发而加载。

if(!Page.IsPostBack)
{
     UpdateListeView();
}

Usually the controls are bound in Page_Load method and you probably need to check Page.IsPostBack there.通常控件绑定在 Page_Load 方法中,您可能需要在那里检查 Page.IsPostBack。

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

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