Monday, 2 January 2012

CheckBoxList Clint side validations Using Javascript in ASP.Net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="checklist.aspx.cs" Inherits="checklist" %>

<!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 runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function projectselect()
{

var BaseControl = null;
BaseControl = document.getElementById('<%= this.form1.ClientID %>');
if (BaseControl == null)
return false;           
var ChildControl = "CheckBoxList1";
var Inputs = BaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
 if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(ChildControl, 0) >= 0 && Inputs[n].checked)
  var valid= true;
if(!valid)
{
alert('Select at least one ..!');
 return false;
 
}
}
    </script>
    
    
</head>
<body>
    <form id="form1" runat="server">
    
    <h2 style=" color:blue; font-style:normal ">  
            How to use CheckBoxList  
            <br />  in asp.net  
        </h2> 
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem>HI</asp:ListItem>
        <asp:ListItem>HOW</asp:ListItem>
        <asp:ListItem>ARE</asp:ListItem>
        <asp:ListItem>YOU</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor=" blue" ></asp:Label><br /><br />
       
        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"  OnClientClick="projectselect()"
            Font-Bold="true"  
            ForeColor="blue" />
        <asp:Button
            ID="Button2" runat="server" Text="Clear" onclick="Button2_Click"  Height="30"  
            Font-Bold="true"  
            ForeColor="blue"/>
    </div>
    </form>
</body>
</html>





using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class checklist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    var sSkill = "";
    CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
    
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;

            }
        }
      
        if (sSkill != "")
        {
            Label1.Text = sSkill;
        }
        else
        {
            Label1.Text = "select one";
        }

}


       
protected void Button2_Click(object sender, EventArgs e)
{

    var sSkill = "";
    CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
    foreach (ListItem oItem in project.Items)
   {
       if (oItem.Selected=false)
       {
        
       }
   }

Label1.Text = "";
}
}






No comments:

Post a Comment