0

我已经面临以下问题并与之抗争了将近 2 天。我有以下代码。

        protected override void Render(HtmlTextWriter writer)
    {
        string controlOutput;
        string validatorOutput;
        string allOutput;
        StringWriter controlStringWriter = new StringWriter();
        HtmlTextWriter controlHtmlWriter = new HtmlTextWriter(controlStringWriter);

        StringWriter validatorsStringWriter = new StringWriter();
        HtmlTextWriter validatorsHtmlWriter = new HtmlTextWriter(validatorsStringWriter);

        base.Render(controlHtmlWriter);
        controlOutput = controlStringWriter.ToString();

        this.renderValidators(validatorsHtmlWriter);
        validatorOutput = validatorsStringWriter.ToString();

        allOutput = String.Format("{0} {1}", controlOutput, validatorOutput);
        writer.Write(allOutput);
    }

renderValidators 函数是:

  private void renderValidators(HtmlTextWriter writer)
        {
            foreach (BaseValidator validator in this.ValidatorsCollection)
            {
                validator.RenderControl(writer);
            }
        }

渲染函数返回一个空字符串。集合中的 Validator 的 Visible 属性设置为 true。

我在 OnInit 函数中添加验证器。在第一次渲染期间 validator.RenderControl(writer); 返回一个空字符串,回发后它开始返回一个 html 输入......

也许有人遇到过这样的问题???

4

1 回答 1

1

据我了解,您需要在呈现验证器之前检查控件是否已添加到页面中。

        Page page = Page;
        if (page == null || page.Request == null) {
            return false;
        }

可以在MSDN中找到一个非常有用的示例

于 2012-04-04T21:44:26.210 回答