Check-Box List Validation In Asp.net

Here we will see how to validate check box list control in asp.net. We will use javascript and custom validation to implement this functionality.
The code is as follows:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function CheckboxValidation(sender, args) {
            var checkBoxList = document.getElementById("<%=chk_list1.ClientID %>");
            var checkboxes = checkBoxList.getElementsByTagName("input");
            var isValid = false;
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].checked) {
                    isValid = true;
                    break;
                }
            }
            args.IsValid = isValid;
        }       
    </script>
</head>
<body>
    <form id="form1" runat="server">        
        <div>
            <asp:CheckBoxList runat="server" ID="chk_list1">
                <asp:ListItem Text="Example 1" Value="Example 1"></asp:ListItem>
                <asp:ListItem Text="Example 2" Value="Example 2"></asp:ListItem>
                <asp:ListItem Text="Example 3" Value="Example 3"></asp:ListItem>
                <asp:ListItem Text="Example 4" Value="Example 4"></asp:ListItem>
                <asp:ListItem Text="Example 5" Value="Example 5"></asp:ListItem>
                <asp:ListItem Text="Example 6" Value="Example 6"></asp:ListItem>
            </asp:CheckBoxList>
            <asp:CustomValidator ID="Checkbox_CustomValidator" runat="server"
                ValidationGroup="CheckBoxValidation"                
                Display="None"
                ClientValidationFunction="CheckboxValidation"                                
                ErrorMessage="One or more checkboxes must be chosen">
            </asp:CustomValidator>
            <asp:Button runat="server" ID="btn_submit" Text="Validate" ValidationGroup="CheckBoxValidation" />
            <asp:ValidationSummary runat="server" ID="validateionsummary" ValidationGroup="CheckBoxValidation" ShowMessageBox="true" ShowSummary="false" />
        </div>
    </form>
</body>
</html>
Advertisements

Solution To Request Timed Out Exception

Here we will see how to resolve the “Request Timed Out” issue in asp.net web applications. The basic reason for this exception to be thrown is that it exceeds the maximum number of seconds that a request is allowed to execute. Default value for the executionTimeout attribute is 90 seconds. This value can be altered by specifying executionTimeout in <httpRuntime> element under <system.web> section in web.config file. The executionTimeout attribute indicates the maximum number of seconds a request is permitted to run before being terminated by the Asp.net web application. This error occurs mostly in either uploading large files or if you run a query that takes more than 90 seconds to process.

Following are the steps we need to follow to set the executionTimeout attribute value in the web.config file.

Step 1) Open the web.config file.

Step 2) Add the httpRuntime element in the system.web section.

Step 3) Add executionTimeout attribute to the above added httpRuntime element.

 <httpRuntime executionTimeout="3600" />

[Note : Increase the value of the executionTimeout attribute according to your requirement.]

Step 4) Save web.config file.

Solution To Maximum Request Length Exceeded

Here we will try to resolve the common issue that many developers face with the file upload functionality in asp.net web application.

File upload maximum length exceed

The basic reason for this exception to be thrown is that it exceeds the maximum request length. Default value for the upload file is 4MB (4096 KB). This value can be altered by specifying maxRequestLength in <httpRuntime> element under <system.web> section in web.config file.

Following are the steps we need to follow to set the maxRequestLength attribute value in the web.config file.

Step 1) Open the web.config file.

Step 2) Add the httpRuntime element in the system.web section.

Step 3) Add maxRequestLength attribute to the above added httpRuntime element.

 <httpRuntime maxRequestLength="1048576" />

[Note : Increase the value of the maxRequestLength attribute according to your requirement.]

Step 4) Save web.config file.

For IIS7 and above, you also need to add:

  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>

Note that maxAllowedContentLength is measured in bytes while maxRequestLength is measured in kilobytes. The maxRequestLength indicates the maximum file upload size supported by Asp.net while maxAllowedContentLength specifies the maximum length of content in a request supported by IIS