Create New Validator Control in ASP.NET

Posted: December 5, 2010 in ASP.NET
Tags: , ,

ListValidator.cs Code

public class ListValidator : BaseValidator
{
public ListValidator()
{}

protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate) as ListControl;
return (ctrl != null);
}

protected override bool EvaluateIsValid()
{
return this.CheckIfItemIsChecked();
}

protected bool CheckIfItemIsChecked()
{
ListControl listItemValidate = ((ListControl)this.FindControl(this.ControlToValidate));
foreach (ListItem listItem in listItemValidate.Items)
{
if (listItem.Selected == true)
return true;
}
return false;
}

protected override void OnPreRender(EventArgs e)
{
// Determines whether the validation control can be rendered
// for a newer (“uplevel”) browser.
// check if client-side validation is enabled.
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, “evaluationfunction”, “CheckIfListChecked”);
this.CreateJavaScript();
}
base.OnPreRender(e);
}

protected void CreateJavaScript()
{
StringBuilder sb = new StringBuilder();
sb.Append(@”<script type=””text/javascript””>function CheckIfListChecked(ctrl){“);
sb.Append(@”var chkBoxList = document.getElementById(document.getElementById(ctrl.id).controltovalidate);”);
sb.Append(@”var chkBoxCount= chkBoxList.getElementsByTagName(“”input””);”);
sb.Append(@”for(var i=0;i<chkBoxCount.length;i++){“);
sb.Append(@”if(chkBoxCount.item(i).checked){“);
sb.Append(@”return true; }”);
sb.Append(@”}return false; “);
sb.Append(@”}</script>”);
Page.ClientScript.RegisterClientScriptBlock(GetType(), “JSScript”, sb.ToString());
}
}

Custom Validator Control – Usage – Client Side

<%@ Register TagPrefix=”CLV” Namespace=”WebApplication1.Concepts.Validators” assembly=”WebApplication1″%>

<form id=”form1″ runat=”server”>
<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server”></asp:CheckBoxList>
<CLV:ListValidator runat=”server” ID=”custLstVal” ControlToValidate=”CheckBoxList1″ ErrorMessage=”At least one item in the checkboxlist should be checked”
EnableClientScript=”true”/>
<asp:RadioButtonList ID=”RadioButtonList1″ runat=”server”></asp:RadioButtonList>
<CLV:ListValidator runat=”server” ID=”custRadVal” ControlToValidate=”RadioButtonList1″ ErrorMessage=”At least one item in the radiobuttonlist should be checked”
EnableClientScript=”true”/>
<br />
<br />
<asp:Button ID=”Button1″ runat=”server”  Text=”Button” />
</form>

Web Page Code Behind Code

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<string> lstStr = new List<string>();
lstStr.Add(“Item 1”);
lstStr.Add(“Item 2”);
lstStr.Add(“Item 3”);
lstStr.Add(“Item 4”);
CheckBoxList1.DataSource = lstStr;
CheckBoxList1.DataBind();
RadioButtonList1.DataSource = lstStr;
RadioButtonList1.DataBind();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s