我正在尝试将 RequiredFieldValidator 动态添加到中继器中的 RadioButtonList 中,但它失败并出现错误:
Unable to find control id 'rblAccessory_40' referenced by the 'ControlToValidate' property of ''.
本节的代码是:
if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
{
Label lblAccID = (Label)e.Item.FindControl("lblAccID");
RadioButtonList rblCondition = (RadioButtonList)e.Item.FindControl("rblCondition");
rblCondition.ID = "rblAccessory_" + lblAccID.Text;
if (conditionList.Count() > 0)
{
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = "rblAccessory_" + lblAccID.Text;
rfv.ErrorMessage = "Please complete the accessories section";
pnlValidation.Controls.Add(rfv);
rblCondition.DataSource = conditionList;
rblCondition.DataValueField = "id";
rblCondition.DataBind();
}
foreach (ListItem li in rblCondition.Items)
{
li.Text = "";
li.Value = "AccessoryID_" + lblAccID.Text + "-ConditionID_" + li.Value;
}
}
}
它肯定找到了 RadioButtonList (rblCondition),因为此时数据已正确绑定:
rblCondition.DataSource = conditionList;
rblCondition.DataValueField = "id";
rblCondition.DataBind();
所以我不明白为什么错误说它无法找到控件ID。
我尝试手动指定控件 ID,如下所示:
rfv.ControlToValidate = "rblAccessory_" + lblAccID.Text;
并且还尝试过:
rfv.ControlToValidate = rblCondition.ID;
lblAccID 是一个隐藏文本字段,用于存储中继器中行的 ID。