Skip to content

Infragistics Community Forum / Web / Ultimate UI for ASP.NET Web Forms / Cannot dynamically Render Required Field Validator & a mandatory symbol

Cannot dynamically Render Required Field Validator & a mandatory symbol

New Discussion
Govind Karanam
Govind Karanam asked on Apr 2, 2012 10:31 PM

Unable to render a Required Field Validator while preparing a Custom Control inheriting from WebTextEditor.

It is displaying the following errors

1. Required a script manager in the page.

After adding a script manager it is displaying the following error now.

1. Unable to find control id 'CustomTextBox1' referenced by the 'ControlToValidate' property of 'rfvCustomTextBox1'.

Custom Control Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI;
using System.ComponentModel;
using Infragistics.Web.UI.EditorControls;

namespace CustomControls
{
    [ToolboxBitmap(typeof(WebTextEditor)), ToolboxData("<{0}:CustomTextBox runat=server></{0}:CustomTextBox>")]
    public class CustomTextBox : WebTextEditor
    {
        private RequiredFieldValidator requiredFieldValidator;
        private ValidatorDisplay _display;

        public CustomTextBox()
        {
        }

        public virtual bool EnableRequiredValidation
        {
            get
            {
                object obj = this.ViewState["EnReq"];
                return (obj != null && (bool)obj);
            }
            set
            {
                this.ViewState["EnReq"] = value;
            }
        }

        public virtual string RequiredFieldValidatorErrorMessage
        {
            get
            {
                object obj = this.ViewState["rfvErrMsg"];
                return (obj != null ? Convert.ToString(obj) : null);
            }
            set
            {
                this.ViewState["rfvErrMsg"] = value;
            }
        }

        public virtual string ValidationGroup
        {
            get
            {
                object obj = this.ViewState["valGrp"];
                return (obj != null ? Convert.ToString(obj) : null);
            }
            set
            {
                this.ViewState["valGrp"] = value;
            }
        }

        public virtual string RequiredFieldValidatorInitialValue
        {
            get
            {
                object obj = this.ViewState["rfvIniVal"];
                return (obj != null ? Convert.ToString(obj) : null);
            }
            set
            {
                this.ViewState["rfvIniVal"] = value;
            }
        }

        [Category("Validation Properties"), Description("Display Validation Message with Validation Control"), DefaultValue(ValidatorDisplay.None)]
        public virtual ValidatorDisplay Display
        {
            get
            {
                return this._display;
            }
            set
            {
                this._display = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ValidateControls();
        }

        private void ValidateControls()
        {
            if (EnableRequiredValidation == true)
            {
                this.requiredFieldValidator = new RequiredFieldValidator();
                this.requiredFieldValidator.ID = string.Format("rfv{0}", this.ID);
                this.requiredFieldValidator.ErrorMessage = this.RequiredFieldValidatorErrorMessage;
                this.requiredFieldValidator.ValidationGroup = this.ValidationGroup;
                this.requiredFieldValidator.ControlToValidate = this.ID;
                this.requiredFieldValidator.InitialValue = this.RequiredFieldValidatorInitialValue;
                this.requiredFieldValidator.Display = this.Display;
                this.Controls.Add(requiredFieldValidator);
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (EnableRequiredValidation && this.requiredFieldValidator != null)
            {
                this.requiredFieldValidator.RenderControl(writer);
            }
            base.Render(writer);
        }
    }
}

WebClient

<asp:Content ID="ContentPlaceHolder1" ContentPlaceHolderID="MainContent" Runat="Server">
    <table>
        <tr>
            <td>
                <UPP:CustomTextBox ID="CustomTextBox1" runat="server" EnableRequiredValidation="True"
                    RequiredFieldValidatorErrorMessage="Enter Value" ValidationGroup="abc" />
            </td>
        </tr>
        <tr>
            <td>
                <ctl:ctlbutton id="CtlButton1" runat="server" text="Click Me" causesvalidation="true"
                    onclick="CtlButton1_Click" validationgroup="abc" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:ValidationSummary runat="server" ShowMessageBox="True" ShowSummary="False" ValidationGroup="abc">
                </asp:ValidationSummary>
            </td>
        </tr>
    </table>
</asp:Content>

The above custom control CustomTextBox is working fine if it is inherited from Dotnet 'TextBox' Control.

Please help me in this issue as early as possible.

In addition to the above logic in customTextBox want to render a mandatory symbol next to the CustomTextBox control.

Sign In to post a reply

Replies

  • 0
    [Infragistics] Viktor Snezhko
    [Infragistics] Viktor Snezhko answered on Jan 17, 2011 10:55 PM

    Hi Govind,

    The WebTextEditor was not designed as a container and adding a dynamic child breaks logic.

    I suggest you to add dynamic validator to form, but not to editor and do not use "Render" or other methods of editor to render validator.

    1. Remove protected override void Render
    2. Replace
      this.Controls.Add(requiredFieldValidator);
      by
      this.Page.Form.Controls.Add(requiredFieldValidator);

    I can also suggest to add/create dynamic validator within PageLoadComplete, but not within this.OnInit.
    Something like

    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      this.Page.LoadComplete += new EventHandler(this.ValidateControls);
    }
    private void ValidateControls(object sender, System.EventArgs e)
    {
        …
        this.Page.Form.Controls.Add(requiredFieldValidator);
    }

    • 0
      Antonio
      Antonio answered on Apr 2, 2012 8:37 AM

      sorry… wrong post

      • 0
        [Infragistics] Viktor Snezhko
        [Infragistics] Viktor Snezhko answered on Apr 2, 2012 10:31 PM

        Hi antoweb,

        If you move control on server to another container, then view state of that control will be probably lost.

        Try to keep control not moved, or you may try to recreate control dynamically (no move) in every postback and use exactly same ID and other properties of that dynamic control. In this case server will probably maintain its viewState.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Govind Karanam
Favorites
0
Replies
3
Created On
Apr 02, 2012
Last Post
13 years, 11 months ago

Suggested Discussions

Created by

Created on

Apr 2, 2012 10:31 PM

Last activity on

Apr 2, 2012 10:31 PM