CreateFieldRenderingControl

CreateFieldRenderingControl

本文介绍了Page_ClientValidate() 对象预期错误,找不到验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单 HomePage.aspx 包含一个空的 asp:Panel,一个下拉列表让用户选择一个 SPFielType... 在索引改变时,我的 HomePage.aspx.cs 页面将选择文本并加载用户控件在面板内,此用户控件将根据用户选择的 spfieldtype 和调用 validateForm() 函数的按钮生成一个控件……我的问题是 validateForm() 中的 Page_ClientValidate() 函数找不到验证器,我也尝试给出一个组名,但仍然无法正常工作.
当我将按钮放入我的 aspx 页面(不动态呈现)时,它正在验证我的页面.( <asp:Button ID="submitbutton" Text="Validate" runat="server"/>).
但是当我动态渲染它时,无法验证表单..这就是我想要做的:

I have a form HomePage.aspx containing an empty asp:Panel, a dropdownlist letting the user pick an SPFielType... on index changed, my HomePage.aspx.cs page will get the text selected and will load a user control inside the panel, this user control will generate a control based on the spfieldtype chosen by the user and a button calling the validateForm() function... my problem is that the Page_ClientValidate() function inside the validateForm() cant find the validator, i also tried to give a groupname but still not working.
When Im putting the button inside my aspx page (not rendering dynamically) it is validating my page.( <asp:Button ID="submitbutton" Text="Validate" runat="server" />).
But when Im rendering it dynamically , cant validate the form..This is what Im trying to do:

protected override void CreateChildControls()
{
    try
    {
        fieldRenderingControl = this.CreateFieldRenderingControl(this.FieldType);
        this.Controls.Add(fieldRenderingControl);

        Button button = new Button();
        button.UseSubmitBehavior = false;
        button.Text = "ValidateButton";
        button.ID = "ValidateButton";
        button.OnClientClick = "validateForm()";
        this.Controls.Add(button);

        RequiredFieldValidator newValidator = new RequiredFieldValidator();
        newValidator.Text = "***";
        newValidator.ID = "valideee";
        newValidator.EnableClientScript = true;
        newValidator.Enabled = true;
        newValidator.SetFocusOnError = true;
        newValidator.Display = ValidatorDisplay.Dynamic;
        newValidator.ControlToValidate = fieldRenderingControl.ID;
        this.Controls.Add(newValidator);

    }
    catch (Exception ex)
    {
    }

}

//CreateFieldRenderingControl() 函数将根据用户选择的参数 fieldType 生成控件.

// the CreateFieldRenderingControl() function will generate a control based on the argument fieldType chosen by the user.

提前致谢.

推荐答案

Sharepoint 有一个丑陋的怪癖,它可以指定一个 Guid 作为控件的 id.我见过由 Sharepoint 生成的 JavaScript 试图将这些 Guid 用作变量名.这不好 - 它会破坏脚本 - JavaScript 变量名称中不允许使用破折号.我怀疑这是您遇到的问题.而且,我猜罪魁祸首是 this.CreateFieldRenderingControl().看起来那个方法正在生成一个Id……那个Id是一个Guid吗?如果是这样,请尝试用安全的东西覆盖 Id,也许只是从 Guid 中删除破折号.

Sharepoint has an ugly quirk where it can assign a Guid to be the id of a control. I've seen JavaScript that was generated by Sharepoint trying to use these Guids as variable names. This is no good - it breaks scripts - dashes aren't allowed in JavaScript variable names. I suspect this is the problem you are experiencing. And, I'd guess the culprit is this.CreateFieldRenderingControl(). It looks like that method is generating an Id... is that Id a Guid? If so, try to overwrite the Id with something safe, perhaps just remove the dashes from the Guid.

fieldRenderingControl.ID.Replace("-", "");

如果这不是确切的解决方案,希望这足以让您指明正确的方向.

If this isn't the exact solution, hopefully it's enough to get you pointed in the right direction.

这篇关于Page_ClientValidate() 对象预期错误,找不到验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:17