【问题标题】:How to add both input and validation control from codebehind如何从代码隐藏添加输入和验证控件
【发布时间】:2018-04-16 08:54:45
【问题描述】:

我在一个名为 RiSE 的 CMS 中工作,该 CMS 内置于 iMIS(我不推荐)

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

所以,我不知道要处理什么或多少表单输入。

该过程针对每个添加的输入

  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) 来确保&lt;input /&gt; 在验证器之前,但无济于事。

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

【问题讨论】:

    标签: c# asp.net validation contact-form


    【解决方案1】:

    您需要使用由aspnet 生成的ID 引用,而不是由属性设置的那个。这是因为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);
    

    【讨论】:

    • +1 表示“真正的”aspnet 控件。如果没有 RiSE 垃圾,ASP 就足够糟糕了。这是我的问题。我没有意识到“属性”几乎是任意设置的。无论如何,隐形身份有什么好处???这很有效,谢谢你的例子。
    【解决方案2】:

    ControlToValidate 需要您要验证的控件的服务器端 ID。您需要像这样设置控件的 id:

    input.ID = "MY_ID";
    

    然后使用输入的 id 进行验证:

    required.ControlToValidate = input.ID;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多