Here is a User Control that serves as a custom validator for a .NET CheckBoxList control.
ASCX file:
- <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CheckBoxListCustomValidator.ascx.cs" Inherits="MARE.Directory.UserControls.CheckBoxListCustomValidator" ClientIDMode="AutoID" %>
- <asp:CustomValidator ID="valCheckBoxList" class="error" EnableClientScript="true" Text="Required" ErrorMessage="Required" runat="server" />
- <script type="text/javascript">
- (function ($) {
- <%= ClientValidationMethodName %> = function (sender, e) {
- var $checkBoxList = $('#<%= CheckBoxListToValidate.ClientID %>');
- e.IsValid = $checkBoxList.find(':checkbox:checked').length > 0;
- };
- $(document).ready(function () {
- var $checkBoxList = $('#<%= CheckBoxListToValidate.ClientID %>');
- // setup validation
- $checkBoxList.find(':checkbox').click(function () {
- // .NET validate the checkbox list
- var validator = document.getElementById('<%= valCheckBoxList.ClientID %>');
- ValidatorValidate(validator);
- });
- });
- })(jQuery);
- </script>
Code-behind:
- using System;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace MARE.Directory.UserControls
- {
- public partial class CheckBoxListCustomValidator : System.Web.UI.UserControl
- {
- private CheckBoxList _checkBoxListToValidate;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!string.IsNullOrEmpty(ControlToValidate))
- {
- _checkBoxListToValidate = FindCheckBoxListRecursive(this.Page, ControlToValidate);
- }
- valCheckBoxList.ClientValidationFunction = ClientValidationMethodName;
- valCheckBoxList.ServerValidate += ValidateCheckBoxList;
- }
- private string _clientValidationMethodName;
- public string ClientValidationMethodName
- {
- get
- {
- if (_clientValidationMethodName == null)
- {
- _clientValidationMethodName = string.Format("window.val{0}", Guid.NewGuid().ToString("N"));
- }
- return _clientValidationMethodName;
- }
- }
- public string ControlToValidate { get; set; }
- public CheckBoxList CheckBoxListToValidate { get { return _checkBoxListToValidate; } }
- public CustomValidator CheckBoxListValidator { get { return valCheckBoxList; } }
- public string ValidatorText
- {
- get { return CheckBoxListValidator.Text; }
- set { CheckBoxListValidator.Text = value; }
- }
- public string ValidatorErrorMessage
- {
- get { return CheckBoxListValidator.ErrorMessage; }
- set { CheckBoxListValidator.ErrorMessage = value; }
- }
- public void SetCheckBoxListToValidate(CheckBoxList checkBoxListToValidate)
- {
- _checkBoxListToValidate = checkBoxListToValidate;
- }
- internal void ValidateCheckBoxList(object sender, ServerValidateEventArgs e)
- {
- bool isValid = false;
- int i = 0;
- while (i < _checkBoxListToValidate.Items.Count && !isValid)
- {
- ListItem li = _checkBoxListToValidate.Items[i];
- isValid = li.Selected;
- i++;
- }
- e.IsValid = isValid;
- }
- private static CheckBoxList FindCheckBoxListRecursive(Control control, string id)
- {
- if (control == null) return null;
- //try to find the control at the current level
- CheckBoxList ctrl = control.FindControl(id) as CheckBoxList;
- if (ctrl == null)
- {
- //search the children
- foreach (Control child in control.Controls)
- {
- ctrl = FindCheckBoxListRecursive(child, id);
- if (ctrl != null) break;
- }
- }
- return ctrl;
- }
- }
- }
Implementation:
- <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="MARE.Directory.UserControls.MyUserControl" %>
- <%@ Register Src="../UserControls/CheckBoxListCustomValidator.ascx" TagPrefix="uc1" TagName="CheckBoxListCustomValidator" %>
- <asp:CheckBoxList ID="cblMyCheckboxList1" RepeatDirection="Horizontal" RepeatColumns="4" runat='server' />
- <uc1:CheckBoxListCustomValidator runat="server" id="valcblMyCheckboxList1" ControlToValidate="cblMyCheckboxList1" ValidatorErrorMessage="Required" />
- <asp:CheckBoxList ID="cblMyCheckboxList2" RepeatDirection="Horizontal" RepeatColumns="4" runat='server' />
- <uc1:CheckBoxListCustomValidator runat="server" id="valcblMyCheckboxList2" ControlToValidate="cblMyCheckboxList2" ValidatorErrorMessage="Required" />
No comments:
Post a Comment