列表框中的选定项目为空

时间:2011-05-02 16:47:16

标签: c# .net asp.net listbox

列表框中的项目很少。当我选择一个项目时,我将它存储在一个字符串中以供进一步使用,但当我显示该字符串值时,它显示为null。

以下是我的Invitee.aspx文件的代码:

<%@ 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>

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

在列表框中选择项目后,字符串“selected”的值为空。

4 个答案:

答案 0 :(得分:7)

这是因为您在每个周期都运行ListBox1.DataBind()。改为:

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

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

答案 1 :(得分:6)

您需要在此条件下添加列表项; !Page.IsPostBack。因为在调用所选的Index Change事件之前,首先会发生Page PostBack事件,并且您的选择将会消失。

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

答案 2 :(得分:3)

正如您在ASP.NET Page Life Cycle Overview中看到的那样,首先执行Page_Load,然后执行SelectedIndexChanged事件。

由于ViewState,您只需要执行一次绑定DropDownList的代码。其余的请求将是PostBack。因此,您需要验证添加条件 if(!Page.IsPostBack)

您需要定义DataValueField(对象的哪个字段将是Value)和DataTextField(该对象的哪个字段对用户可见)。

答案 3 :(得分:0)

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

因为,您的列表框autopostback=true和您点击的列表框被点回,并且listBoxLoad再次重新加载并且索引已更改。