locked
The base class includes the field 'CONTROLNAME, but its type (NAMESPACE.CLASS) is not compatible with the type of control (NAMESPACE.CLASS). RRS feed

  • Question

  • User341825205 posted

    In an aspx page(frmReports.aspx) I have a checklistbox control,Which I am validating using a customusercontrol(CustomListValidator.cs) as suggested by various websites. The validation control overrides the Basevalidator class and is workng fine. Now when I tried to access the Validation control in page load event i am not getting the contorl in the code behind. Now I changed the build action from content to compile for this class file. Now I am able to access the control in the page. But when I am trying to testg the page it gives the following error.

    The base class includes the field 'CBLV_chkbxlstAccode', but its type (CustomListValidator.ListValidator) is not compatible with the type of control (CustomListValidator.ListValidator).

     Can any one guide me ?

     

    frmReports.Aspx

     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="frmReports.aspx.cs" Inherits="HMLINTRANET.Forms.frmReports" %>
    <%@ Register Namespace="CustomListValidator" TagPrefix="CVL" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
    </head>
    <body class="text" bgcolor="#F8F9FC">
        <form id="form1" runat="server">
       
        <div>
     <asp:CheckBoxList ID="chkbxlstAccode" Enabled="false" runat="server" Height="101px"
                                                                Width="260px"
                                                                 ></asp:CheckBoxList>
                                                               
     <CVL:ListValidator  ID="CBLV_chkbxlstAccode" runat="server" ControlToValidate="chkbxlstAccode" ErrorMessage="A/c Code List Selection is Invalid..."  ValidationGroup="ValidationForShowERPReports"  Display="Dynamic"   >*</CVL:ListValidator>


    <asp:ImageButton ID="imgBtnShow" runat="server" ImageUrl="~/images/show.gIF" OnClick="imgBtnShow_Click"  ValidationGroup="ValidationForShowERPReports"   />
                                                           
        </div>
    </form>
    <body>
    </html>

    CustomListValidator.cs
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Text;
    namespace CustomListValidator
    {
        public class ListValidator : BaseValidator
        {
            private ListControl _listctrl;
            private bool _isRequired;
           ;
           public ListValidator()
            {
                base.EnableClientScript = false;
                _isRequired = true;
                      }
                    public bool IsRequired
            {
                get
                {
                    return _isRequired;
                }
                set
                {
                    _isRequired = value;
                }
            }
               protected override bool ControlPropertiesValid()
            {
                Control ctrl = FindControl(ControlToValidate);

                if (ctrl != null)
                {
                    _listctrl = (ListControl)ctrl;
                    return (_listctrl != null);
                }
                else
                    return false;  // raise exception
            }
            protected override bool EvaluateIsValid()
            {
                          
                int selectedCount = 0;
               
                foreach (ListItem item in _listctrl.Items)
                {
                    if (item.Selected)
                    {
                                           selectedCount++;

                    }
                }
                if (selectedCount == 0 && _isRequired)
                {
                    return false;
                }
              return true;

            }

            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("\r");
                sb.Append(@"var chkBoxList = document.getElementById(document.getElementById(ctrl.id).controltovalidate);");
                sb.Append("\r");
                sb.Append(@"var chkBoxCount= chkBoxList.getElementsByTagName(""input"");");
                sb.Append("\r");
                sb.Append(@"for(var i=0;i<chkBoxCount.length;i++){");
                sb.Append("\r");
                sb.Append(@"if(chkBoxCount.item(i).checked){");
                sb.Append("\r");
                sb.Append(@"return true; }");
                sb.Append("\r");
                sb.Append(@"}return false; ");
                sb.Append("\r");
                sb.Append(@"}</script>");
                sb.Append("\r");
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "JSScript", sb.ToString());
            }
      
        }
    }

    Friday, August 5, 2011 6:27 AM

All replies