如题
高手出来一个帮我解决一下,如何解决用foreach 来遍历出所有的DropDownList 并且赋值,具体方法如下:
/// <summary>
/// 初始化一个字典类
/// </summary>
private Dictionary<int, string> dropItems = new Dictionary<int, string>(); #region private void SetDictionnary()
{
dropItems.Add(1, "1:"+this.GetStringById("1"));
dropItems.Add(2, "2:"+this.GetStringById("2"));
dropItems.Add(3, "3:"+this.GetStringById("3"));
dropItems.Add(4, "4:"+this.GetStringById("4"));
dropItems.Add(5, "5:"+this.GetStringById("5"));
dropItems.Add(6, "6:"+this.GetStringById("6"));
dropItems.Add(7, "7:"+this.GetStringById("7"));
} #endregion
/// <summary>
/// 设置DropDownList
/// </summary>
private void SetDropDownList()
{
foreach (Control ctrl in this.TabPanel1.Controls)
{
if(ctrl is DropDownList)
{
DropDownList drop = (DropDownList) ctrl;
drop.DataSource = dropItems;
drop.DataTextField = "Value";
drop.DataValueField = "Key";
drop.DataBind();
}
}
}
高手出来一个帮我解决一下,如何解决用foreach 来遍历出所有的DropDownList 并且赋值,具体方法如下:
/// <summary>
/// 初始化一个字典类
/// </summary>
private Dictionary<int, string> dropItems = new Dictionary<int, string>(); #region private void SetDictionnary()
{
dropItems.Add(1, "1:"+this.GetStringById("1"));
dropItems.Add(2, "2:"+this.GetStringById("2"));
dropItems.Add(3, "3:"+this.GetStringById("3"));
dropItems.Add(4, "4:"+this.GetStringById("4"));
dropItems.Add(5, "5:"+this.GetStringById("5"));
dropItems.Add(6, "6:"+this.GetStringById("6"));
dropItems.Add(7, "7:"+this.GetStringById("7"));
} #endregion
/// <summary>
/// 设置DropDownList
/// </summary>
private void SetDropDownList()
{
foreach (Control ctrl in this.TabPanel1.Controls)
{
if(ctrl is DropDownList)
{
DropDownList drop = (DropDownList) ctrl;
drop.DataSource = dropItems;
drop.DataTextField = "Value";
drop.DataValueField = "Key";
drop.DataBind();
}
}
}
{
if(ctrl is DropDownList)
{
DropDownList drop = (DropDownList) ctrl;
drop.DataSource = dropItems;
drop.DataTextField = "Value";
drop.DataValueField = "Key";
drop.DataBind();
}
}
这个是不正确的
{
if(ctrl is DropDownList)
{
DropDownList drop = ctrl as DropDownList;
}
}