I am working within a CMS known as RiSE built in iMIS (I don't recommend)
I am building a plugin type thing which will allow the administrator from the backend of the program to build a contact form. (really simple right!?!?!?)
So, I don't know what, or how many form inputs will be present to process.
the process is for each added input
- add its "id" to a list to be used when the form is submitted
- build the input and add attributes based on administrator selected options
- add the validator
The following code is the piece the adds the inputs to the page. The idea is simple, the administrator "creates" the input and chooses their options (name, label, placeholder, readonly, etc)
The codebehind would then take these options and build the input and corosponding input validation control.
I keep getting this error
Unable to find control id 'text_9d8f153' referenced by the 'ControlToValidate' property of 'val_9d8f153'.
here are the files; 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);
}
}
I have tried breaking the validation part out into it's own method, and calling the first part (making the input) onLoad and then adding the validators onPreRender but I get the same error.
I also tried using Control.AddAt(2, input) to ensure the <input /> is before the validator, but to no avail.
How can I build an input & validation dynamically?