2012-11-15 54 views
0

我有6個從sqldatasource填充的dropdownlist。我也有另一個返回一些行的sqldatasource。我想要做的是遍歷第二個數據源的每一行,並在我的下拉列表中選擇該值。因此,如果第二個數據源包含三行,它會在前三個下拉列表中選擇適當的值,並將其他值設置爲「N/A」。從sqldatamart選擇下拉列表項目asp.net

這裏是一些僞代碼,我認爲

protected void fileattributes_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
     DropDownList kw1 = (DropDownList)e.Item.FindControl("keyw1"); 
     DropDownList kw2 = (DropDownList)e.Item.FindControl("keyw2"); 
     DropDownList kw3 = (DropDownList)e.Item.FindControl("keyw3"); 
     DropDownList kw4 = (DropDownList)e.Item.FindControl("keyw4"); 
     DropDownList kw5 = (DropDownList)e.Item.FindControl("keyw5"); 
     DropDownList kw6 = (DropDownList)e.Item.FindControl("keyw6"); 

     DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 }; 

     for(int i = 0; i<sqldatasource2.length;i++) 
     {    
       array[i].SelectedItem.Text = sqldatasource2.item  
     } 

     foreach(Array a in array) 
     {  
       if (a is null) 
       {  
        a.selecteditem.text = "N/A"; 
       }   
     }   
}  
+0

希望我的回答可以解決問題 – Sami

回答

1
DataSourceSelectArguments args = new DataSourceSelectArguments(); 
DataView view = (DataView)SqlDataSource1.Select(args); 
DataTable dt = view.ToTable(); 

DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 }; 
int i; 
for (i = 0; i < dt.Rows.Count;i++) 
{ 
    array[i].SelectedItem.Text = dt.Rows[i][0].ToString(); 
} 

// If array length (number of DropDowns) is greater than rows in datasource 
// Remaining Dropdowns will get text N/A --- Now i=dt.Rows.Count 
for (; i < array.Length; i++) 
{ 
    array[i].SelectedItem.Text = "N/A"; 
} 
+0

的datasourceselectarguments是任何參數傳遞我在SqlDataSource?我如何創建一個而不是傳遞一個空白的? – user541597

1

我不是很有經驗直接與SqlDataSource的工作,所以我所做的就是填補與結果集的DataTable。希望這個作品!

DropDownList kw1 = (DropDownList)e.Item.FindControl("keyw1"); 
    DropDownList kw2 = (DropDownList)e.Item.FindControl("keyw2"); 
    DropDownList kw3 = (DropDownList)e.Item.FindControl("keyw3"); 
    DropDownList kw4 = (DropDownList)e.Item.FindControl("keyw4"); 
    DropDownList kw5 = (DropDownList)e.Item.FindControl("keyw5"); 
    DropDownList kw6 = (DropDownList)e.Item.FindControl("keyw6"); 

    DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 }; 

    DataView dv = new DataView(); 
    DataTable dt = new DataTable();  
    dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); 
    dt = dv.ToTable(); 

    int i = 0; 
    foreach (DataRow dr in dt.Rows) 
    { 
     // dr["column name or column index"] (zero in my case) 
     array[i].SelectedItem.Text = dr[0].ToString(); 
     i++; 
    } 
相關問題