Thank's for answering the questions. I found the solution. Here is the entire code of the server control. The trick was to implement IValidator. It gives us two property and one metod. ErrorMessage and IsValid properties and Validate method. I wrote all
the validation code in Validate method and set this.IsValid. This solved the problem.
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
public class MyControl : WebControl, IValidator
{
protected override void RenderContents(HtmlTextWriter output)
{
//Render the required html
}
protected override void Render(HtmlTextWriter writer)
{
this.RenderContents(writer);
}
protected override void OnInit(EventArgs e)
{
Page.Validators.Add(this);
base.OnInit(e);
}
public string ErrorMessage
{
get;
set;
}
public bool IsValid
{
get;
set;
}
public void Validate()
{
string code = Context.Request["txtCode"];
this.IsValid = Validate(code);//this method calls the webservice and returns true or false
if (!this.IsValid)
{
ErrorMessage = "Invalid Code";
}
}
}
nccsbim071
Member
24 Points
44 Posts
Re: integrate custom control controls validation with Page.IsValid
Jul 07, 2011 05:17 AM|LINK
Hi Guys,
Thank's for answering the questions. I found the solution. Here is the entire code of the server control. The trick was to implement IValidator. It gives us two property and one metod. ErrorMessage and IsValid properties and Validate method. I wrote all the validation code in Validate method and set this.IsValid. This solved the problem.
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")] public class MyControl : WebControl, IValidator { protected override void RenderContents(HtmlTextWriter output) { //Render the required html } protected override void Render(HtmlTextWriter writer) { this.RenderContents(writer); } protected override void OnInit(EventArgs e) { Page.Validators.Add(this); base.OnInit(e); } public string ErrorMessage { get; set; } public bool IsValid { get; set; } public void Validate() { string code = Context.Request["txtCode"]; this.IsValid = Validate(code);//this method calls the webservice and returns true or false if (!this.IsValid) { ErrorMessage = "Invalid Code"; } } }