如何从代码隐藏中同时添加输入和验证控件

乍得

我正在iMIS内置的称为RiSE的CMS中工作(不推荐)

我正在构建一个插件类型的东西,它将允许程序后端的管理员构建联系表单。(真的很简单!!!!?!?)

因此,我不知道要处理什么或输入多少表格。

该过程适用于每个添加的输入

  1. 将其“ id”添加到提交表单时要使用的列表中
  2. 根据管理员选择的选项构建输入并添加属性
  3. 添加验证器

以下代码是将输入添加到页面的片段。这个想法很简单,管理员可以“创建”输入并选择其选项(名称,标签,占位符,只读等)

然后,后面的代码将采用这些选项并构建输入和对应的输入验证控件。

我不断收到此错误

找不到由“ val_9d8f153”的“ ControlToValidate”属性引用的控件ID“ text_9d8f153”。

这是文件;EmailFormTextBoxDisplay.ascx

<%@ Control Language="C#" AutoEventWireup="True" CodeBehind="EmailFormTextBoxDisplay.ascx.cs" Inherits="EmailForm.EmailFormTextBoxDisplay" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register TagPrefix="asiweb" Assembly="Asi.Web" Namespace="Asi.Web.UI.WebControls" %>

<div onprerender="buildMe" runat="server"></div>

EmailFormTextBoxDisplay.ascx.cs

using System
...
    protected void buildMe(object sender, EventArgs e)
    {
        HtmlGenericControl div = (HtmlGenericControl)sender;
        if (EmailForm.formProcessed)
        {
            div.Visible = false;
        }
        else
        {
            String id = inputEmailLabel.Replace(" ", "-") + "_" + randId;
            // add this input to the list
            EmailForm.inputs.Add(new String[]
            {
                id,
                inputType,
                inputEmailLabel
            });
            // if label is not empty, add it
            if (inputLabel != "")
            {
                HtmlGenericControl label = new HtmlGenericControl("label");
                label.InnerText = inputLabel + " ";
                label.TagName = "label";
                div.Controls.Add(label);
            }
            // build and add the input
            HtmlInputGenericControl input = new HtmlInputGenericControl("input");

            // get each setting and add attributes to the input
            input.Attributes.Add("type", inputType);
            input.Attributes.Add("id", id);
            input.Attributes.Add("placeholder", inputPlaceholder);
            if (inputValue != "") input.Attributes.Add("value", inputValue);
            if (inputDisabled) input.Attributes.Add("disabled", "disabled");
            if (inputReadOnly) input.Attributes.Add("readonly", "readonly");
            if (inputRequired)
            {
                input.Attributes.Add("required", "required");
                AsiRequiredFieldValidator required = new AsiRequiredFieldValidator();
                required.ControlToValidate = id;
                required.ID = "val_" + randId;
                required.Text = "This is Required";
                div.Controls.Add(required);
            }
            if (inputRegEx != "" && inputRegEx != null)
            {
                AsiRegularExpressionValidator regEx = new AsiRegularExpressionValidator();
                regEx.ValidationExpression = inputRegEx;
                regEx.ControlToValidate = id;
                regEx.ID = "regExVal_" + randId;
                regEx.Text = inputRegExMsg;
                div.Controls.Add(regEx);
            }

            div.Controls.Add(input);
        }
    }

我尝试将验证部分分解为自己的方法,然后调用第一部分(进行输入)onLoad,然后添加验证器,onPreRender但遇到相同的错误。

我还尝试使用Control.AddAt(2, input)来确保<input />验证器之前的,但无济于事。

如何动态建立输入和验证?

虚拟世界

您需要使用ID由ASPNET生成的引用,而不是由Attribute设置的引用这是因为该ID将由aspnet客户端重命名,因此html中的实际ID可能会变成这样。ctl00_PlaceHolder1_myID

HtmlInputGenericControl input = new HtmlInputGenericControl("input");
input.ID = id;

然后是验证器

required.ControlToValidate = input.ID;

您也可以使用“真实的” aspnet控件。

TextBox tb = new TextBox();
tb.ID = "myTextBox" + i;

RequiredFieldValidator val = new RequiredFieldValidator();
val.ControlToValidate = tb.ID;
val.ErrorMessage = "Required field";

Literal lit = new Literal();
lit.Text = "<br>";

PlaceHolder1.Controls.Add(tb);
PlaceHolder1.Controls.Add(val);
PlaceHolder1.Controls.Add(lit);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在此代码中如何隐藏输入字段:顶部,左侧和右侧边框?

在CrafterCMS中,如何添加验证特定范围的数字控件?

如何在colvis代码中添加全部显示和全部隐藏

如何验证ASP.NET MVC 4中隐藏的输入

如何从 ResourceDictionary 的代码隐藏访问 UI 控件

从代码隐藏中获取HTML控件

如何在JavaScript中识别和检查验证控件?

如何将UILongPressGestureRecognizer和UITapGestureRecognizer同时添加到同一控件?

WPF – 如何在代码隐藏中创建的控件模板中附加 VisualStateGroups?

如何从Web窗体用户控件中的标记访问代码隐藏中定义的属性

如何从 Python 中的输入同时附加和覆盖文件

如何同时验证多个输入字段

在页眉(th)上添加按钮以隐藏和显示引导表中的过滤器控件

如何在单个调用中同时获取隐藏文件和非隐藏文件

如何从代码隐藏(ASP.NET)中查找控件并将其更改为选中/未选中

如何验证离子中动态添加的离子输入

如何使用单元格的应用程序脚本将 Google 表格数据验证添加到列中,以仅允许输入颜色和十六进制颜色代码

如何验证用户在MFC属性网格控件(CMFCPropertyGridCtrl)中输入的每个字符

如何淡入和淡出视频中添加的控件,如默认控件

如何在表单的输入值中添加PHP代码

如何在UWP中隐藏MediaPlayer控件

如何隐藏/显示ListView控件中的列?

如何在 SAP Ui5 中添加验证,在用户端强制仅输入具有相应最大长度和最小长度的数字的输入字段中,

Javascript:如何在<p hidden>中添加和删除隐藏

如何基于 Blazor 中的选择控件显示或隐藏控件

如何在 Python 中验证数组的负 # 和 alpha 输入

如何为用户和管理员使用一种表单,但对用户隐藏一个输入控件?

同时验证多个输入

客户端验证无法在输入代码中添加“ onclick”命令