2

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

  1. add its "id" to a list to be used when the form is submitted
  2. build the input and add attributes based on administrator selected options
  3. 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?

2 Answers 2

1

You need to use reference the ID generated by aspnet, not the one set by an Attribute. THis is because the ID will be renamed by aspnet client side, so the actual ID in html might become something like this ctl00_PlaceHolder1_myID

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

and then the Validator

required.ControlToValidate = input.ID;

You could also use "real" aspnet Controls.

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);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for "real" aspnet controls. ASP is bad enough without RiSE garbage on top. This was my issue. I didn't realize that the "attributes" are being set almost arbitrarily. What good is an invisible id anyway??? This worked, thanks for the example.
0

ControlToValidate requires the server side id of the control you want to validate. You will need to set the id of the control like this:

input.ID = "MY_ID";

Then use the inputs id to validate:

required.ControlToValidate = input.ID;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.