如题:
我想把字符串"RadioButton1"强转成RadioButton按钮
比如
1. RadioButton aaa = new RadioButton()
2. aaa = (ControlIDConverter)("RadioButton1");
3. aaa .Checked = true;当然,第2步是错的...请问正确第2步咋写?...
在线=答案
我想把字符串"RadioButton1"强转成RadioButton按钮
比如
1. RadioButton aaa = new RadioButton()
2. aaa = (ControlIDConverter)("RadioButton1");
3. aaa .Checked = true;当然,第2步是错的...请问正确第2步咋写?...
在线=答案
this.radioButton1.Location = new System.Drawing.Point(82, 112);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(95, 16);
this.radioButton1.TabIndex = 0;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
t.Text = "RadioButton";
panel1.Controls.Add(t);
Type _ControlType=_Assembly.GetType("System.Windows.Forms."+"RadioButton"); RadioButton _Control = (RadioButton)Activator.CreateInstance(_ControlType);如果你不知道 Assembly 那来的 尝试反射所有的程序集。参考
http://blog.csdn.net/zgke/archive/2009/03/11/3980137.aspx
Type _ControlType=_Assembly.GetType("System.Windows.Forms."+"RadioButton"); RadioButton _Control = (RadioButton)Activator.CreateInstance(_ControlType);
我现在有5个RadioButton 分别叫RadioButton1,RadioButton2,RadioButton3,RadioButton4,RadioButton5
我可以从数据库里获取到用户选的值是几
比如从数据库里得到值"2"
于是我就想要RadioButton2.Checked = true;
当然,我可以写成
switch (数据库里的值)
{
case 1:
RadioButton1.Checked = true;
break;
case 2:
RadioButton2.Checked = true;
break;
case 3:
RadioButton3.Checked = true;
break;
case 4:
RadioButton4.Checked = true;
break;
case 5:
RadioButton5.Checked = true;
break;
}
但是由于RadioButton数量过多(30多个,不能写那么冗长的switch)
所以我只能这样写
string RadioButtonName = "RadioButton" + "数据库里的值";
RadioButton aaa = (ControlIDConverter)(RadioButtonName);
aaa.Checked = true;
大家理解了我的问题了吗?
RadioButton TextBoxa = (RadioButton)this.FindControl(stra);
比如
RADIOBUTTON1.TAG=1;
RADIOBUTTON2.TAG=2;
如果数据库中取到num
foreach (Control ctl in this.Controls)
{
if (ctl.Tag.ToString() == num.ToString())
{
((RadioButton)ctl).Checked=True;
}
}