2012-06-18 49 views
0

我有相關的給用戶的ListBox。用戶需要能夠選擇一個或多個選項,並保存在SQL數據庫這些選項,但我只能保存一個選項。如何選擇ListBox中的多個選項?

當我建立了我的形式我用了「啓用回傳選項」,在我的「selecction模式」我有:multiple

這裏是我的代碼:

//IN MY FORM 

if (!IsPostBack) 
{ 
    ClLinea_1 seleccion = new ClLinea_1(); 
    DataSet ds = seleccion.sqlSelectLinea_1(); 
    DataTable dt = ds.Tables[0]; 
    ListBoxLinea_1.DataSource = dt; 
    ListBoxLinea_1.DataTextField = "descripcion"; 
    ListBoxLinea_1.DataValueField = "id_linea_1"; 
    ListBoxLinea_1.DataBind(); 
} 

// IN MY BUTTON 
protected void btnInsertaLinea_1_Click(object sender, EventArgs e) 
{ 
    ClLinea_1 inserta = new ClLinea_1(); 
    inserta.SqlSeleccionLinea_1(int.Parse(ListBoxLinea_1.SelectedValue.),int.Parse 
    (txtUsuario.Text)); 
} 

回答

2

那麼你需要確保mulitiselect屬性設置爲true。

然後使用類似。

foreach(var item in MyListBox.SelectedItems) 
{ 
    int value; 
    if (int.TryParse(item.ToString(), out value) 
    { 
     // insert to db here. 
    } 
} 
相關問題