繁体   English   中英

获取列表框的选定索引和选定值

[英]Get the selected index and selected value of a ListBox

我在获取列表框的选定索引和选定值时遇到问题。 我试图用数据库中的值填充列表框,此后,在选定索引更改事件中,我无法提取选定索引和选定值。 相反,我得到的选定索引为-1,并且选定的值不包含值。

这是单击ListBox中的任何项目之前的屏幕截图:

在此处输入图片说明

这是单击一个项目后拍摄的屏幕截图:

在此处输入图片说明

这是c#代码:

public partial class std_Course_dashboard : System.Web.UI.Page
{
    int index;
    string value;

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["uid"].ToString();
        Label2.Text = Session["crs_id"].ToString();
        ListBox1.Items.Clear();
        SqlDataReader r;
        SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No   from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
        con.Open();
        ListBox1.DataSource = cmd.ExecuteReader();
        ListBox1.DataTextField = "lecture_Title";
        ListBox1.DataValueField = "lecture_No";
        ListBox1.DataBind();
        con.Close();        
        ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        value = ListBox1.SelectedValue;
        index = ListBox1.SelectedIndex;
        Label3.Text = value;
        Label4.Text = index.ToString();
    }
}

这是因为每次页面发出请求时都会清除项目

ListBox1.Items.Clear();

您可以删除此行代码并添加if(!IsPostBack)以便仅在页面加载时才第一次加载数据

if(!IsPostBack)
{
         Label1.Text = Session["uid"].ToString();
          Label2.Text = Session["crs_id"].ToString();
          SqlDataReader r;
          SqlCommand cmd = new SqlCommand("select lecture_text,     lecture_Title,lecture_No   from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
          con.Open();
          ListBox1.DataSource = cmd.ExecuteReader();
          ListBox1.DataTextField = "lecture_Title";
          ListBox1.DataValueField = "lecture_No";
          ListBox1.DataBind();
          con.Close();        
          ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}

如果在Page_Load中放置一个断点,您会注意到该断点会在每个PostBack上命中。 既然发生了这种情况,您的代码将经过设计,以便您的ListBox每次回发时(包括当您在ListBox中选择新索引时)都与数据反弹。 这就是导致您的SelectedIndex重置的原因。

您只需要绑定一次ListBox。 您可以通过检查Page.IsPostBack条件来实现。 如果PostBack不是由客户端引起的,请绑定ListBox。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Label1.Text = Session["uid"].ToString();
        Label2.Text = Session["crs_id"].ToString();
        ListBox1.Items.Clear();
        SqlDataReader r;
        SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No   from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
        con.Open();
        ListBox1.DataSource = cmd.ExecuteReader();
        ListBox1.DataTextField = "lecture_Title";
        ListBox1.DataValueField = "lecture_No";
        ListBox1.DataBind();
        con.Close();        
        ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
    }
}

暂无
暂无

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

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