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