页: 1 DropDownList . 当你选择<代码>DropDownList的数值时,该数值改变了相应的<代码>TextBox的背景颜色。
To make this easier, I tried to create a Dictionary<TextBox, DropDownList>
, but the Key
property is System.Web.UI.WebControls.TextBox
and the Value
property is System.Web.UI.WebControls.DropDownList
, instead of TextBox1
and DropDownList1
.
如何通过<代码>Dictionary,适当设定TextBox
和DropDownList
数值?
namespace TextBoxColorPicker
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
get_box_colors();
}
protected void get_box_colors()
{
var textbox_dict = new Dictionary<TextBox, DropDownList>();
textbox_dict.Add(TextBox1,DropDownList1);
textbox_dict.Add(TextBox2,DropDownList2);
textbox_dict.Add(TextBox3, DropDownList3);
foreach (KeyValuePair<TextBox, DropDownList> entry in textbox_dict)
{
TextBox txtbox = entry.Key;
DropDownList list = entry.Value;
Label1.Text = txtbox.ToString();
Label2.Text = list.ToString();
if (list.SelectedValue == "R")
{
txtbox.BackColor = System.Drawing.Color.Red;
}
else if (list.SelectedValue == "A")
{
txtbox.BackColor = System.Drawing.Color.Gold;
}
else if (list.SelectedValue == "G")
{
txtbox.BackColor = System.Drawing.Color.Lime;
}
else
{
txtbox.BackColor = System.Drawing.Color.White;
}
}
}
}
}