C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Javascrip for Select All Checkbox


Posted Date: 13 Jun 2007    Resource Type: Code Snippets    Category: Validations

Posted By: Brij Mohan       Member Level: Bronze
Rating:     Points: 10



This code uses Javascrip to perform operation like Select All,
For eg: In screen we Have
1) Check 1
2) Check 2
3) Check 3
4) Check 4
5) Select All

These check boxes are created dynamically by ASP.NET, C# code, In client side just we need to write the javascrip code.

C#, code behind to create Dynamic check boxes

private void CreateCheckBox(string chkName,ArrayList lstCheckItems)
{
StringBuilder sCheckStr=new StringBuilder();
for(int i=0;i {
sCheckStr.Append(""+ lstCheckItems[i] +"
");
}
divFurtherInfo.Text=sCheckStr.ToString();
}


Javascript to Perform 'Select All' operation, with self descriptive comments

function SelectAll(object, all)
{

var checkedCount=1;
var selectedText="";

//get the total number of itmes in the checkboxes
var count=document.getElementById("txtCountInfo").value;
//if status is false set it true else set if false, but check status only onclick
//of 'All the above' checkbox, and when the checked status of 'All the above' is false.
if(!all) {
if(state && object.id=="cblFurtherInfo"+(count-1) && document.getElementById("cblFurtherInfo"+(count-1)).checked==false)
state=false;
else
state=true;

for(i=0;i {
//if 'All the above' is clicked select all the items, else even if one is unchecked
//then Uncheck 'All the above'
if(object.id=="cblFurtherInfo"+(count-1))
document.getElementById("cblFurtherInfo"+i).checked=state;
else
document.getElementById("cblFurtherInfo"+(count-1)).checked=false;

//this is to get all the selected items, and store it as semicolon seperated in a variable,
//to save in database, but dont capture 'All the above'.
if(document.getElementById("cblFurtherInfo"+i).checked==true && document.getElementById("cblFurtherInfo"+i).value!="All the above")
{
selectedText=selectedText+document.getElementById("cblFurtherInfo"+i).value+";";
checkedCount=checkedCount+1;
}
}

//check if all the checkbox is selected then select 'All the above' check box also,
//if all are selected then selectedCount will be equal to Total number of items-1(Excluding 'All the above')
if(checkedCount==count)
document.getElementById("cblFurtherInfo"+(count-1)).checked=true;
//Assigining the value of all the selected items in the hidden text box, to use in CodeBehind
document.getElementById("txtSelectedFurtherInfo").value=selectedText;
}
//this is used for page load event, where we have to select all the items.
else
{
for(i=0;i {
//selecting all the checkboxes to true and assigning the selected values to hidden text field
document.getElementById("cblFurtherInfo"+i).checked=true;
if(document.getElementById("cblFurtherInfo"+i).value!="All the above")
selectedText=selectedText+document.getElementById("cblFurtherInfo"+i).value+";";
}
document.getElementById("txtSelectedFurtherInfo").value=selectedText;
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Detect Weekend in JavaScript and Confirm
Previous Resource: Text Box Validation for allowing only alpha and special characters of @ and _ in VB.Net
Return to Discussion Resource Index
Post New Resource
Category: Validations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use