Cannot dynamically Render Required Field Validator & a mandatory symbol
New DiscussionUnable 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.