简体   繁体   中英

Get the selected index and selected value of a ListBox

I am having problems getting the selected index and selected value of a ListBox. I am trying to fill the list box with values from a database and after this, on selected index change event, I cannot extract the selected index and selected value. Instead, I get a selected index of -1 and the selected value does not contain a value.

Here is a screenshot before clicking any item in the ListBox:

在此处输入图片说明

And this is a screen shot taken after clicking an item:

在此处输入图片说明

Here is the c# code:

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

This is because of clearing the items each time the page makes request

ListBox1.Items.Clear();

you can remove this line of code and adding if(!IsPostBack) so that data loads only for the first time when page loads

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

If you place a breakpoint within your Page_Load, you will notice that it hits on every single PostBack. Since this happens, your code is designed such that your ListBox gets rebound with data every time you PostBack - including when you select a new index in your ListBox. This is what is causing your SelectedIndex to get reset.

What you need to do is only bind your ListBox once. You can achieve this by checking the Page.IsPostBack condition. If the PostBack is not caused by the client, bind the 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"));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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