【发布时间】:2010-04-29 12:36:15
【问题描述】:
我有一个绑定到许多自定义数据项/类型的中继器
在转发器的 itemdatabound 事件中,代码调用 renderit 函数,该函数将根据自定义数据类型呈现自定义控件。它还将(如果设置了验证标志)为适当的渲染编辑控件渲染验证控件
编辑控件覆盖了自定义控件的 CreateChildControls() 方法,从而添加了一些文字控件
protected override void CreateChildControls()
{
//other bits removed - but it is this 'hidden' control i am trying to validate
this.Controls.Add(new LiteralControl(string.Format(
"<input type=\"text\" name=\"{0}\" id=\"{0}\" value=\"{1}\" style=\"display:none;\" \">"
, this.UniqueID
, this.MediaId.ToString())
));
//some other bits removed
}
验证控件是这样渲染的:其中传入的editcontrol是上面createchildcontrols方法的控件实例。
public override Control RenderValidationControl(Control editControl)
{
Control ctrl = new PlaceHolder();
RequiredFieldValidator req = new RequiredFieldValidator();
req.ID = editControl.ClientID + "_validator";
req.ControlToValidate = editControl.UniqueID;
req.Display = ValidatorDisplay.Dynamic;
req.InitialValue = "0";
req.ErrorMessage = this.Caption + " cannot be blank";
ctrl.Controls.Add(req);
return ctrl;
}
问题是,尽管验证控件的 .ControlToValidate 属性设置为编辑控件的唯一 ID。 当我点击页面时,我收到以下错误: 找不到“FieldRepeater_ctl01_ctl00_validator”的“ControlToValidate”属性引用的控件 ID“FieldRepeater$ctl01$ctl00”。
我尝试将 createchildcontrols 中的文字更改为新的 TextBox(),然后设置 id 等,但我遇到了类似的问题。
谁能启发我? 这是因为控件呈现的顺序吗?即验证控件写在editcontrol 之前? 或者……
任何帮助都非常感谢
谢谢
自然
【问题讨论】:
-
editControl 是 CustomControl 还是任何基本的 asp .net 输入控件?
标签: c# asp.net validation custom-controls