我有一个内置代码的表单。
我想要做的是检测单选按钮何时被选中,以及它何时被选中。
我有以下
RadioButton radio = new RadioButton() { ClientIDMode = System.Web.UI.ClientIDMode.Static, AutoPostBack=true, ID = "rbOther" + testEquipment.Id, GroupName = "grp" + testEquipment.Id, Checked = false };
radio.CheckedChanged += new EventHandler(radio_CheckedChanged);
testEquipment 是存储一些数据的对象的实例。
tdBrandName.Controls.Add(new TextBox() { ClientIDMode = System.Web.UI.ClientIDMode.Static, ID = "txtBrandName" + testEquipment.Id, Text = serviceStationTestEquipment != null ? serviceStationTestEquipment.BrandName : string.Empty, Width = new Unit(300), Enabled = serviceStationTestEquipment != null ? serviceStationTestEquipment.Other : false });
tdBrandName.Controls.Add(new RequiredFieldValidator() { Display = ValidatorDisplay.Dynamic, ControlToValidate = "txtBrandName" + testEquipment.Id, Text = "Required field", ErrorMessage = "Test Equipment Tab: Field is required", CssClass = "validationerror", Enabled = serviceStationTestEquipment != null ? serviceStationTestEquipment.Other : false, ID = ID = "reqValidator" + testEquipment.Id });
然后,我想在单击单选按钮时禁用文本框和 requiredfieldvalidator。有三个即。无,默认,其他
选择其他类型时,文本框必须与所需的字段验证器一起启用。
void radio_CheckedChanged(object sender, EventArgs e)
{
RadioButton check = sender as RadioButton;
if (check == null)
return;
string Id = check.ID.Replace("rbOther", "");
TextBox text = check.Parent.Parent.FindControl(string.Format("txtBrandName{0}", Id)) as TextBox;
text.Enabled = check.Checked;
RequiredFieldValidator validator = check.Parent.Parent.FindControl(string.Format("reqValidator{0}", Id)) as RequiredFieldValidator;
validator.Enabled = check.Checked;
}