我有一个从sqldatasource填充的6个下拉列表。我还有另一个sqldatasource返回一些行。我想要做的是遍历第二个数据源的每一行,并在我的下拉列表中选择该值。因此,如果第二个数据源包含三行,它将在前三个下拉列表中选择适当的值,并将其他数据集设置为“N / A”。
这是我想到的一些伪代码
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 :(得分: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++;
}
答案 1 :(得分: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";
}