Selected item in list box is null

2019-02-18 18:44发布

问题:

I have few items in the listbox. When I select an item, I store it in a string for further use, but when I display that string value it is shown as null.

Below is the code for my Invitee.aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Invitee.aspx.cs" Inherits="FinalProj2.Invitee" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList3" runat="server">
        </asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList4" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList5" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList6" runat="server">
        </asp:DropDownList>

    </div>
    <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" Height="310px" 
        onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="271px">
    </asp:ListBox>
    <asp:TextBox ID="TextBox1" runat="server" Height="217px" Width="544px"></asp:TextBox>
    </form>
</body>
</html>

And code for Invitee.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FinalProj2
{
    public partial class Invitee : System.Web.UI.Page
    {
        FinalProj2.Models.DataClasses1DataContext db = new FinalProj2.Models.DataClasses1DataContext();

        protected void Page_Load(object sender, EventArgs e)
        {

            for (int i = 1; i < 13; i++)
            {
                DropDownList1.Items.Add(new ListItem(i.ToString()));
                DropDownList4.Items.Add(new ListItem(i.ToString()));
            }

            for (int i = 1; i < 32; i++)
            {
                DropDownList2.Items.Add(new ListItem(i.ToString()));
                DropDownList5.Items.Add(new ListItem(i.ToString()));
            }

            for (int i = 2010; i < 2021; i++)
            {
                DropDownList3.Items.Add(new ListItem(i.ToString()));
                DropDownList6.Items.Add(new ListItem(i.ToString()));
            }


            var query = from emp in db.Employees
                        select emp.Employee_Name;

            ListBox1.DataSource = query;
            ListBox1.DataBind();
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write("Hello");

            string selected = ListBox1.SelectedValue.ToString();

            Response.Write("\n Selected Value is" + selected);

            var query = from emp in db.Employees
                        where emp.Employee_Name == selected
                        select emp.Employee_ID;

            Response.Write(query);

            //int empid = query.First();

            //var query1 = from meet_emp in db.Meet_Emps
            //             where meet_emp.Employee_ID == empid
            //             select meet_emp.Meeting_ID;

            //int meetid = query1.First();

            //Response.Write(meetid);
        }
    }
}

Value of string "selected" is null after I select an item in list box.

回答1:

It's because you are running ListBox1.DataBind() on every cycle. Change to:

if (!IsPostBack)
{
    var query = from emp in db.Employees
                select emp.Employee_Name;

    ListBox1.DataSource = query;
    ListBox1.DataBind();
}


回答2:

You need to add a list item under this condition; !Page.IsPostBack. Because before the selected Index Change event is called, a Page PostBack event first occurs and your selection will be gone.

if(!Page.IsPostBack)
{
for (int i = 1; i < 13; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString()));
            DropDownList4.Items.Add(new ListItem(i.ToString()));
        }

        for (int i = 1; i < 32; i++)
        {
            DropDownList2.Items.Add(new ListItem(i.ToString()));
            DropDownList5.Items.Add(new ListItem(i.ToString()));
        }

        for (int i = 2010; i < 2021; i++)
        {
            DropDownList3.Items.Add(new ListItem(i.ToString()));
            DropDownList6.Items.Add(new ListItem(i.ToString()));
        }


        var query = from emp in db.Employees
                    select emp.Employee_Name;

        ListBox1.DataSource = query;
        ListBox1.DataBind();
}


回答3:

As you can see in ASP.NET Page Life Cycle Overview, the Page_Load will be executed first and then the event SelectedIndexChanged.

You only need to execute the code of binding the DropDownList once, because of ViewState. The rest of requests will be PostBack. So you need to validate adding the condition if(!Page.IsPostBack).

And you need to define DataValueField (which field of object bounded will be the Value) and DataTextField (which field of the object will be visible to the user).



回答4:

protected void Page_Load(object sender, EventArgs e) {    
   if(!IsPostBack)
   listBoxLoad();
}

Because, your listbox autopostback=true and your listbox clicked be postback and listBoxLoad reloaded again and index changed.