【发布时间】:2018-04-16 08:54:45
【问题描述】:
我在一个名为 RiSE 的 CMS 中工作,该 CMS 内置于 iMIS(我不推荐)
我正在构建一个插件类型的东西,它将允许程序后端的管理员构建一个联系表单。 (真的很简单吧!?!?!?)
所以,我不知道要处理什么或多少表单输入。
该过程针对每个添加的输入
- 将其“id”添加到提交表单时要使用的列表中
- 根据管理员选择的选项构建输入并添加属性
- 添加验证器
以下代码是将输入添加到页面的部分。这个想法很简单,管理员“创建”输入并选择他们的选项(名称、标签、占位符、只读等)
然后,代码隐藏将采用这些选项并构建输入和对应的输入验证控件。
我不断收到此错误
找不到由“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 /> 在验证器之前,但无济于事。
如何动态构建输入和验证?
【问题讨论】:
标签: c# asp.net validation contact-form