ListBox 中獲得選中項的方法

 

主要記錄下ListBox的GetSelectedIndices()方法。返回一個表示所有選定項的整型數組。

//SelectionMode = "Single"
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    
{
       
if(this.ListBox1.SelectedIndex != -1)
        
{
           Response.Write(
this.ListBox1.SelectedItem.Text + "---------" + this.ListBox1.SelectedItem.Value+"<br>");
        }

    }


//SelectionMode = "Multiple"
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    
{
       
//方法一
        foreach (int i in this.ListBox1.GetSelectedIndices())
        
{
            ListItem li 
= this.ListBox1.Items[i];
            Response.Write(li.Text 
+ "---------" + li.Value+"<br>");
        }


       
//方法二
        foreach (ListItem li in this.ListBox1.Items)
        
{
            
if (li.Selected)
            
{
                Response.Write(li.Text 
+ "---------" + li.Value+"<br>");
            }

        }

    }
 
發佈了35 篇原創文章 · 獲贊 1 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章