【问题标题】:validation control unable to find its control to validate验证控件无法找到要验证的控件
【发布时间】: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


【解决方案1】:

你必须使用editControl.ID 而不是editControl.UniqueID

还要考虑到如果editControl 必须是可以与必填字段vaidator 一起使用的东西。这个验证器对所有输入控件没有意义。

【讨论】:

  • 您好,感谢您的回复,是的,我有许多用于验证的不同自定义数据类型的 ovverides。但我应该使用 editControl.ID 即使控件是用 id 和 name = this.UniqueID 呈现的?
  • 所以我根本无法验证这一点?即使editcontrol 包含一个标准的文本框?我可以使用自定义验证器吗?谢谢
  • 您好,再次感谢您,添加验证属性成功了
猜你喜欢
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
相关资源
最近更新 更多