2016-05-09 17 views
2

ListBoxの選択されたインデックスと選択された値を取得する際に問題があります。リストボックスにデータベースからの値を入力しようとしていますが、その後、選択したインデックス変更イベントで、選択したインデックスと選択した値を抽出できません。代わりに、選択インデックスが-1になり、選択された値に値が含まれません。ここでListBoxの選択されたインデックスと選択された値を取得

は、リストボックスで任意の項目をクリックする前にスクリーンショットです:

enter image description here

そして、これは、アイテムをクリックした後に撮影したスクリーンショットである:ここで

enter image description here

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

答えて

3

これは、アイテムをページでは、このコード行を削除した場合にのみページが読み込ま初めてif(!IsPostBack)ので、データのロードを追加することができます

ListBox1.Items.Clear(); 

を要求する各時間をクリアするのである

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

Page_Load内にブレークポイントを配置すると、PostBackごとにヒットすることがわかります。これにより、あなたのコードは、ListBoxで新しいインデックスを選択するときを含めて、PostBackのたびにデータでリバウンドするように設計されています。これがあなたの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")); 
    } 
} 
関連する問題